Install Python Pip on Ubuntu 22.04

October 13, 2022 · 0 min read
Install Python Pip on Ubuntu 22.04
Pip is a Python package manager for installing, upgrading, configuring, and managing project dependencies. You can use pip to find, download, and install packages from the Python Package Index (PyPI) and other package indexes.

This tutorial will show you how to install pip for Python 3 and Python 2 on Ubuntu 22.04. We'll also go over the fundamentals of installing and managing Python packages with pip.

Before You Begin

Python is available in two flavors: Python 2 and Python 3. Python 3 is installed as part of the base system, and Python 2 can be obtained from the default Ubuntu repositories. Python 3 is recommended for users.

When installing a Python module globally, it is best to use the apt tool to install the module's deb package because it has been tested to work properly on Ubuntu systems. If there is no deb package for a module, use pip to install it globally.
Python 3 packages begin with python3-, and Python 2 packages begin with python2.

You should only use pip within a virtual environment. Python Virtual Environments allow you to install Python modules in a separate location for each project rather than installing them globally. You won't have to worry about affecting other Python projects this way.

Installing pip for Python 3

Installing pip for Python 3 on Ubuntu 22.04 is a simple procedure. In your terminal, run the following commands as the root or sudo user:

sudo apt update
sudo apt install python3-pip

The command above also installs all of the dependencies required for Python module development.
Once the installation is complete, double-check the pip version:

pip3 --version

The version number may differ, but it will look like this:

pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)

Installing pip for Python 2

The Ubuntu 22.04 repositories do not include Pip for Python 2. The get-pip.py script will be used to install pip for Python 2.
If Python 2 is not already installed on your system, install it by running:

sudo apt update
sudo apt install python2

Download the get-pip.py script with curl:

curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py

To install pip for Python 2, run the script as the sudo user and use the python2 binary:

sudo python2 get-pip.py

The preceding command installs pip globally. Run the command without sudo if you only want to install it for your user. The script also includes the setuptools and wheel packages required to install source distributions.
Print the pip version number to confirm the installation:

pip2 --version

The output will look something like this:

pip 20.3.4 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

How to Use Pip

In this section, we'll go over some basic pip commands. Type: to get a list of all pip commands and options.

pip3 --help

Using pip <command> --help, you can get more information about a specific command. For example, to learn more about the install command, enter:

Installing Packages with Pip

The pip tool's most basic function is to install a package. Assume you want to install Numpy.

To install the most recent version of a package, use the following command:

pip3 install <package_name>

To install the NumPy package, for example, type:

pip3 install numpy

To install a specific version of a package, follow the package name with == and the version number:

pip3 install numpy==1.18.5

Installing Packages with Pip using the Requirements Files

requirement.txt is a text file that contains a list of pip packages and their versions that are required to run a particular Python project.
Use the following command to install a list of requirements specified in a file:

pip3 install -r requirements.txt

Listing Installed Packages

Use the "list" subcommand to see a list of all installed pip packages:

pip3 list

Upgrade a Package With Pip

To update an already installed package to the most recent version, type:

pip3 install --upgrade package_name

Uninstalling Packages With Pip 

To uninstall a package, use the following command:

pip3 uninstall package_name

Conclusion

We demonstrated how to install pip on Ubuntu and use pip to manage Python packages. Visit the pip user guide page for more information.


Need help? Call our award-winning support team at +254 114 450 462