Coding Agents & Terminal · recorded 2026-08-29 · applies to 2 tool(s) ✓ Verified

Problem: pip install fails with "externally-managed-environment" on new Ubuntu/WSL —— Trove problem library

Verified 2026-08-29pip reports externally-managed-environment (PEP 668) — create a venv and install inside it: python3 -m venv .venv && source .venv/bin/activate.

Quick fix

# One-liner: create and activate a venv; pip install stops erroring afterwards
python3 -m venv .venv && source .venv/bin/activate && pip install -r requirements.txt

What you will run into

On a fresh Ubuntu (or WSL), pip install of any package fails immediately with error: externally-managed-environment — even pip install numpy won't install.

You think Python 3.14 is too new, or pip is broken, or you even consider downgrading Python — none of that is it: this is the distro's default security policy, and pip is simply refusing, per the rules, to install into the system environment.

Why this happens

Ubuntu 23.04+ enables PEP 668 by default: the system Python is marked "externally managed", and pip refuses to install third-party packages directly into the system environment, preventing third-party packages from overwriting apt-managed system packages and breaking system Python.

  • The error is unrelated to the Python version; it triggers for any package, at any time — this is designed behavior.
  • The right fix isn't bypassing the check, it's using a virtual environment: venv is an isolated environment where pip can install freely without polluting the system.

PEP 668 is an "environment ownership" declaration: the system Python's site-packages are owned by apt/distro package managers, and pip writing directly would desync from apt's records (apt does not know what pip installed and may overwrite it on upgrade;

pip does not know what apt changed).

Distros declare ownership via a marker file (EXTERNALLY-MANAGED), and pip refuses when it detects it.

Deterministic takeaway: externally-managed-environment has nothing to do with the package name or version — it is about the target environment;

the fix is changing the environment, not bypassing it (--break-system-packages is an explicit "I know I will dirty the system" statement, fine only for containers/disposable environments); venv never triggers this because it has its own site-packages and is not marked externally-managed;

the fixed path is: python3 -m venv .venv first, then pip — do not fight the system Python.

Step by step

1. Create the virtual environment — Required

python3 -m venv .venv

If it reports ensurepip is unavailable, install it first: sudo apt install python3-venv (common on slim images).

2. Activate — Required

source .venv/bin/activate

After activation a (.venv) appears before the prompt, and pip now points into the venv (measured: pip install six inside the venv succeeds directly).

3. Install normally — Required

pip install -r requirements.txt

From now on, source .venv/bin/activate before working in the project and you'll never hit PEP 668 again.

4. Use pipx for CLI tools (optional) — Advanced

Prefer pipx install <app> for system-level CLI apps — pipx automatically creates an isolated venv per app, doesn't touch system Python, and doesn't hit PEP 668.

How to confirm it works

source .venv/bin/activate && pip install six && python -c "import six; print(six.__version__)"

No more externally-managed-environment and a successful import means it's fixed (measured: install succeeded inside the venv; the dry-run --break-system-packages path also verified, outputting Would install six-1.17.0).

Other environments

EnvironmentNotes
Ubuntu 23.04+ / Debian 12+PEP 668 on by default; venv/pipx/--break-system-packages required
Old Ubuntu (22.04 and below)Not enforced, but venv is still recommended
WSL UbuntuBehaves the same as native Ubuntu
Slim imagesOften missing python3-venv; apt install python3-venv first

Symptom

On Ubuntu/Debian (including a fresh WSL Ubuntu 26.04), pip install <pkg> fails immediately:

error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install ...

Installing any package fails, regardless of project dependencies (even pip install numpy fails); it is easy to misread as a Python 3.14 version issue and to consider downgrading Python.

Cause

Debian-family systems such as Ubuntu 26.04 enable PEP 668 by default: the system Python is marked "externally managed" and pip refuses to install third-party packages into it, preventing third-party packages from overwriting apt-managed system packages and breaking system Python. This is not a Python 3.14 bug but the distro default policy (enabled by default since Ubuntu 23.04).

Solution

  1. Long-term (recommended): create a virtual environment, then normal pip install works again:
   python3 -m venv .venv
   source .venv/bin/activate
   pip install -r requirements.txt

Note: if it reports a missing ensurepip, run sudo apt install python3-venv first (common on slim images).

  1. Temporary bypass (one-off): pip install <pkg> --break-system-packages — skips the PEP 668 check; may overwrite system packages and conflict with apt, not recommended long-term.
  2. System-level / CLI tools: prefer apt install python3-<pkg>; for command-line apps use pipx install <app> (pipx manages its own isolated venv).
Verification Evidence
  • Local repro (2026-08-29, WSL Ubuntu, Python 3.14.4 / pip 25.1.1, matching the article's environment):
  • System pip pip3 install six → reproduced error: externally-managed-environment with the --break-system-packages override hint;
  • pip3 install --dry-run --break-system-packages six → resolved normally, output Would install six-1.17.0 (bypass works; dry-run installs nothing);
  • python3 -m venv /tmp/venv-test then pip install six inside the venv → success (long-term fix works); temp dir removed afterwards.

Evidence details kept locally (2026-08-29)

Applicable tools (2)

This problem is linked from the tool pages below — reachable from either side.

pythonpip

Bundled solutions (combos)

This problem is part of the following verified bundles.

Source & evidence

Recorded Verified ✓ VerifiedApplies to 2 tool(s)
Entries contain locally measured or reviewable-source solutions; unmeasured items are explicitly labeled.

Source: https://www.cnblogs.com/weixizhou/p/20271782

← Coding Agents & Terminal domain | Problem Catalog Experience | Facing another issue? Go to the Helpdesk