A package manager called Pip is used to install, upgrade, configure, and manage Python dependencies. Using pip, you can search for, download, and install packages from the Python Package Index (PyPI) and other package indexes.
This page has instructions on how to set up pip on Ubuntu 20.04 to 22.04 for both Python 3 and Python 2. Along with that, we will show you how to use pip to install and manage packages.
Prerequisites / More information
Python has two different versions: Python 2 and Python 3. Python 3 is installed with the base system, and Python 2 can be installed from the default repositories in Ubuntu. Python 3 users are urged to use it.
Most of the time, the best way to install a Python module globally is to use the apt tool to install the module’s deb package. This is because deb packages are tested to work well on Ubuntu systems. If there is no deb package for a module, use pip to install it globally.
Packages for Python 3 start with python3-, and packages for Python 2 start with python2-.
You should only use pip inside of a virtual environment. With Python Virtual Environments, you can install Python modules in a separate place for a single project instead of having them installed everywhere. So, you don’t have to worry about how your changes will affect other Python projects.
How to install pip for Python 3
In Ubuntu versions 20.04 through 22.04, installing pip for Python 3 is a breeze. Execute the following in the terminal as the root or sudo user:
sudo apt update
sudo apt install python3-pip
All the prerequisites for constructing Python modules are also set up using the preceding command.
When everything is set up, double check by looking at the pip version:
pip --version
The version number may vary, but it will look something like this:
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)
How to install pip for Python 2
The Ubuntu 22.04 repositories do not have Pip for Python 2. We’ll use the get-pip.py script to set up pip for Python 2.
If Python 2 is not already on your system, you can install it by running:
sudo apt update
sudo apt install python2
To get the get-pip.py script, use 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 with the python2 binary:
sudo python2 get-pip.py
Using the above command installs pip everywhere. If you only want it installed for your user, run the command without sudo. The script also installs the setuptools and wheel packages, which let you set up source distributions.
Print the pip version number to verify the installation:
pip2 –V
pip 20.3.4 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
How to Use Pip
The following part will teach you the fundamental pip commands you’ll need. Enter the following to see a complete list of pip commands and options:
pip --help
Using pip command> —help, you can learn more about that command. If you want to know more about the install command, for instance, you may type:
Pip install --help
How to install Packages with Pip
The most fundamental use for the pip tool is to do an installation. Suppose you’re interested in setting up NumPy.
This is the command to perform if you want to install a package’s most recent version:
pip install <package_name>
You may type to set up the NumPy package.
pip install numpy
After the package name, append == and the version number to install a specific revision:
pip install numpy==1.18.5
Replace pip with pip2 if using Python 2.
How to install Packages with Pip using the Requirements Files
A Python project’s requirement.txt file specifies the pip packages and their versions that are needed to compile and operate the project.
The following command will install the contents of a file containing a list of prerequisites:
pip install -r requirements.txt
Listing Installed Packages
Use the “list” pip subcommand to see a list of all currently installed packages:
pip list
Upgrade a Package With Pip
To update an existing installation to the most recent version, type:
pip install --upgrade package_name
Uninstalling Packages With Pip
This is the command you need to execute in order to remove a package:
pip uninstall package_name
Conclusion
You now know how to set up pip on Ubuntu and use it to handle Python package management. Visit the pip user guide website for additional details.
Please use the space below to ask questions and offer suggestions..