pip install Failed (tested)

pip install fails for a handful of distinct reasons that get lumped together under "pip install failed": a blocked system Python (PEP 668), a missing compiler or header for a package built from source, a bad TLS certificate, or a package index that isn't reachable. Each needs a different fix. We reproduced four of them on a real Debian 13 box and recorded the exact text pip prints.

1. "externally-managed-environment" (the most common one today)

On Debian, Ubuntu and other distros that follow PEP 668, the system Python ships a marker file (/usr/lib/python3.13/EXTERNALLY-MANAGED on our box) that blocks pip install outside a virtual environment. This is new compared to older tutorials, which is why it confuses people. Running pip install six with the system Python, no venv active, gave us exactly this:

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.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

    See /usr/share/doc/python3.13/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

Nothing was installed and the system Python was left untouched — pip aborts before touching any files. pip itself suggests three fixes, in order of how safe they are:

2. The fix, proven: same package, in a venv, works

To make the contrast concrete we installed the exact same package in a virtual environment right after the failure above:

$ python3 -m venv /tmp/demo-venv
$ /tmp/demo-venv/bin/pip install six
Collecting six
  Using cached six-1.17.0-py2.py3-none-any.whl.metadata (1.7 kB)
Using cached six-1.17.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six
Successfully installed six-1.17.0

No flags, no errors. A venv has its own site-packages directory, so it is never "externally managed" and pip will install into it freely. If you only ever see this error in system contexts (a bare pip install with no venv active, or inside a minimal Docker image using the distro's Python), creating a venv — or using a python base image that doesn't apply PEP 668, see below — fixes it for good.

3. "Failed to build wheel for X" / "Failed building wheel for X"

This means pip had to compile a package from source (no prebuilt wheel matched your OS/Python/CPU) and the build failed, usually because a system library or compiler header is missing. We reproduced it with psycopg2 (the PostgreSQL driver, whose source build needs pg_config from libpq, forced with pip install psycopg2==2.9.9 --no-binary :all:):

running egg_info
writing psycopg2.egg-info/PKG-INFO
writing dependency_links to psycopg2.egg-info/dependency_links.txt
writing top-level names to psycopg2.egg-info/top_level.txt

Error: pg_config executable not found.

pg_config is required to build psycopg2 from source.  Please add the directory
containing pg_config to the $PATH or specify the full executable path with the
option:

    python setup.py build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.

If you prefer to avoid building psycopg2 from source, please install the PyPI
'psycopg2-binary' package instead.

ERROR: Failed to build 'psycopg2' when getting requirements to build wheel

The fix depends on which library is missing, but the pattern is always the same:

4. SSL certificate errors (per pip/CPython docs, not measured)

We did not stand up a self-signed HTTPS index for this guide, but the three real-world causes are well documented and worth naming directly, since the generic pip._vendor.urllib3...SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] message doesn't tell you which one you have:

If the exact wording is "self-signed certificate in certificate chain," see our dedicated page: self-signed certificate in certificate chain. For the browser/TLS-handshake version of a broken certificate, see ERR_SSL_PROTOCOL_ERROR.

5. "Failed to establish a new connection" / index unreachable

This is pip saying it can't open a TCP connection to the package index at all — no proxy, no DNS, no listener, or a firewall drop. We pointed pip at a port nothing listens on (--index-url http://127.0.0.1:1/) to get the exact text:

Looking in indexes: http://127.0.0.1:1/
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError("HTTPConnection(host='127.0.0.1', port=1): Failed to establish a new connection: [Errno 111] Connection refused")': /somepkg-doesnotmatter/
WARNING: Retrying (Retry(total=3, ...)) after connection broken by '...Connection refused...'
WARNING: Retrying (Retry(total=2, ...)) after connection broken by '...Connection refused...'
WARNING: Retrying (Retry(total=1, ...)) after connection broken by '...Connection refused...'
WARNING: Retrying (Retry(total=0, ...)) after connection broken by '...Connection refused...'
ERROR: Could not find a version that satisfies the requirement somepkg-doesnotmatter (from versions: none)
ERROR: No matching distribution found for somepkg-doesnotmatter

pip retries 5 times with backoff before giving up, which is why this error is slow to appear. Common real causes and fixes:

If you see "Connection refused" from a specific service rather than pip's index, see ERR_CONNECTION_REFUSED / ECONNREFUSED, or the requests-library version in Python requests ConnectionError: Max retries exceeded.

6. "pip install fails in Docker build"

Inside a Docker build, every error above can show up, but two causes are specific to containers:

If the daemon itself is the problem rather than pip, that's a different error: see Docker: Cannot connect to the Docker daemon.

7. Quick diagnosis table

What you seeLikely causeFix
error: externally-managed-environmentPEP 668 system Python guardUse a venv, or pipx, or --break-system-packages as a last resort
Failed to build wheel for X / Failed building wheel for XMissing compiler or system library for a source buildRead the lines just above the error; install the missing dependency or switch to a binary wheel package
SSLError: CERTIFICATE_VERIFY_FAILEDMITM proxy, stale certifi, or clock skewPoint pip at the right CA bundle, upgrade pip/certifi, or fix the clock
Failed to establish a new connectionIndex unreachable: no network, wrong proxy, bad index URLTest with curl first; fix proxy/index-url/network

If the failure is actually about the package not existing once it does download, that's a different bug class — see ModuleNotFoundError vs ImportError for what happens after a successful install when the import still fails.

Got SyntaxError: invalid syntax instead? You typed pip at the Python >>> prompt: see pip install SyntaxError: invalid syntax.

Frequently asked questions

What does "externally-managed-environment" mean?

Your system's Python (Debian, Ubuntu, Fedora and others) is marked as managed by the OS package manager, per PEP 668. pip refuses to install into it directly to avoid breaking OS tools. We reproduced the exact error on Debian 13; the fix is to use a virtual environment, pipx, or pass --break-system-packages if you accept the risk.

Is --break-system-packages safe to use?

It works, but it can overwrite files that apt manages, including packages some OS tools depend on. We did not need it in our tests because a venv installed the same package cleanly. Use a venv first; reach for --break-system-packages only as a last resort, and reinstall the OS package with apt if something breaks afterward.

Why does the same pip install work in a venv but not outside one?

A virtual environment has its own site-packages directory that is never marked externally managed, so PEP 668's guard does not apply. We installed the identical package (six) both ways: it failed system-wide with the externally-managed error and succeeded immediately in a venv.

Why does pip say "Failed to build wheel" for some packages but not others?

Packages that ship a prebuilt wheel for your OS/Python/CPU combination never need to build. Packages without a matching wheel are compiled from source, which needs a compiler and often a system library (like libpq for psycopg2, which we used to reproduce the error). Installing the missing system dependency or switching to a binary-only package name usually fixes it.

pip install hangs then fails with a connection error — is that a pip bug?

No, it's pip retrying a genuinely unreachable host 5 times with backoff before giving up, which is why it feels slow. We saw this by pointing pip at a port with nothing listening (Errno 111, Connection refused). Test the network path directly with curl against the same index URL to confirm it's a connectivity or proxy-configuration problem, not pip itself.

Written with AI assistance. Measured on 2026-10-02 on Debian GNU/Linux 13 (trixie) with system Python 3.13.5 and pip 26.2.1 (run from a separate venv at /tmp/py415venv, since this Debian install ships no system-wide pip module). The SSL certificate section is from pip/CPython docs, not measured. PlainHeaders is our extension; curl, Python and pip are free.