Pip is the official package manager for Python and the standard way to install libraries from the Python Package Index (PyPI). PyPI hosts hundreds of thousands of packages covering web development, data science, machine learning, automation, and more. Pip is not installed by default on Ubuntu 18.04, but the setup is quick.
This guide shows you how to install pip on Ubuntu 18.04 for both Python 3 and Python 2, and walks you through the most useful pip commands for day-to-day package management.
<strong>Prerequisite:</strong> You need sudo access.
Python 3 (recommended). Ubuntu 18.04 ships with Python 3 as the default. Install pip for Python 3 with:
bashsudo apt updatesudo apt install python3-pip
This also installs the header files and build tools required to compile Python extensions. Verify the installation:
bashpip3 --version
The output shows the pip version and the Python version it is linked to, for example: pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6).
Python 2. Python 2 is not installed by default in Ubuntu 18.04. To install both Python 2 and its pip package in one command:
bashsudo apt updatesudo apt install python-pip
Verify with:
bashpip --version
Before installing Python packages globally with pip, it is worth knowing when to reach for apt instead.
If a Python package is available through the Ubuntu repositories, install it with apt. Those packages are tested for compatibility with Ubuntu’s system libraries and receive security updates through the normal Ubuntu update process.
Use pip for packages that apt does not offer, or when you need a newer version than what Ubuntu provides.
The safest approach for most projects is to use pip inside a virtual environment. Create one with python3 -m venv myenv and activate it with source myenv/bin/activate. Once inside, pip installs packages into an isolated environment for that specific project only. This way, upgrading a package for one project will not break another.
Install a package (latest version):
bashpip3 install scrapy
Install a specific version:
bashpip3 install scrapy==1.5
Install from a requirements file. A requirements.txt file lists the packages a project needs, with optional version pins. It is the standard way to share and reproduce a Python environment:
bashpip3 install -r requirements.txt
Generate a requirements file from your current environment with pip3 freeze > requirements.txt. This captures all installed packages and their exact versions.
List installed packages:
bashpip3 list
Upgrade a package:
bashpip3 install --upgrade package_name
Uninstall a package:
bashpip3 uninstall package_name
Replace pip3 with pip in any of these commands when working with Python 2. To get help for any subcommand, run pip3 <command> --help.
Pip is now installed on your Ubuntu 18.04 system. Visit the pip user guide to learn about dependency resolution, trusted hosts, and configuration files. Leave a comment below if you run into any issues.
R is an open-source programming language and environment built for statistical computing and data visualization. It…
Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…
Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…
GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…
Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…
WordPress is the most popular open-source CMS in the world, powering over 40% of all websites…