In this tutorial, you will learn how to install Python pip on Ubuntu 22.04.
Pip is a package management system written in Python and is used to install and manage software packages. It connects to an online repository of public packages called the Python Package Index. It can also be configured to connect to other package repositories.
Usually, Pip is automatically installed while working in a virtual environment, and also if you are using Python that has not been modified by the redistributor to remove ensure pip.
Pip is recommended tool for installing Python packages. For you to start using pip, you need to have python3 installed on your system.
Installing Python pip on Ubuntu 22.04
You can install pip either through a virtual environment or while installing Python. It depends on which one you prefer. I will be using a virtual environment to install pip on Ubuntu 22.04.
1. Run system updates
First, we need to update our repositories to make them up to date by issuing update and upgrade commands on our terminal.
$ sudo apt update && apt upgrade -y
2. Create a virtual environment
A virtual environment is used to manage Python packages for different projects. It creates an isolated environment for pip packages.
Begin by creating a directory where your virtual environment will be located.
$ sudo mkdir nextgen $ cd nextgen
Next, create a virtual environment
$ python3 -m venv env
env is the name given to a virtual environment.
In case you encounter the following error, follow the instruction in order to make necessary changes.
The virtual environment was not created successfully because ensurepip is not available. On Debian/Ubuntu systems, you need to install the python3-venv package using the following command. apt install python3.10-venv You may need to use sudo with that command. After installing the python3-venv package, recreate your virtual environment. Failing command: ['/root/nextgen/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
Correct this error by running
sudo apt install python3.10-venv
From the above, you will see the following output.
The following additional packages will be installed: python3-pip-whl python3-setuptools-whl The following NEW packages will be installed: python3-pip-whl python3-setuptools-whl python3.10-venv 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Need to get 2473 kB of archives. After this operation, 2882 kB of additional disk space will be used. Do you want to continue? [Y/n]
The next thing is to run
python3 -m venv env again.
sudo python3 -m venv env
You need to activate the virtual environment to start using it.
$ source env/bin/activate (env) root@localhost:~/nextgen#
Now that we have activated the virtual environment, you can now check if pip has been installed, because pip is installed while creating the virtual environment.
pip --version pip 22.0.2 from /root/nextgen/env/lib/python3.10/site-packages/pip (python 3.10)
We have learned how to install pip on Ubuntu 22.04 because this is the starting point for any python programming. I am glad you have enjoyed the tutorial. Feel free to comment in case you are faced with a challenge.