pip install cryptography tries to compile and fails in a Docker build
error: Can not find Rust compilerShort answer
cryptography ships prebuilt wheels. If pip is too old for the image's platform, or no wheel matches, pip compiles it and needs a Rust compiler plus C headers. First upgrade pip so it picks a wheel. If it must build, install the toolchain packages the project lists for your base image, and use Alpine 3.21 or newer.
Fix it fast
1Find out which cause you have
Upgrade pip inside the same image and install again. A wheel needs no compiler, so a successful install here means the rest of this page is not needed.
pip install -U pip pip install cryptography2Match what you see, then jump to its fix
- 1
error: Can not find Rust compiler, on an image that has a wheel publishedpip is too old to use the available wheelFix - 2
error: Can not find Rust compiler, or a missing header, on an alpine baseAlpine image without the build dependenciesFix - 3
error: Can not find Rust compiler, or openssl/aes.h not found, on a debian or ubuntu baseDebian or Ubuntu image without the build dependenciesFix
- 1
What does the error look like?
error: Can not find Rust compilerfatal error: 'openssl/aes.h' file not found
Cause 1 of 3
pip is too old to use the available wheel
1Check
Linux manylinux2014 wheels need pip 19.3 or newer, and Alpine musllinux wheels need pip 21.2.4 or newer. An older pip ignores them and builds from source.
pip --version2Fix
Upgrade pip before installing, in the same RUN layer.
RUN pip install -U pip && pip install cryptography3Verify
The install finishes without a compiler step, and the module imports.
python -c "import cryptography; print(cryptography.__version__)"Confirmed by cryptography.io
Cause 2 of 3
Alpine image without the build dependencies
1Check
If no wheel fits (an unusual CPU, or --no-binary), pip compiles. That needs a C compiler, the Python and OpenSSL headers, libffi and Rust with cargo. On Alpine before 3.21 the packaged Rust is too old.
cat /etc/alpine-release
apk info -e cargo2Fix
Use Alpine 3.21 or newer and add the build packages the project documents. Remove them again in the same layer if image size matters.
RUN apk add --no-cache gcc musl-dev python3-dev libffi-dev openssl-dev cargo pkgconfig3Verify
Build the image again and import the module.
docker build -t crypto-test . && docker run --rm crypto-test python -c "import cryptography"Confirmed by cryptography.io
Cause 3 of 3
Debian or Ubuntu image without the build dependencies
1Check
The same compile needs build-essential, the SSL, libffi and Python headers, cargo and pkg-config. Debian releases before 13 ship a Rust that is too old for current cryptography.
cat /etc/os-release2Fix
Install the packages the project lists, or install a current Rust with rustup (the minimum supported Rust is 1.83.0).
RUN apt-get update && apt-get install -y build-essential libssl-dev libffi-dev python3-dev cargo pkg-config3Verify
Install cryptography again.
pip install cryptographyConfirmed by cryptography.io
How do I stop it coming back?
- Pin cryptography in requirements.txt and rebuild on a base image whose platform has a wheel.
- Keep pip current in the first layer of every Python image.
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 tries to compile tokenizers and fails with a Rust or cargo error
Could not build wheels for tokenizers, which is required to install pyproject.toml-based projects
tokenizers is written in Rust.
3 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
Docker cannot find manage.py or Permission denied on '/manage.py'
PermissionError: [Errno 13] Permission denied: '/manage.py'
The container is looking for manage.py in a directory where the file is not.
3 causes, each with a fixServers
Compose cannot start the service: exec "python manage.py runserver" failed
starting container process caused: exec: "python manage.py runserver
The container runtime was asked to run a program literally named 'python manage.py runserver', including the spaces.
2 causes, each with a fixServers
docker logs prints nothing for a Flask or Python container
the output appears only after the app exits, or in large delayed bursts
docker logs only shows what the process writes to standard output and standard error.
2 causes, each with a fix