Enter your email address below and subscribe to our newsletter

How to Install Pip in Ubuntu

Learn how to install and use pip on Ubuntu with virtual environments.

Share your love

You will find multiple tools which depend on pip packages and that’s where the confusion starts as you can not install those packages via your traditional package manager like apt.

Pip (Pip Installs Packages) is a command line package manager to install Python packages which are listed in the Python Package Index (PyPI).

So in this tutorial, I will walk you through the following:

  • How to install pip in Ubuntu
  • How to install packages in isolation
  • Bonus: Installing python packages without creating virtual environment

Let’s start with the first one.

Installing Pip in Ubuntu

As pip is available in the default repository of Ubuntu, you can install pip using the apt packager. To use the apt package manager for installing pip, use the following:

sudo apt update && sudo apt install python3-pip python3-venv

Once done, you can check the installed version of pip using the following command:

pip3 --version
Install pip in Ubuntu using apt

Now, let’s take a look at how to use a pip package manager in the virtual environment.

How to use Pip in a Virtual Environment

The reason why you must install Python packages in the virtual environment is pip won’t let you install them system-wide.

For example, earlier, we used to install packages using the pip install command and now, if I try using that command, it will give me an error saying “externally-managed-environment”:

This environment is externally managed

This error suggests that you can no longer install Python packages without isolation. So, let’s take a look at how to do it.

Create a virtual environment

To create a virtual environment, you need to use the venv flag with the python3 command and replace <env-name> with the actual name of the virtual environment:

python3 -m venv <env-name>

For example, if I want to name my virtual environment env, then I will use the following command:

python3 -m venv env

Once you create the virtual environment, you can activate it using the source command and by entering the name of your virtual environment:

source <env-name>/bin/activate

As I named my virtual environment env, I will use the given command to activate my virtual environment:

source env/bin/activate
Create vitual environment to use Pip in Ubuntu

Using pip inside the virtual environment

Once you get into the virtual environment, you can use the pip package manager to perform various tasks. Here’s a table suggesting the basic use of the pip package manager:

CommandDescription
pip3 install –upgrade <package-name>Upgrades a software package to the latest version.
pip listLists installed packages.
pip list –outdatedLists outdated packages and shows the latest versions available.
pip3 search <package-name>Searches for a particular package.
pip3 install <package-name>Installs the latest version of a software package.
pip3 uninstall <package-name>Removes a Python package.
pip3 show <package-name>Prints additional package details.
pip download <package-name>Downloads a package without installing it.
Basic use of the pip package manager

Now, let’s take a look at some basic use cases of pip.

Install the package using pip

To install a package using pip, you need to use the pip3 install command along with the package name as shown here:

pip3 install <package_name>

For example, if I want to install a library called requests, then I will use the following:

pip3 install requests
Install package using pip in ubuntu

Upgrade a package using pip

To upgrade a package using pip, you need to use the pip install with the --upgrade flag as shown here:

pip install --upgrade <package-name>

For example, if I want to upgrade the requests package, then I will use the following:

pip install --upgrade requests
Upgrade package using pip

Uninstall the package using the pip

To uninstall the package that you installed using pip, use the pip3 uninstall command as shown here:

pip3 uninstall <package-name>

So let’s say I want to uninstall a package named requests, then I will use the following:

pip3 uninstall requests
Uninstall a package using pip in ubuntu

Bonus: Use pipx to automate virtual environments

I find creating and managing virtual environments a tedious task and that’s when I came across a tool called pipx which is similar to pip but takes care of the virtualisation of packages automatically and you don’t have to switch to the environment either.

To install pipx on Ubuntu, you can use the following command:

sudo apt install pipx

Once done, add it to the $PATH so it can be used from anywhere:

pipx ensurepath

Now, you can use the pipx as the pip command and flags will remain the same. For example, if I want to install a package using pipx, I’d use the following:

pipx install <package-name>
Install package using pipx

Conclusion

In this tutorial, I went through how you can install pip on Ubuntu and how you can create a virtual environment to install and manage pip packages.

I hope you would have found my bonus tip helpful.

Share your love
Kabir
Kabir

A tech journalist whose life revolves around networks.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!