sodcalculator

10 Tech How-To

How to Install and Use pip on Ubuntu

Guide 10-47737 · Updated · Step by step

This guide shows you how to install pip, the Python package manager, on Ubuntu and how to use it to install, upgrade, and remove Python packages. You’ll start with a system check, then install pip for Python 3 (and optionally Python 2), and finish with practical commands for everyday use.

What You’ll Need

  • An Ubuntu system (20.04, 22.04, 24.04, or similar) with internet access.
  • A user account with sudo privileges (for system-wide installs).
  • Python 3 installed (Ubuntu usually includes it). Check with python3 --version.

Step-by-Step

  1. Update your package list
    Open a terminal and run:
    sudo apt update
    This refreshes the list of available packages from Ubuntu’s repositories.
  2. Install pip for Python 3
    Run:
    sudo apt install python3-pip
    This installs pip and its dependencies. Confirm the installation when prompted.
  3. Verify the installation
    Check the version with:
    pip3 --version
    You should see output like pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12). The exact version depends on your Ubuntu release.
  4. Optional: Install pip for Python 2
    Python 2 is deprecated, but if you need it for legacy scripts, run:
    sudo apt install python-pip
    Note that Python 2 is no longer maintained; avoid using it for new projects.
  5. Use pip to install a package
    For example, to install the requests library:
    pip3 install requests
    This downloads and installs the package and its dependencies from the Python Package Index (PyPI).
  6. Use pip with sudo (if needed)
    If you get a permission error, prefix with sudo:
    sudo pip3 install requests
    However, prefer using a virtual environment to avoid system-wide changes (see Tips).
  7. List installed packages
    To see what you have:
    pip3 list
    This shows all installed packages and their versions.
  8. Upgrade a package
    To upgrade an existing package to the latest version:
    pip3 install --upgrade requests
  9. Uninstall a package
    To remove a package:
    pip3 uninstall requests
    Confirm when asked.
  10. Search for a package
    To find packages on PyPI:
    pip3 search keyword
    Note: the search command is deprecated in recent pip versions; use the PyPI website instead.

Common Mistakes

  • Using pip instead of pip3 – On Ubuntu, pip may point to Python 2 or may not exist. Always use pip3 for Python 3.
  • Installing globally without a virtual environment – This can lead to version conflicts between projects. Use a virtual environment for project-specific dependencies.
  • Forgetting to update pip itself – Old pip versions may have bugs. Upgrade pip with pip3 install --upgrade pip.
  • Ignoring the “externally managed environment” error – On newer Ubuntu versions, pip may refuse to install system-wide to avoid breaking system packages. This is normal; use a virtual environment.

Tips

  • Use virtual environments – Create one with python3 -m venv myenv, then activate it with source myenv/bin/activate. Inside, use pip (without sudo) to install packages.
  • Keep pip updated – Run pip3 install --upgrade pip periodically to get the latest features and security fixes.
  • Use pip3 freeze to generate a requirements file – This lists all installed packages and versions, useful for sharing or replicating environments.
  • Check package documentation – Some packages have system dependencies; read the docs if installation fails.

Troubleshooting

  • “pip3: command not found” – This means pip is not installed. Re-run sudo apt install python3-pip and check for errors.
  • “Permission denied” when installing – Use sudo pip3 install package or better, create a virtual environment.
  • “Externally managed environment” error – This happens on Ubuntu 23.04+ where pip is configured to avoid system conflicts. Create a virtual environment: python3 -m venv myenv, then activate it and use pip install.
  • Installation fails with “Could not find a version that satisfies the requirement” – Check the package name spelling, ensure you have internet, and try upgrading pip.
  • pip is slow or times out – Try using a different PyPI mirror or increase the timeout with pip3 install --timeout 60 package.
  • After upgrading pip, it still shows old version – This can happen if you have multiple Python installations. Use python3 -m pip --version to check the pip associated with your Python.

Frequently Asked Questions

Do I need to install pip separately if I have Python?

Yes, pip is not always included with Python on Ubuntu. Installing python3-pip ensures you have it.

What is the difference between pip and pip3?

On Ubuntu, pip may refer to Python 2, while pip3 always refers to Python 3. Use pip3 for Python 3 projects.

Can I use pip without sudo?

Yes, if you use a virtual environment or install with the --user flag: pip3 install --user package.

How do I upgrade pip itself?

Run pip3 install --upgrade pip. If you get a permission error, add --user or use sudo.

Why do I get a “externally managed environment” error?

Ubuntu 23.04 and later restrict system-wide pip installs to protect system packages. Use a virtual environment instead.

How do I uninstall a package installed with pip?

Use pip3 uninstall package-name. Confirm the prompt to remove it.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →