You cannot pip install packages inside an Ubuntu Docker container
pip: command not foundShort answer
If pip is missing, install the python3-pip package with apt, in the same RUN as apt-get update. If pip exists but refuses, the distribution marked its Python as externally managed (PEP 668): use a virtual environment, or in a single-purpose image pass --break-system-packages.
Fix it fast
1Find out which cause you have
Run the base image on its own and ask which of python3 and pip exist. The result splits the causes.
docker run --rm ubuntu:24.04 sh -c 'command -v python3; command -v pip3; echo done'2Match what you see, then jump to its fix
- 1
pip: command not found, or pip3 not found, in a fresh ubuntu imagepip is not installed in the imageFix - 2
apt cannot find the package in a build that has apt-get update on its own line, or a cached older layerapt-get update ran in a different layer from apt-get installFix - 3
pip exists, but pip install stops with externally-managed-environmentUbuntu's Python is externally managed (PEP 668)Fix
- 1
What does the error look like?
pip: command not founderror: externally-managed-environment
Cause 1 of 3
pip is not installed in the image
1Check
Ubuntu publishes pip as the python3-pip package. The first step shows whether the image has it; check whether your Dockerfile installs it.
grep -n 'python3-pip\|apt-get' Dockerfile2Fix
Install it with apt, and call pip through python3 so it matches the interpreter.
RUN apt-get update && apt-get install -y --no-install-recommends python3 python3-pip \
&& rm -rf /var/lib/apt/lists/*3Verify
pip answers inside the image.
docker run --rm my-image python3 -m pip --versionConfirmed by packages.ubuntu.com
Cause 2 of 3
apt-get update ran in a different layer from apt-get install
1Check
Docker's best-practice guide says to always combine apt-get update with apt-get install in one RUN, because a lone update line is cached and later installs then fail.
grep -n -B1 -A1 'apt-get' Dockerfile2Fix
Put both in one RUN instruction, and rebuild without the cache once to drop the stale layer.
RUN apt-get update && apt-get install -y --no-install-recommends python3-pip3Verify
The build passes the apt step.
docker build --no-cache -t my-image .Confirmed by docs.docker.com
Cause 3 of 3
Ubuntu's Python is externally managed (PEP 668)
1Check
PEP 668 lets a distribution mark its Python as managed by the package manager, and installers must then refuse system-wide installs unless overridden. The marker is a file named EXTERNALLY-MANAGED in the Python library folder.
docker run --rm ubuntu:24.04 sh -c 'ls /usr/lib/python3*/EXTERNALLY-MANAGED'2Fix
Best is a virtual environment: the PEP says even in containers this is a good idea, because OS commands written in Python can break. For a single-purpose image, the pip flag --break-system-packages is the documented override, shown here.
# single-purpose image only
RUN python3 -m pip install --break-system-packages -r requirements.txt3Verify
pip lists the packages.
docker run --rm my-image python3 -m pip listConfirmed by peps.python.org
How do I stop it coming back?
- Always write apt-get update && apt-get install in one RUN.
- Choose a base image that already has Python and pip when you do not need Ubuntu itself.
- Pin dependency versions in requirements.txt so image builds repeat.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Servers
pip refuses to install: this environment is externally managed
To install Python packages system-wide, try apt install python3-xyz
Your Linux distribution marked its system Python as managed by apt, so pip will not write into it (PEP 668).
4 causes, each with a fixServers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
Docker build fails with apk: not found in a Python or Ubuntu image
/bin/sh: 1: apk: not found
apk is the package manager of Alpine Linux only.
2 causes, each with a fixServers
pip install psycopg2 fails while building the Docker image
libpq-fe.h: No such file or directory
The plain psycopg2 package compiles a C extension, so the image needs a compiler, the Python headers, the libpq headers and pg_config.
3 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
sam local fails: Running AWS SAM projects locally requires Docker
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
sam local start-api and sam local invoke run your function inside a Docker container, so SAM stops if it cannot talk to Docker.
3 causes, each with a fix