pip says externally-managed-environment: why it happens and the right fix

You typed pip install on a VPS and got error: externally-managed-environment. Nothing is broken and you are not missing permissions: it is a safeguard. On newer systems (for example recent Ubuntu and Debian releases), the system’s Python belongs to the system’s package manager, and pip refuses to install libraries on top of it, so as not to break the system’s own tools that depend on it. The short answer: use a virtual environment for each project. That is what the error is asking for.

The ways out, best to worst

You want to install… Do this Why
A project’s libraries (your application) One virtual environment per project: python3 -m venv .venv, then .venv/bin/pip install .... It isolates the project from the system. It is the rule for any application.
A command-line tool written in Python pipx (sudo apt install pipx on Ubuntu and Debian), which creates a separate environment for each tool. The tool is available in the terminal without touching the system.
A library the system already offers The distribution package, for example sudo apt install python3-requests. It comes with the system’s security updates.
Anything, by force pip install --break-system-packages ... It switches the safeguard off. Last resort: it can break system tools.

The virtual environment, step by step

1 Install what is needed to create environments: sudo apt update
sudo apt install python3-venv python3-pip
If python3 -m venv complains “ensurepip is not available”, this package is the missing piece.
2 Create the environment in the project folder: cd ~/my-app
python3 -m venv .venv
3 Activate it and install:source .venv/bin/activate
pip install -r requirements.txt
With the environment active, the prompt shows (.venv) and pip is the environment’s own. Confirm with pip --version.
4 Use the environment’s Python in services. systemd, PM2 and Docker activate nothing on their own: point straight at /path/to/app/.venv/bin/python (or at the gunicorn in there). See gunicorn and systemd.
5 Save the versions that work: pip freeze > requirements.txt, with the environment active.
Do not delete the system’s EXTERNALLY-MANAGED file and do not sudo pip install to “silence” the error. That is exactly what the safeguard wants to prevent: a system update can bring back an old version of a library, or a system tool stops starting. If an application of yours broke after an install done that way, the remedy is to recreate the virtual environment.
Did not see this error and expected to? It depends on the operating system you chose when ordering: older releases, such as Ubuntu 22.04 LTS, do not have the safeguard. Even so, the virtual environment is still good practice: one project, one environment. On shared hosting, “Setup Python App” already creates the environment for you: Python in cPanel.

Need a server where you pick the system and install your own tools? Have a look at our VPS plans.

See the VPS servers

SEE ALSO

Python in cPanel: virtual environments and pip

Running Flask or Django with gunicorn and systemd on a VPS

Which operating system to choose for your VPS or server

How far our support goes: what we handle and what is yours

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $6.60/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?