If you have ever used Python, you know that you need to install various libraries and packages using pip, the native package manager. Using pip, you can search and install packages from the package indexes, mainly from the Python Package Index (PyPI).
During this process, pip downloads your desired package and installs it and any dependencies that are needed to make the package work.
In this comprehensive guide, we’ll go into the basics of installing pip for both Python 2 and Python 3 on Ubuntu 20.04.
Let’s start with the prerequisites.
You need an Ubuntu 20.04 instance and a user with sudo privileges.
Install pip For Python 3
Before starting the process, make sure you’re logged in as the root user.
In the terminal, start with the update command for the package manager.
# sudo apt update -y
Once the command finishes, use the following command to install pip for Python 3:
# sudo apt install python3-pip
During the installation process, the command will also download and install the dependencies required for using and building the libraries.
It is important to verify that pip is installed properly. A simple way of doing this is to find out the version of the pip installed on the system. For this, in the terminal, run the following command
# pip3 --version
During our test run, we got the following output.
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
Install pip for Python 2
Installing pip for Python 2 is a slightly longer process because Ubuntu 20.04 doesn’t come with pip for Python 2.
To work around this, we’ll use the get-pip.py script. Let’s start the process by enabling the universal repository with the following command:
# sudo add-apt-repository universe
Next, update the package index with the update command:
# sudo apt update -y
Chances are you don’t have Python 2 installed on your Ubuntu machine. If that’s the case, install it.
# sudo apt install python2
At this point, Python 2 is installed on your machine and you’re ready to install pip for Python 2. Now download the get-pip.py script with the following curl command:
# curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
This is the output of the command:
Now that the script is available, we can easily install pip for Python 2 by running the script.
Important: If you run the script as root, it will be installed globally for all users. On the other hand, if you want to install pip only for a specific user, log in with the user and run the script.
We’ll install the script for all users with the following command:
# sudo python2 get-pip.py
During the installation, the script will also install additional setup tools and wheel, which allow you to install source distributions.
Here’s a screenshot of the output:
Once the process is complete, you need to verify that pip for Python 2 is installed correctly. For this, we’ll check the version of the pip with the following command:
# pip2 --version
If pip has been installed properly, you’ll see something like this:
pip 20.3.4 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
How To Use pip
Now that you’ve installed pip on your Ubuntu machine, it is now time to understand how you can use pip for managing Python packages. Generally, pip is most frequently used for installing packages from PyPI.
Get More Information About pip Command
Let’s start with how you can view the various available options and commands with the following command:
# pip3 --help
This will list all the included commands and options you can use with pip.
Since this is a long list, finding what you’re looking for requires time. To get straight to the point, you can use the — help option with a specific command to get more information and a list of options. For instance, here’s how you can get information about the install command:
#pip install --help
Install Python Packages
Installing packages is perhaps the most frequent task you’ll carry out with pip. For this, you’ll use the install command.
For instance, if you want to install Scrapy, a popular data scraping library, use the following command:
# pip3 install scrapy
This command will download the required libraries and install the Scrapy package. You can follow the progress in the terminal.
Get the List of Installed Packages
If you want to get a list of all the packages installed on your machine, pip provides a simple list command that generates a list of all installed packages.
# pip3 list
Here’s the output on our test machine:
Upgrade Installed Packages
After installation, the most frequent task is upgrading installed Python packages. pip offers a simple upgrade command that takes the name of the package as the option.
# pip3 install --upgrade package_name
Finally, you can use pip to uninstall packages from the Ubuntu machine with the uninstall command. Like the install command, you need to provide the name of the package you wish to uninstall.
# pip3 uninstall package_name
If you’re a Python developer, pip is a must-have tool that you use on a regular basis. It simplifies managing Python packages and helps streamline your development workflows. We discussed the process of installing pip on Ubuntu 20.04 for both Python 3 and Python 2.
Let us know in the comments how you install pip and what are your favorite Python packages.