How to Install PIP on Ubuntu 20.04

Python is a high-level programming language that has a thriving open-source community. There are new packages in a well-maintained global repository called Python Package Index (pypi.org). You can easily install PIP on Ubuntu and download any package of your choice. 

One of the obvious reasons why Python is such a diverse and widely-used language is its open-source community. It has quickly become popular in many emerging fields like data science, machine learning, and automation.  Such popularity warrants a simple and easy-to-use interface. PIP, short for Pip Installs Packages, is the package management tool for Python. 

With PIP, you can download and install any package of your choice from this repository and others like it. PIP is different for each of the two main versions of Python, which are, Python 2 and Python 3. While Python 3 is the current standard, Python 2 is still in demand and is a dependency for many tools.

In this article, we will provide instructions on how to install PIP on Ubuntu for both versions.

Installation Prerequisites

Please make a note of the following items before starting PIP installation.

  • Make sure you have root permissions or sudo privilege on the host machine. 
  • Python3 comes pre-installed with Ubuntu 20.04. However, you still need to install PIP.
  • You can identify Python 3 packages with the ‘python-3’ prefix, while Python 2 packages have the ‘python-2’ prefix in their names. 
  • To install Pip, you can use Ubuntu’s APT package manager.
  • PIP comes bundled with Python version >= 3.4, which means there is no need to install PIP separately.

Install PIP for Python 3

It is essential to update your Ubuntu APT package lists so that it has information about the latest and greatest version of the packages.

For example:

sudo apt update

Now, install the PIP package using APT.

For example:

sudo apt install python3-pip

Note the required space and if you have enough storage capacity. Next, say “Yes” to continue the installation. Please note that the required space may be different in your environment.

Install PIP on Ubuntu

Once install is completed, PIP and all its dependencies should be installed. Confirm installation by running a version check.

Code:

pip3 --version

If the version check displays a proper version, this means that PIP you have installed it successfully. Please note that the version may be different in your environment.

Install PIP for Python 2

PIP for Python 2 is not available in Ubuntu’s default APT repositories but is still available in the ‘universe’ repository. The Universe Repository is home to many open source packages. So, you must enable this repository in APT to access these packages with the add-apt-repository command.

Code:

sudo add-apt-repository universe
Install PIP on Ubuntu

Update your Ubuntu APT package lists after adding information about ‘universe’.

Code:

sudo apt update

The next step is to install Python 2.

Code:

sudo apt install python3

Confirm if the installation was successful or not by checking the version.

For example:

python2 –version

If the version check displays a proper version, this means that PIP you have installed it successfully. Please note that the major version will be 2, but the minor version may be different in your environment.

PIP for Python 2 is not available via APT. It is available via the ‘get-pip.py’ script. You can download this script with ‘curl’. If you do not have curl installed, install it via APT.

For example:

sudo apt install curl

Download the ‘get-pip.py’ script.

Install PIP on Ubuntu

Once the download completes, it’s finally time to install PIP with Python 2.

For example:

sudo python2 get-pip.py
Install PIP on Ubuntu

If your installation is successful, you will see the above output.

You can also confirm successful installation by running a version check.

For example:

pip2 –version

If the version check displays a proper version, this means that PIP has been installed successfully. Please note that the version may be different in your environment. Hence, after following these steps, you can successfully install PIP on Ubuntu.

Now that we have come this far, let’s also have a look at how to use PIP to install/uninstall Python packages.

Command Syntax

PIP command takes a few arguments that are described below.

pip3 <command> [options]

You can use one of the following commands.

  • Install: Install packages.
  • Download: Download packages.
  • Uninstall: Uninstall packages.
  • Freeze: Output installed packages in requirements format.
  • List: The list of already installed packages.
  • Show: Show information about installed packages.
  • Check: Verify compatibility of installed packages dependencies.
  • Config: Manage local and global configuration.
  • Search: Search PyPI for the availability of packages.
  • Wheel: Build wheels from your requirements.
  • Hash: Compute the hash of package archives.
  • Completion: Helper command for command completion.
  • Debug: Show debug output.
  • Help: Show help for the command.

You can use one or more of the following options. Only commonly used options are described here. Use –help option to list all commands and options.

  • -h or –help: Show usage of pip3 and list all commands and options
  • -v or –verbose: Increase verbosity of the output.
  • -V or –version: Show version and exit.
  • -q or –quiet: Decrease verbosity.
  • –retries <number>: Try the connection specified number of times before failing. 
  • –timeout <seconds>: Wait a specified number of seconds before disconnecting.
  • –cert <path>: Use the specified path for the CA bundle.

Use PIP to Install a Package

To install a Python package via PIP, use the following syntax.

pip3 install <package name>

Since PIP is a module of Python, the following alternate syntax can also be used.

For example:

python3 -m pip install <package name> 

For more information on installing Python, you can also check the Python Installation guide.

Below, we will try to install the ‘pandas’, which is a library written in Python for data analytics. For example:

pip3 install pandas
Install Pandas Using Pip

As you can see above, the end of the installation is marked by a message that says ‘Successfully installed <packages>’. Also, an important point to note is that PIP also installed ‘NumPy’, which is a dependency of ‘pandas’.

Use PIP to Uninstall a Package

To uninstall a Python package via PIP, use the following syntax.

pip3 uninstall <package name> 

Or the alternate syntax is:

python3 -m pip uninstall <package name>

We will now try to uninstall the ‘pandas’ package with PIP that you installed earlier.

For example:

pip3 uninstall pandas
Uninstall Pandas using PIP

As you can see above, you will see a message ‘Successfully uninstalled <package>’ when the package is completely uninstalled. The dependencies are, however, not uninstalled.

In conclusion, PIP is a handy tool to install/uninstall or manage Python packages. In this article, you have covered how to install PIP on Ubuntu 20.04. PIP is to Python as APT is to Ubuntu. PIP supports several commands for package management and takes care of package dependencies. Check its official documentation for more details. Instead of manual installation, always use PIP to install Python packages/modules.

If this guide helped you, please share it.

Leave a Reply
Related Posts