Cannot "pip install cryptography" in Docker Alpine Linux 3.3 with OpenSSL 1.0.2g and Python 2.7 Cannot "pip install cryptography" in Docker Alpine Linux 3.3 with OpenSSL 1.0.2g and Python 2.7 python python

Cannot "pip install cryptography" in Docker Alpine Linux 3.3 with OpenSSL 1.0.2g and Python 2.7


For those who are still experiencing problems installing cryptography==2.1.4 in Alpine 3.7 like this:

writing manifest file 'src/cryptography.egg-info/SOURCES.txt'running build_extgenerating cffi module 'build/temp.linux-x86_64-2.7/_padding.c'creating build/temp.linux-x86_64-2.7generating cffi module 'build/temp.linux-x86_64-2.7/_constant_time.c'generating cffi module 'build/temp.linux-x86_64-2.7/_openssl.c'building '_openssl' extensioncreating build/temp.linux-x86_64-2.7/buildcreating build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7gcc -fno-strict-aliasing -Os -fomit-frame-pointer -g -DNDEBUG -Os -fomit-frame-pointer -g -DTHREAD_STACK_SIZE=0x100000 -fPIC -I/usr/include/python2.7 -c build/temp.linux-x86_64-2.7/_openssl.c -o build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7/_openssl.o -Wconversion -Wno-error=sign-conversionbuild/temp.linux-x86_64-2.7/_openssl.c:493:30: fatal error: openssl/opensslv.h: No such file or directory #include <openssl/opensslv.h>                              ^compilation terminated.error: command 'gcc' failed with exit status 1

Solution

Install these dependencies in the Alpine container:

$ apk add --no-cache libressl-dev musl-dev libffi-dev

To install these dependencies using a Dockerfile:

RUN apk add --no-cache \        libressl-dev \        musl-dev \        libffi-dev && \    pip install --no-cache-dir cryptography==2.1.4 && \    apk del \        libressl-dev \        musl-dev \        libffi-dev

Reference

Installation instructions for cryptography on Alpine can be found here:

Here is the relevant portion:

Building cryptography on Linux

[skipping over the part for non-Alpine Linux]

$ pip install cryptography

If you are on Alpine or just want to compile it yourself then cryptography requires a compiler, headers for Python (if you're not using pypy), and headers for the OpenSSL and libffi libraries available on your system.

Alpine

Replace python3-dev with python-dev if you're using Python 2.

$ sudo apk add gcc musl-dev python3-dev libffi-dev openssl-dev

If you get an error with openssl-dev you may have to use libressl-dev.


If it fails because of Rust version, then following is recommended in cryptography's docs:

The Rust available by default in Alpine < 3.12 is older than the minimum supported version. See the Rust installation instructions for information about installing a newer Rust.
$ sudo apk add gcc musl-dev python3-dev libffi-dev openssl-dev cargo

in my case, python3.8-alpine, adding cargo resolved.


Add this before install:

RUN apk -U upgrade

RUN apk add --no-cache libffi-dev openssl-dev