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

Using PIP is like adding additional software repositories to Linux.  Unlike the apt command, however, PIP uses more functions.  For example, it allows a user to choose from multiple versions of software to install. It also allows a user to install software to a virtual environment.

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
  • Access to a terminal window / command line (Ctrl-Alt-T)

NOTE

Prior to moving on to the installation steps, make sure you are logged into the server as root user or use the sudo command.

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 –versionThe 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

NOTE

As with Pip3, make sure you are logged into the server as root user or use the sudo command.

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 PPIP for Python 2 and all the dependencies for building Python modules by running the following command:
sudo apt install python-pip

4. 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

Essential PIP 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 as seen in the line below:

sudo pip3 install Enter_Package_Name==2.4

Uninstalling a Package

To remove a 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)

How To Update Packages

To update a software package:

sudo pip3 install --upgrade Enter_Package_Name

NOTE

PIP uninstalls the outdated version of the package before installing the latest available version.

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

Best Practices

If you’re installing software that will be used across your whole Linux installation, it’s better to use the apt package manager to install the software. If there is no package available in the standard repositories, you may use PIP to install software globally.

However, pip is designed to be an intelligent installer for use with virtual environments.  A Python virtual environment is a self-contained system. It allows a user to create a contained and isolated environment for a Python project.  When pip is run from within a Python virtual environment, it will install the software only to that virtual environment.

There are some packages available through PIP that are not available through the standard repositories.  This is especially true for Python modules, which add features or functionality to a Python installation. PIP makes installing and managing these modules much more manageable.

Like the apt-get upgrade command, PIP can also be used to upgrade a software package to the latest version:

pip install ––upgrade package_name

Unlike the apt command, however, pip can be directed to install a specific version of the software.

Use the following syntax:

pip install package_name==2.0.6

Replace package_name with the name of the software you want to install, and 2.0.6 with the specific version you need.

This will install a Python package from the PyPI repository, with the name of module_name.  (Note: you can use the pip search command to find the name of the package you’re looking for.)

(optional) Python virtual environments

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. (Note: this process uses 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. An example might be /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’re 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 and using PIP is simple. Just remember when to use the apt package manager, and when to use PIP.  Keep in mind that PIP is specifically for Python packages, but apt is for your whole operating system.

With the instructions in this guide, you have learned how to install PIP on Ubuntu 18.04 & Python 2. We also went over installing PIP3 on Python 3.