pip vs apt: Installing Python Packages on Linux

By 

Updated on

5 min read

pip and apt package manager cards for Python packages on Linux

When you need a Python package on Ubuntu or Debian, the right install command is not always obvious. Some packages come from the distribution through apt, while others come from the Python Package Index through pip. They solve different problems, and mixing them in the wrong place can break system tools or leave projects with conflicting dependencies.

This guide explains when to use apt, when to use pip, and why Python virtual environments are the safest default for application dependencies.

Quick Decision Guide

SituationUse
Installing a system CLI tool maintained by Ubuntu or Debianapt
Installing Python libraries for a projectpip inside a virtual environment
Installing a package that is not in the apt repositoriespip inside a virtual environment
Installing a newer package version than the distribution providespip inside a virtual environment
Installing a Python CLI tool for your user accountpipx
Installing a dependency required by another apt packageapt

What Each Tool Does

apt installs packages from the distribution repositories. Python packages distributed through apt, such as python3-requests or python3-numpy, are tested against the Python version and system libraries in that Ubuntu or Debian release. They install system-wide and integrate with the distribution dependency resolver.

pip installs packages from the Python Package Index (PyPI). PyPI has more packages and newer versions than most distribution repositories. pip does not coordinate with apt, so it does not know which Python packages the operating system installed or depends on.

When to Use apt

Use apt for:

  • System tools that happen to be written in Python, such as cloud-init, ansible, or certbot. These packages are maintained as system components and expect the system Python.
  • Python packages required by other apt packages. If you install a Python-based CLI tool with apt and it lists python3-yaml as a dependency, apt handles that automatically.
  • Servers or containers where you want reproducible, distribution-tested versions and do not need the latest upstream release.

Install a Python package from the Ubuntu or Debian repositories with apt:

Terminal
sudo apt install python3-requests

The package name usually includes the python3- prefix. After installation, the package is available to the system Python interpreter and to other packages installed through apt.

When to Use pip

Use pip for:

  • Application dependencies inside a virtual environment.
  • Packages not available in the distribution repositories.
  • Packages where you need a specific or newer version than apt provides.

Install a package with pip inside a virtual environment:

Terminal
pip install requests

In this case the PyPI package name is requests, while the equivalent apt package is named python3-requests. That naming difference is normal.

Why sudo pip install Is Risky

Running sudo pip install can place packages on the system Python import path, alongside or ahead of packages managed by apt. This creates two problems:

  1. pip can replace, downgrade, or shadow packages that system tools depend on.
  2. On Ubuntu 23.04 and later, and Debian 12 and later, pip refuses to install into the system Python by default and raises an externally-managed-environment error. This restriction is intentional.

The error message looks like:

output
error: externally-managed-environment

× 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.

The right response to this error is to use a virtual environment, not to override the restriction with --break-system-packages.

Use Virtual Environments for Projects

A virtual environment is an isolated Python installation with its own site-packages. Installing packages inside it does not affect the system Python or any other environment:

Terminal
python3 -m venv myenv
source myenv/bin/activate
pip install requests flask gunicorn

On Ubuntu and Debian, install venv support first if the python3 -m venv command is not available:

Terminal
sudo apt install python3-venv

After activate, pip and python point to the virtual environment. Packages install there instead of system-wide. Deactivate with:

Terminal
deactivate

Virtual environments are the standard practice for any Python application or project. Create one per project, add it to .gitignore, and install dependencies from a requirements.txt:

Terminal
pip install -r requirements.txt

For a fuller walkthrough, see our guide to creating Python virtual environments .

Use pipx for User CLI Tools

If you want a Python command-line tool available in your user account, use pipx instead of installing it into the system Python or a shared user site-packages directory. pipx creates a small private virtual environment for each tool and exposes the command on your PATH.

Install pipx and then install a tool:

Terminal
sudo apt install pipx
pipx install httpie

This is a good fit for tools such as httpie, black, ruff, and poetry, where you want the command available everywhere but do not want the package mixed into a project environment.

You may still see older guides recommend pip install --user. That installs packages under ~/.local/, but all user-level packages share the same location and can conflict with each other. On externally managed Python installations, pip install --user can also be rejected for the same reason as a system install. Use pipx for standalone CLI tools and a virtual environment for project libraries.

Where to Install pip Itself

On Ubuntu and Debian, the distribution package for pip is python3-pip:

Terminal
sudo apt install python3-pip

This gives you the pip command, but it does not mean you should install packages into the system Python. Treat it as the installer you use inside virtual environments. For detailed setup instructions, see How to Install Python Pip on Ubuntu .

Conclusion

Use apt for Python packages that belong to the operating system, pip inside a virtual environment for project dependencies, and pipx for standalone Python CLI tools. If you see the externally-managed-environment error, create a virtual environment rather than overriding it with --break-system-packages. The pip cheatsheet lists the install, version-pin, and requirements commands for the pip side of this workflow.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page