Install pip
sudo apt update
sudo apt install python3-pip
pip3 --version
The version line also shows which Python pip belongs to and where it lives. python3 -m pip --version gives the same answer and is the more reliable form when several Python versions are installed. The same package exists on Debian 12 and 13. On AlmaLinux and Rocky Linux, install it with sudo dnf install python3-pip. If Python itself is missing or you need a newer one, see how to install Python on Ubuntu.
The Externally Managed Environment Error
On Ubuntu 24.04 and later, and on Debian 12 and later, this fails:
pip3 install requests
pip stops with an error that says the environment is externally managed. This is intended. The system Python belongs to apt: tools such as the package manager's helpers and cloud-init depend on the exact modules apt installed. If pip replaced one of them with another version, an upgrade or a system tool could break. The distribution marks its Python as managed, and pip refuses to write there.
The error message itself lists the ways forward, and they are the three options below.
Option 1: A Virtual Environment for an Application
A virtual environment is a folder with its own Python and its own packages, separate from the system:
sudo apt install python3-venv
mkdir -p ~/apps/myapp
cd ~/apps/myapp
python3 -m venv .venv
source .venv/bin/activate
pip install requests
pip install -r requirements.txt
While the environment is active, pip and python refer to the copies inside .venv, and no sudo is needed. deactivate leaves it. A service or cron job does not need to activate it: call /home/deploy/apps/myapp/.venv/bin/python directly, for example in the ExecStart line of a unit as shown in systemd unit files. Keep .venv out of Git with .gitignore.
Option 2: pipx for Command-Line Tools
For a program you run from the shell, such as a linter or an HTTP client, pipx installs each tool into its own environment and puts the command on your path:
sudo apt install pipx
pipx ensurepath
pipx install httpie
pipx upgrade-all
Open a new shell after ensurepath so the path change takes effect. pipx list shows what is installed and pipx uninstall removes a tool cleanly.
Option 3: apt Packages for System Modules
When a system script needs a common module, install the distribution's package:
apt search python3-yaml
sudo apt install python3-yaml
Most popular modules are packaged with a python3- prefix. These versions are older but receive security updates with the rest of the system; see sudo apt update.
Why Not sudo pip
- It overwrites files apt owns: the next upgrade or a system tool may fail in ways that are hard to trace.
- It runs package code as root: installing a package runs its build steps with full rights.
- It mixes applications: two applications that need different versions of one module cannot share one system Python.
pip offers an option that overrides the protection. It exists for special cases and containers, not as the fix for this error; a virtual environment does the same job without the risk.
Upgrade pip
Upgrade pip inside a virtual environment, where it affects only that environment:
python -m pip install --upgrade pip
The system pip is upgraded by apt with the rest of the system.
Common Problems
- pip: command not found: install
python3-pip, or usepip3; inside a virtual environment, activate it first. - ensurepip is not available: creating a virtual environment failed because
python3-venvis missing; the message names the package to install. - A pipx command is not found: run
pipx ensurepathand open a new shell.