Home Ubuntu How to install Pip on Ubuntu

How to install Pip on Ubuntu

by Karen
pip on ubuntu

Are you a Python developer? If yes, then you have probably come across the Pip utility. Pip is a Python command line tool that allows you to download, install and manage Python packages on your system. Think of it as NPM or YARN, which Javascript developers use to manage NodeJS packages.

This post will provide a comprehensive guide on installing pip for Python2 and Python3 on Ubuntu 20.04 LTS. The steps described in this post will also work for Ubuntu 16.04 LTS and other newer releases.

Why pip2 and pip3?

You might be wondering why there are different versions of pip? That’s because there are currently two Python flavors in the tech industry – Python2 and Python3. As the names suggest, pip2 is used for managing Python2 packages while pip3 is used to manage Python3 packages.

Although pip works well in managing packages; when installing a Python module globally, it’s highly recommended to use the Ubuntu apt package manager. However, that will also depend if the package is available in the Ubuntu repository. These packages are developed and tested to work well in Ubuntu systems.

Installing packages with pip is recommended when working with Python virtual environments. Virtual environments allow you to install a package inside an isolated environment. That way, you don’t have to worry about conflicting packages from other projects breaking your application or program.

Note: Since the release of Ubuntu 20.04 LTS, the only Python version that comes pre-installed is Python3.

Installing pip for Python3

Launch the Terminal and execute the commands below to install pip for Python3 on your Ubuntu system.

sudo apt update
sudo apt install python3-pip

The command above will download and install pip3 on your system. When done, run the command below to verify the installation.

pip3 --version

You should see an output similar to the image below, although the release version might be different.

pip3 version

Pip3 version

Installing pip for Python2

Before installing pip for Python2, you first need t install Python2 on your system as it doesn’t come pre-installed on Ubuntu 20.04 or newer releases. Follow the steps below.

  • Enable the universe repository by executing the command below.
    sudo add-apt-repository universe
  • Update your system and install Python2 using the commands below.
    sudo apt update
    sudo apt install python2
  • Use the command below to verify if you installed Python2 successfully.
    python2 --version
    You should see an output similar to the image below, although your release version might be different.

    python2 version

    Python2 Version

Since pip for Python2 is unavailable in the Ubuntu 20.04 LTS repositories, we will use the get-pip.py script to install it on our system.

Follow the steps below.

  • Use the curl command to download the get-pip.py script.
    curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
    Tip: Use the command below to install curl if you don’t have it already installed on your system.
    sudo apt install curl
  • Run the script using Python2, as shown below.
    sudo python2 get-pip.py
  • Execute the command below to verify whether you successfully installed pip for Python2.
    pip2 --version

You should see an output similar to the image below.

pip2 version

pip2 version

How to use pip

Tip: This post will give examples using pip3 since we have only Python3 installed.

This section will give you a list of helpful pip commands to download and install packages from PyPI. One of the basic commands you can use is the --help command which lists all the available commands you can use with pip.

pip3 --help
pip3 help command

pip3 help command

Use the command syntax below to view the various options available for each command.

pip3 [command] --help

e.g

pip3 install --help
pip3 command options

pip3 command options

Install packages with pip

As an example, this post will install Scapy – a Python package used to create, forge and decode network packets. However, you can use the command syntax to install any other package of your choice.

pip3 install [package-name]
e.g

pip3 install scapy
install scapy

Install scapy

Install a specific version of a package with pip

There are situations where you might want to install a specific version of a package. Luckily, pip has provision for that feature. Use the syntax below.

pip3 install [package-name]==[version-number]e.g
pip3 install scapy==2.4.5

Install packages using the requirements.txt File

When you download a Python project from a platform like GitHub, you will notice that it comes with a requirements.txt file containing a list of all Python modules required to run the project. You can easily install all these packages with pip using the command below.

pip3 install -r requirements.txt

List all Installed Packages with pip

If you want to keep track of all the packages you have installed with pip, you can easily do so using the command below.

pip3 list
list pip3 packages

List pip3 packages

Upgrade a package with pip

If you want to install an update of an already installed package, you can easily do so using the command below.

pip3 install --upgrade [package-name]e.g
pip3 install --upgrade scapy
upgrade a pip package

Upgrade a pip package

Uninstall a package

To uninstall any package installed using pip, execute the command below.

pip3 uninstall [package-name]e.g
pip3 uninstall scapy
uninstall a package with pip

Uninstall a package with pip

Conclusion

This post has given you a comprehensive guide on installing and working with pip on your Ubuntu system. It has shown you:

  • How to install pip for Python2
  • How to install pip for Python3
  • How to install, manage, upgrade, and uninstall packages with pip

Did you come across any issues, or do you have any comments regarding this post? Don’t hesitate to let us know in the comments below.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.