GrepBoard/Manuals
605 manuals ·

Fix pip error: externally-managed-environment on Ubuntu 24.04

·Tested on Ubuntu 24.04 · Python 3.12.3 · pip 24.0 · pipx 1.4.3 (Docker) · 2026-10·Test scope ↓·6 steps

pip install fails with error: externally-managed-environment on Ubuntu 24.04 because apt owns the system Python. Use a venv, pipx, or apt instead.

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.

Ubuntu 24.04 blocks pip from installing into the system Python because apt owns it. Put libraries in a virtual environment (venv), command-line tools in pipx, and distro-packaged libraries through apt, and the error goes away.

[1] Confirm what is blocking you. pip raises this error when it finds an EXTERNALLY-MANAGED file in the standard library directory (PEP 668 specification). The spec tells installers to refuse when that file exists and they are not running in a virtual environment; the message body is the file's Error= text.

console
$ ls -l /usr/lib/python3.12/EXTERNALLY-MANAGED-rw-r--r-- 1 root root 645 Aug 31 10:18 /usr/lib/python3.12/EXTERNALLY-MANAGED

Adding --user (install into your home directory) fails the same way. pip skips this check only when you point it at another directory with --target, --prefix, or --root (per a comment in the pip 24.0 source), but you then have to manage that directory yourself via PYTHONPATH and similar, so it is not a recommended fix. The usual route is one of venv, pipx, or apt below.

console
$ pip install --user requestserror: externally-managed-environment

[2] Create a venv for libraries your own code imports. The first attempt may fail like this, because Ubuntu ships ensurepip in the separate python3-venv package.

console
$ python3 -m venv ~/.venvs/appThe virtual environment was not created successfully because ensurepip is notavailable.  On Debian/Ubuntu systems, you need to install the python3-venvpackage using the following command.     apt install python3.12-venv

The failed run leaves a half-built ~/.venvs/app directory, and the last line of the message says "After installing the python3-venv package, recreate your virtual environment." Before recreating it, check that the directory is only the skeleton left by this failure: bin holds just the python links and no pip.

console
$ ls -A ~/.venvs/app ~/.venvs/app/bin/home/user/.venvs/app:binincludeliblib64pyvenv.cfg /home/user/.venvs/app/bin:pythonpython3python3.12
console
$ sudo apt install python3-venv$ mv ~/.venvs/app ~/.venvs/app.broken && python3 -m venv ~/.venvs/app && ~/.venvs/app/bin/pip install -q requests && ~/.venvs/app/bin/python -c "import requests; print(requests.__version__)"2.34.2

Once the new environment works, remove the moved leftover.

console
$ rm -r ~/.venvs/app.broken && ls -A ~/.venvsapp

[3] Activate the venv and check that pip now points inside it, not at /usr/lib/python3/dist-packages.

console
$ source ~/.venvs/app/bin/activate && which python pip && pip --version/home/user/.venvs/app/bin/python/home/user/.venvs/app/bin/pippip 24.0 from /home/user/.venvs/app/lib/python3.12/site-packages/pip (python 3.12)

Outside the venv, the system python3 still raises ModuleNotFoundError: No module named 'requests'. Run scripts with the venv interpreter (~/.venvs/app/bin/python app.py) or activate first. In systemd units and crontab lines, put the venv's python path in ExecStart= or the command.

[4] Install command-line tools such as ruff, black, or httpie with pipx. It creates one venv per tool and exposes only the executables in ~/.local/bin.

console
$ sudo apt install pipx$ pipx install ruffcreating virtual environment...creating shared libraries...upgrading shared libraries...installing ruff...⚠️  Note: '/home/user/.local/bin' is not on your PATH environment variable.

If you see that warning, run pipx ensurepath once and open a new terminal. Ubuntu's default ~/.profile adds ~/.local/bin to PATH when the directory exists, so a fresh login shell may already report it as present.

console
$ pipx ensurepath/home/user/.local/bin is already in PATH.$ ~/.local/bin/ruff --versionruff 0.17.2

[5] Use apt when the library is needed system-wide and the distro packages it, usually as python3-<name>. The version lags PyPI, but Ubuntu ships the security fixes.

console
$ apt-cache policy python3-requestspython3-requests:  Installed: (none)  Candidate: 2.31.0+dfsg-1ubuntu1.2

At the same moment the venv received 2.34.2 from PyPI. If you need the latest release, use the venv.

[6] Avoid --break-system-packages. The error's note: line mentions it, together with "at the risk of breaking your Python installation or OS". If pip overwrites an apt-installed python3-* package with a different version, system tools that depend on it can break. The spec also says this override should be off by default and clearly marked as risky. Even in throwaway container images, a venv keeps your packages from colliding with apt's.

What you installWhere it goes
Libraries for your projectpython3 -m venv + the venv's pip
Command-line tools (ruff, etc.)pipx install
Distro-packaged libraries, system-wideapt install python3-<name>

If apt stops on a lock error, see apt dpkg lock-frontend; if it cannot find the package name, see E: Unable to locate package.

#pip#python#venv#pipx#pep-668