Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

pip install cryptography tries to compile and fails in a Docker build

Errorerror: Can not find Rust compiler

Short 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.

  • Applies to Python 3.12, Alpine 3.22, Debian 12, Docker Engine 28
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 cryptography
  2. 2Match what you see, then jump to its fix

    1. 1error: Can not find Rust compiler, on an image that has a wheel publishedpip is too old to use the available wheelFix
    2. 2error: Can not find Rust compiler, or a missing header, on an alpine baseAlpine image without the build dependenciesFix
    3. 3error: Can not find Rust compiler, or openssl/aes.h not found, on a debian or ubuntu baseDebian or Ubuntu image without the build dependenciesFix

What does the error look like?

  • error: Can not find Rust compiler
  • fatal 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 --version

2Fix

Upgrade pip before installing, in the same RUN layer.

RUN pip install -U pip && pip install cryptography

3Verify

The install finishes without a compiler step, and the module imports.

python -c "import cryptography; print(cryptography.__version__)"

Confirmed by cryptography.io

Did this fix it?

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 cargo

2Fix

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 pkgconfig

3Verify

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

Did this fix it?

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-release

2Fix

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-config

3Verify

Install cryptography again.

pip install cryptography

Confirmed by cryptography.io

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.