keepcalculator

06 Numbers and Powers

Pip vs apt: Which Python Package Manager?

Card No. 06·6098 · Updated · Standard formula, shown below

Average pace 5.00 min/km
Average speed
12.00 km/h
Result
Finish time
50 min
You entered
Distance
10 km
You entered
You enter
Finish time, Distance
You get
Average pace, Average speed
Rounding
2 dp

Pip and apt are both package managers, but they serve different layers of your system: pip installs Python libraries from the Python Package Index (PyPI), while apt installs system software from your Linux distribution’s repositories. The short answer: use pip for Python-only dependencies inside a virtual environment, and use apt for system-level tools or libraries that other programs rely on. Mixing them carelessly can break your OS, so this article explains exactly when each one is the right choice.

What pip actually does

Pip is the standard package installer for Python. It downloads packages from PyPI, which hosts over 500,000 projects ranging from small utilities to large frameworks like Django, Flask, and NumPy. Pip resolves dependencies—meaning it reads each package’s metadata to figure out which other packages it needs—and installs them into a specific Python environment.

When you run pip install requests, pip fetches the package, checks its dependencies, and places files into the site-packages folder of your Python installation. It does not touch anything outside that Python environment. This isolation is why pip is safe to use for Python projects, but it also means pip-installed packages are invisible to system tools that expect apt-managed files.

  • Scope: Python libraries only
  • Source: PyPI (pypi.org)
  • Default location: site-packages directory
  • Dependency handling: automatic, but only within Python

What apt actually does

Apt (Advanced Package Tool) is the package manager for Debian-based Linux distributions like Ubuntu, Debian, and Linux Mint. It installs software from the distribution’s repositories—collections of pre-compiled binaries and metadata that the distro maintainers have tested for compatibility with that specific OS version.

When you run apt install python3-requests, apt installs the system package that provides the requests library. That package is built from a specific version of the source code, patched by Debian maintainers, and placed in system directories like /usr/lib/python3/dist-packages. Apt also manages shared libraries, executables, and configuration files that may be used by many different programs.

The key difference is that apt packages are system-wide and version-pinned to your distribution. They are designed to work together as a coherent whole, which is why apt will refuse to install a package if it would break another installed package’s dependencies.

Why mixing them causes breakage

The classic problem: you install a Python package with pip, and later apt tries to upgrade a system library that the pip package depends on. The pip package was compiled against an older version, so it breaks. Or the reverse—pip installs a newer version of a library that shadows the apt-installed one, and a system tool that relied on the old version crashes.

This is especially dangerous on Ubuntu because the system Python is used by critical tools like apt itself, the software updater, and even the desktop environment. If you run sudo pip install and overwrite a system Python library, you can render your system unbootable or make it impossible to install further updates.

The safe rule is: never use pip with sudo. Always use pip inside a virtual environment (venv) or a user-level install (pip install --user). This isolates your Python projects from the system Python, so pip can’t break anything outside its own sandbox.

When to use apt

Use apt when you need a system-level tool or a Python library that other programs depend on. For example, if you’re installing a desktop application like GIMP or a development tool like Git, apt is the right choice because those programs integrate with the rest of the system.

For Python libraries specifically, apt is appropriate when:

  • The library is a dependency for a system package (e.g., python3-requests is needed by some system scripts)
  • You want automatic security updates through the distro’s update mechanism
  • You don’t need the latest version—stability matters more than freshness

Ubuntu’s repositories often lag behind PyPI by months or even years. For example, as of this writing, Ubuntu 22.04 ships Python 3.10 and a version of NumPy that may be several releases old. If you’re doing data science or web development, that lag is usually unacceptable.

When to use pip

Use pip when you need a Python package that is not in your distro’s repos, or when you need a specific version that is newer than what apt provides. Pip is also the only way to get packages that are not packaged by your distribution at all—many niche libraries are only on PyPI.

Pip is the right choice for:

  • Python projects with their own dependencies (use a virtual environment)
  • Getting the latest release of a library
  • Installing packages that are not in apt at all
  • Development environments where you need to switch between versions

The standard practice is to create a virtual environment per project:

  1. python3 -m venv myproject
  2. source myproject/bin/activate
  3. pip install flask

This keeps all your Python dependencies inside the myproject folder, completely separate from the system Python. You can delete the folder and everything is gone—no system pollution.

How to check what’s installed

To see which packages pip manages, run pip list. To see which apt packages are installed, run apt list --installed. But note that a Python library can be installed by both—for example, python3-requests from apt and requests from pip are different packages that provide the same import name.

If you have both, the one that appears first in Python’s module search path wins. Usually that’s the pip-installed one if it’s in a virtual environment, or the user-level one if you used --user. This can lead to confusing bugs where you update a pip package but the system still uses the apt version, or vice versa.

To see which version Python actually imports, run python -c "import requests; print(requests.__file__)". This shows the exact path, which tells you whether it’s from apt (/usr/lib/python3/dist-packages) or pip (~/.local/lib/python3.x/site-packages or a venv path).

Common problems and fixes

Problem: “Externally managed environment” error on newer Ubuntu

Ubuntu 23.04 and later, and Debian 12, mark the system Python as externally managed. This means pip refuses to install system-wide by default, showing a message that says to use a virtual environment. This is intentional—it prevents exactly the breakage described above. The fix is to create a venv or use pip install --user if you’re sure you want a user-level install.

Problem: pip installs but the command isn’t found

This happens when pip installs a script into a directory that’s not on your PATH. For example, pip install --user on Ubuntu puts scripts in ~/.local/bin, which may not be in your PATH by default. Add it with export PATH="$HOME/.local/bin:$PATH" in your .bashrc.

Problem: apt and pip versions conflict

If you have both, and Python imports the wrong one, you can either uninstall the pip version (if you don’t need it) or uninstall the apt version (if you don’t need it). Never try to upgrade the apt version with pip—that will break the system package.

Alternatives and best practices

For modern Python development, the recommended tool is not pip directly, but pipenv, poetry, or simply venv with a requirements.txt file. These tools manage virtual environments and dependencies together, making it easier to reproduce environments across machines.

For system-level package management, apt remains the standard on Debian-based systems. Other distributions have their own equivalents: dnf on Fedora, pacman on Arch, zypper on openSUSE. The same principle applies—use the system package manager for system software, and use pip only inside isolated Python environments.

A good rule of thumb: if you’re writing a Python program, use pip in a venv. If you’re installing a tool that happens to be written in Python but you just want to use it as a command, check if apt has it first—it usually does, and it will be better integrated.

Frequently Asked Questions

Can I use pip to install a package that apt also provides?

Yes, but you should avoid it unless you’re in a virtual environment. If you install the same library with both, you risk version conflicts that are hard to debug. Prefer one or the other, and if you must use both, always use a venv for pip.

Is it safe to run pip install with sudo?

No. Running pip with sudo installs packages system-wide, which can overwrite files managed by apt and break your OS. Always use a virtual environment or --user instead.

Why does apt have older versions of Python packages?

Distributions prioritize stability over freshness. They test packages against the specific versions of libraries and system components that ship with the release, so they pin versions to those that are known to work together. PyPI is updated continuously, so it always has the latest.

How do I uninstall a package installed by pip?

Use pip uninstall package-name. If you installed it with --user, it will uninstall from your user directory. If you’re in a venv, it uninstalls from that venv. You cannot uninstall an apt package with pip, and vice versa.

What is the difference between pip3 and pip?

On most modern systems, pip is an alias for pip3 (Python 3). On older systems, pip might refer to Python 2. Always use pip3 or python3 -m pip to be explicit and avoid installing into the wrong Python version.

Can I use apt to install a package that is only on PyPI?

No. Apt only installs packages that are in your distribution’s repositories. If a Python package is not packaged by your distro, you must use pip (or another Python-specific tool) to install it.

Related

Formula used
in t | Finish time | min | 50 | 0
in d | Distance | km | 10 | 0 | 100000
out pace | Average pace | min/km | t / d | 2
out speed | Average speed | km/h | d / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →