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.
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.
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 theget-pip.py
script.
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
Tip: Use the command below to installcurl
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.
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
Use the command syntax below to view the various options available for each command.
pip3 [command] --help
e.g
pip3 install --help
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 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
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
Uninstall a package
To uninstall any package installed using pip, execute the command below.
pip3 uninstall [package-name]e.g
pip3 uninstall scapy
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.