Python pip is the standard package manager for installing, upgrading, and managing Python libraries from the Python Package Index (PyPI) and other repositories. Pip handles dependency resolution automatically, ensuring required packages install alongside your target library, and supports version pinning to maintain consistent project environments across development, testing, and production systems.
Pip integrates seamlessly with Python virtual environments, letting you isolate project dependencies and avoid system-wide package conflicts. This guide covers installing pip on Ubuntu, verifying the installation, and using common package management commands to install, upgrade, list, and remove Python libraries.
How to Install Python Pip on Ubuntu via APT
Use the APT package manager to install Python pip on Ubuntu. Update your package index first, then install pip with the latest repository metadata:
sudo apt update
sudo apt install python3-pip
Next, verify the installation by checking the pip version:
python3 -m pip --version
The output displays the installed pip version, Python version it’s associated with, and the installation path:
pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)
This confirms pip installed correctly and shows which Python interpreter it uses. The version numbers will vary based on your Ubuntu release, but the format remains consistent across versions.

System vs Virtual Environment Package Installation
While you can install Python packages system-wide with pip, best practice recommends using virtual environments for project-specific dependencies. Virtual environments isolate package installations, preventing version conflicts between projects and avoiding permission issues or system-wide package corruption. Install packages with apt when they’re system utilities or dependencies for other software; use pip within virtual environments for development work.
Install the virtual environment module if your minimal Ubuntu image does not include it, then create and activate a dedicated environment for your project:
sudo apt install python3-venv
python3 -m venv ~/venvs/project
source ~/venvs/project/bin/activate
Ubuntu 22.04 LTS and newer versions implement PEP 668, which restricts system-wide pip installations to protect packages managed by apt. Virtual environments isolate your project dependencies completely, preventing version conflicts and avoiding system package corruption. This is the recommended approach for all development work.
Python Pip Command Examples
The following examples demonstrate common pip operations. Use these commands within activated virtual environments for project work, or add the --user flag for user-level installations that avoid system package conflicts. Running pip via python3 -m pip ensures you use the correct Python interpreter even when shell aliases differ.
Install Python Packages
Install packages from PyPI or other repositories with the install command. This downloads the package, resolves dependencies automatically, and makes the library available in your Python environment.
python3 -m pip install package_name
For example, to install the requests library, run the following command:
python3 -m pip install requests
The installation output shows pip downloading the package, resolving dependencies, and confirming successful installation:
Collecting requests Downloading requests-2.31.0-py3-none-any.whl (62 kB) Collecting charset-normalizer<4,>=2 Downloading charset_normalizer-3.3.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (142 kB) Collecting idna<4,>=2.5 Downloading idna-3.6-py3-none-any.whl (61 kB) Collecting urllib3<3,>=1.21.1 Downloading urllib3-2.1.0-py3-none-any.whl (104 kB) Collecting certifi>=2017.4.17 Downloading certifi-2024.2.2-py3-none-any.whl (163 kB) Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests Successfully installed certifi-2024.2.2 charset-normalizer-3.3.2 idna-3.6 requests-2.31.0 urllib3-2.1.0
Notice how pip automatically installed five packages: the requests library plus four dependencies it requires. Pip handles this dependency resolution automatically, ensuring your target package has everything it needs to function properly.
Upgrade Python Packages
Update packages to their latest versions to receive bug fixes, security patches, and new features. Pip checks PyPI for newer releases and upgrades the package while preserving compatibility with existing dependencies.
python3 -m pip install --upgrade package_name
For example, run the following command to upgrade the requests library to the latest version:
python3 -m pip install --upgrade requests
Remove Python Packages
Uninstall packages you no longer need to free disk space and reduce dependency clutter. Note that pip removes only the specified package, not its dependencies, unless those dependencies are unused by other packages.
python3 -m pip uninstall package_name
For example, run this command to remove the requests library:
python3 -m pip uninstall requests
List Installed Python Packages
View all installed packages and their versions to audit your environment, check for outdated packages, or document project dependencies. Use this before creating requirements files or troubleshooting import errors.
python3 -m pip list
The output displays package names in the first column and version numbers in the second column, sorted alphabetically:
Package Version ------------------ --------- certifi 2024.2.2 charset-normalizer 3.3.2 idna 3.6 pip 24.0 requests 2.31.0 setuptools 68.1.2 urllib3 2.1.0 wheel 0.42.0
This formatted table view makes it easy to scan your environment and identify which packages are installed and their current versions. The list includes both packages you installed explicitly (like requests) and their dependencies (certifi, charset-normalizer, etc.).
Install Specific Package Versions
Pin packages to specific versions when you need reproducible environments or must avoid breaking changes in newer releases. Version pinning ensures your code runs with tested library versions across development, testing, and production systems.
python3 -m pip install package_name==version
For example, install requests version 2.28.0 specifically:
python3 -m pip install requests==2.28.0
You can also specify version ranges using operators like >=, <=, or != to ensure compatibility while allowing minor updates:
python3 -m pip install "requests>=2.28.0,<3.0.0"
View Package Information
Display detailed information about installed packages, including version, location, dependencies, and metadata. This helps troubleshoot import errors, verify installations, and understand package relationships.
python3 -m pip show package_name
For example, view details about the requests library:
python3 -m pip show requests
The output displays comprehensive package metadata:
Name: requests Version: 2.31.0 Summary: Python HTTP for Humans. Home-page: https://requests.readthedocs.io Author: Kenneth Reitz Author-email: me@kennethreitz.org License: Apache 2.0 Location: /usr/local/lib/python3.12/dist-packages Requires: certifi, charset-normalizer, idna, urllib3 Required-by:
The Requires field lists dependencies this package needs, while Required-by shows which other packages depend on this one. The Location field indicates where Python loads the package from.
Check for Outdated Packages
Identify packages with available updates to keep your environment current with security patches, bug fixes, and new features. Regular audits help maintain secure, stable development environments.
python3 -m pip list --outdated
The output shows packages with newer versions available on PyPI:
Package Version Latest Type ---------- -------- -------- ----- requests 2.28.0 2.31.0 wheel setuptools 68.0.0 68.1.2 wheel
Review the list and upgrade packages as needed. Note the version gap between your installed version and the latest available release to assess whether updates are minor patches or major version changes.
Export and Install from Requirements Files
Create requirements files to document exact package versions for reproducible environments. This workflow ensures team members, CI/CD systems, and production deployments use identical library versions, preventing "works on my machine" issues.
Export currently installed packages to a requirements file:
python3 -m pip freeze > requirements.txt
The generated requirements.txt file lists all installed packages with exact version pins:
certifi==2024.2.2 charset-normalizer==3.3.2 idna==3.6 requests==2.31.0 urllib3==2.1.0
Install packages from a requirements file to recreate the environment:
python3 -m pip install -r requirements.txt
This command reads the file line by line, installing each package at the specified version. Use this when setting up new development machines, deploying to production servers, or initializing CI/CD environments to ensure consistent dependencies across all systems.
Troubleshooting Common Pip Issues
Externally-Managed-Environment Error
Ubuntu 22.04 LTS and newer versions implement PEP 668, which prevents system-wide pip installations to protect system packages managed by apt. When you attempt system-wide installations, you'll see this error:
error: externally-managed-environment
[X] This environment is externally managed
-> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.
If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.
For more information visit http://rptl.io/venv
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider.
This protection prevents pip from breaking system packages that Ubuntu's package manager controls. Instead of system-wide installation, use one of these approaches:
- Virtual environments (recommended): Install
python3-venvif needed, then create and activate isolated environments with Python virtual environments before runningpython3 -m pipinstalls - User-level installation: Add
--userflag to install packages in your home directory without affecting system packages - System packages via apt: Use
apt install python3-packagenamefor packages available through Ubuntu repositories
Permission Denied Errors
When you see permission errors during installation, it typically means pip is trying to write to system directories without sufficient privileges:
error: could not create '/usr/local/lib/python3.12/dist-packages/requests': Permission denied
Avoid using sudo python3 -m pip install for system-wide installations, as this can create permission conflicts and break system packages. Instead, use virtual environments or the --user flag to install packages in your home directory:
python3 -m pip install --user package_name
The --user flag installs packages to ~/.local/lib/python3.x/site-packages/, which your user account owns and can modify without elevated privileges.
Pip vs Pip3 Command Confusion
On Ubuntu systems with both Python 2 and Python 3 installed, pip traditionally referred to Python 2's package manager while pip3 managed Python 3 packages. Since Python 2 reached end-of-life in 2020 and Ubuntu removed it from default installations, modern Ubuntu systems typically make pip an alias to pip3, but using python3 -m pip remains the most explicit option.
Check which Python version your pip commands target:
pip --version
pip3 --version
python3 -m pip --version
All three commands should show Python 3.x in their output on current Ubuntu releases. Use python3 -m pip in scripts and documentation to avoid ambiguity, since pip may not exist on older distributions where only Python 2's pip was available.
Conclusion
With pip installed, you're ready to manage Python project dependencies through virtual environments, create reproducible requirements files, and maintain consistent library versions across development, testing, and production systems. Use pip within virtual environments to isolate dependencies and prevent version conflicts across projects.