Pip (Pip Installs Packages) is a software utility that downloads packages from PyPI – the Python Package Index.

Pip is a command line program so, when installed, it adds the Pip command line to the system. You can use it to install and manage Python software packages.

This guide will walk you through installing Pip on Ubuntu 18.04.

Prerequisites

  • A Linux system running Ubuntu 18.04
  • Access to a user account with sudo privileges. Or, create a sudo user on Ubuntu.
  • Access to a terminal window/command line (Ctrl+Alt+T)

Installing Pip for Python 3

1. Open the terminal. The simplest way is to right-click on the desktop and select Open Terminal from the drop-down menu.

2. Update the package list by running the following command in the terminal:

sudo apt update

3. Install Pip for Python 3 and all the dependencies for building Python modules by running the following command:

sudo apt install python3-pip

4. The package installs quickly. To verify the install run the following command:
pip3 –version

The installed version might be different for you, but the general output should resemble the line below:
OUTPUT
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

5. To upgrade Pip3 to the latest version, you would issue the upgrade command just like for any other PyPI package:

sudo pip3 install --upgrade pip

Install Pip for Python 2

1. Open the terminal. The simplest way is to use the CTRL+ALT+T

2. Update the package list by running the following command:

sudo apt update

3. Install Pip for Python 2 and all the dependencies for building Python modules by running:

sudo apt install python-pip

4. To verify the install run the following command:

sudo pip –version

At the time of writing this article, the latest version of Pip was 9.0.1, and this may vary.
OUTPUT
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)

5. This step is optional but highly recommended. Namely, you can install a required file that contains all the packages that can be installed with Pip. To install the requirements contained in requirements.txt, run the following command:

sudo pip install -r requirements.txt

6. To upgrade Pip for Python 2 to the latest version, run the following command:

sudo pip install --upgrade pip


NOTE: If you are using Python in a virtual environment created with pyvenv or virtualenv, then Pip is available regardless of the version of Python in use. This also applies to Python 2.7.9 or newer (Python series 2) and Python 3.4 or newer (Python series 3)


Essential Pip Commands

The lines below cover the most essential Pip commands.

IMPORTANT: If you are using Pip for Python 2, use pip instead of pip3 in the commands.

List All Packages

To list installed Pip packages, use the following command:

sudo pip3 list

Search For a Package

Search for a particular package: sudo pip3 search Enter_Search_Term

Installing Software Packages

Type the following to install the latest version of a software package:

sudo pip3 install Enter_Package_Name

To install a specific version of a software package, specify the version after you have defined the software package name.

For example: sudo pip3 install Enter_Package_Name==2.4

Uninstalling a Package

To remove a Python package, type:

sudo pip3 uninstall Enter_Package_Name

Getting a List of Outdated Packages

To prompt a list of your installed outdated packages and see the latest versions available:

sudo pip3 list --outdated

See below for a sample output:

OUTPUT

workflow (Current: 1.2.0 Latest: 2.1.5)
Sphinx (Current: 1.1.2 Latest: 1.1.3)

Pip Update Packages

As the apt upgrade command, Pip can also be used to upgrade a software package to the latest version:

sudo pip3 install --upgrade Enter_Package_Name

Get Additional Details

To prompt additional details, run the following command:

sudo pip3 show Enter_Package_Name

See a sample output below:

Name: Sphinx
Version: 1.1.3
Location: /my/env/lib/pythonx.x/site-packages
Requires: Pygments, Jinja2, docutils

Python Virtual Environments (Optional)

To create a virtual environment, the python-venv module is required.

Install it with the following terminal command:

sudo apt install python3–venv

Allow the process to complete. We are using the apt package installer because we’re installing the python-venv module globally.

Once that process completes, you can create a virtual environment for Python.  Change your directory to a location where you want to store your virtual environment. For example,

/users/username/python.

Enter the following command to create a virtual environment in that path:

python3 –m venv my_test_environment

You can replace my_test­_environment with the name of your project. This environment includes Python, Pip, the Python library, and supporting files. A new directory will be created with the name you provide.

Activate the virtual environment with the following command:

source my_test_environment/activate

Your command line will change to indicate that you are operating within the new Python virtual environment. From here, you can use Pip to install a module only to this environment.

An example might look as follows:

pip install module_name

Conclusion

Installing Pip is simple. Just keep in mind that Pip is specifically for Python packages found in the Python Package Index (PyPI).

With the instructions in this guide, you have learned how to install Pip on Ubuntu 18.04 for Python 2 and Python 3.