1Installation 2============ 3 4You can install ``cryptography`` with ``pip``: 5 6.. code-block:: console 7 8 $ pip install cryptography 9 10Supported platforms 11------------------- 12 13Currently we test ``cryptography`` on Python 2.7, 3.4+, and 14PyPy 5.4+ on these operating systems. 15 16* x86-64 CentOS 7.x 17* macOS 10.12 Sierra, 10.11 El Capitan 18* x86-64 Ubuntu 14.04, 16.04, and rolling 19* x86-64 Debian Wheezy (7.x), Jessie (8.x), Stretch (9.x), and Sid (unstable) 20* x86-64 Alpine (latest) 21* 32-bit and 64-bit Python on 64-bit Windows Server 2012 22 23We test compiling with ``clang`` as well as ``gcc`` and use the following 24OpenSSL releases: 25 26* ``OpenSSL 1.0.1`` 27* ``OpenSSL 1.0.1e-fips`` (``RHEL/CentOS 7``) 28* ``OpenSSL 1.0.1f`` 29* ``OpenSSL 1.0.2-latest`` 30* ``OpenSSL 1.1.0-latest`` 31* ``OpenSSL 1.1.1-latest`` 32 33.. warning:: 34 Cryptography 2.4 has deprecated support for OpenSSL 1.0.1. 35 36 37Building cryptography on Windows 38-------------------------------- 39 40The wheel package on Windows is a statically linked build (as of 0.5) so all 41dependencies are included. To install ``cryptography``, you will typically 42just run 43 44.. code-block:: console 45 46 $ pip install cryptography 47 48If you prefer to compile it yourself you'll need to have OpenSSL installed. 49You can compile OpenSSL yourself as well or use the binaries we build for our 50release infrastructure (`openssl-release`_). Be sure to download the proper 51version for your architecture and Python (2010 works for Python 2.7, 3.3, 52and 3.4 while 2015 is required for 3.5 and above). Wherever you place your copy 53of OpenSSL you'll need to set the ``LIB`` and ``INCLUDE`` environment variables 54to include the proper locations. For example: 55 56.. code-block:: console 57 58 C:\> \path\to\vcvarsall.bat x86_amd64 59 C:\> set LIB=C:\OpenSSL-win64\lib;%LIB% 60 C:\> set INCLUDE=C:\OpenSSL-win64\include;%INCLUDE% 61 C:\> pip install cryptography 62 63As of OpenSSL 1.1.0 the library names have changed from ``libeay32`` and 64``ssleay32`` to ``libcrypto`` and ``libssl`` (matching their names on all other 65platforms). ``cryptography`` links against the new 1.1.0 names by default. If 66you need to compile ``cryptography`` against an older version then you **must** 67set ``CRYPTOGRAPHY_WINDOWS_LINK_LEGACY_OPENSSL`` or else installation will fail. 68 69If you need to rebuild ``cryptography`` for any reason be sure to clear the 70local `wheel cache`_. 71 72.. _build-on-linux: 73 74Building cryptography on Linux 75------------------------------ 76 77``cryptography`` ships a ``manylinux1`` wheel (as of 2.0) so all dependencies 78are included. For users on pip 8.1 or above running on a ``manylinux1`` 79compatible distribution (almost everything except Alpine) all you should 80need to do is: 81 82.. code-block:: console 83 84 $ pip install cryptography 85 86If you are on Alpine or just want to compile it yourself then 87``cryptography`` requires a compiler, headers for Python (if you're not 88using ``pypy``), and headers for the OpenSSL and ``libffi`` libraries 89available on your system. 90 91Alpine 92~~~~~~ 93 94Replace ``python3-dev`` with ``python-dev`` if you're using Python 2. 95 96.. code-block:: console 97 98 $ sudo apk add gcc musl-dev python3-dev libffi-dev openssl-dev 99 100If you get an error with ``openssl-dev`` you may have to use ``libressl-dev``. 101 102Debian/Ubuntu 103~~~~~~~~~~~~~ 104 105Replace ``python3-dev`` with ``python-dev`` if you're using Python 2. 106 107.. code-block:: console 108 109 $ sudo apt-get install build-essential libssl-dev libffi-dev python3-dev 110 111RHEL/CentOS 112~~~~~~~~~~~ 113 114.. code-block:: console 115 116 $ sudo yum install redhat-rpm-config gcc libffi-devel python-devel \ 117 openssl-devel 118 119 120Building 121~~~~~~~~ 122 123You should now be able to build and install cryptography. To avoid getting 124the pre-built wheel on ``manylinux1`` distributions you'll need to use 125``--no-binary``. 126 127.. code-block:: console 128 129 $ pip install cryptography --no-binary cryptography 130 131 132Using your own OpenSSL on Linux 133~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 134 135Python links to OpenSSL for its own purposes and this can sometimes cause 136problems when you wish to use a different version of OpenSSL with cryptography. 137If you want to use cryptography with your own build of OpenSSL you will need to 138make sure that the build is configured correctly so that your version of 139OpenSSL doesn't conflict with Python's. 140 141The options you need to add allow the linker to identify every symbol correctly 142even when multiple versions of the library are linked into the same program. If 143you are using your distribution's source packages these will probably be 144patched in for you already, otherwise you'll need to use options something like 145this when configuring OpenSSL: 146 147.. code-block:: console 148 149 $ ./config -Wl,--version-script=openssl.ld -Wl,-Bsymbolic-functions -fPIC shared 150 151You'll also need to generate your own ``openssl.ld`` file. For example:: 152 153 OPENSSL_1.1.0E_CUSTOM { 154 global: 155 *; 156 }; 157 158You should replace the version string on the first line as appropriate for your 159build. 160 161Static Wheels 162~~~~~~~~~~~~~ 163 164Cryptography ships statically-linked wheels for macOS, Windows, and Linux (via 165``manylinux1``). This allows compatible environments to use the most recent 166OpenSSL, regardless of what is shipped by default on those platforms. Some 167Linux distributions (most notably Alpine) are not ``manylinux1`` compatible so 168we cannot distribute wheels for them. 169 170However, you can build your own statically-linked wheels that will work on your 171own systems. This will allow you to continue to use relatively old Linux 172distributions (such as LTS releases), while making sure you have the most 173recent OpenSSL available to your Python programs. 174 175To do so, you should find yourself a machine that is as similar as possible to 176your target environment (e.g. your production environment): for example, spin 177up a new cloud server running your target Linux distribution. On this machine, 178install the Cryptography dependencies as mentioned in :ref:`build-on-linux`. 179Please also make sure you have `virtualenv`_ installed: this should be 180available from your system package manager. 181 182Then, paste the following into a shell script. You'll need to populate the 183``OPENSSL_VERSION`` variable. To do that, visit `openssl.org`_ and find the 184latest non-FIPS release version number, then set the string appropriately. For 185example, for OpenSSL 1.0.2k, use ``OPENSSL_VERSION="1.0.2k"``. 186 187When this shell script is complete, you'll find a collection of wheel files in 188a directory called ``wheelhouse``. These wheels can be installed by a 189sufficiently-recent version of ``pip``. The Cryptography wheel in this 190directory contains a statically-linked OpenSSL binding, which ensures that you 191have access to the most-recent OpenSSL releases without corrupting your system 192dependencies. 193 194.. code-block:: console 195 196 set -e 197 198 OPENSSL_VERSION="VERSIONGOESHERE" 199 CWD=$(pwd) 200 201 virtualenv env 202 . env/bin/activate 203 pip install -U setuptools 204 pip install -U wheel pip 205 curl -O https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz 206 tar xvf openssl-${OPENSSL_VERSION}.tar.gz 207 cd openssl-${OPENSSL_VERSION} 208 ./config no-shared no-ssl2 no-ssl3 -fPIC --prefix=${CWD}/openssl 209 make && make install 210 cd .. 211 CFLAGS="-I${CWD}/openssl/include" LDFLAGS="-L${CWD}/openssl/lib" pip wheel --no-binary :all: cryptography 212 213Building cryptography on macOS 214------------------------------ 215 216.. note:: 217 218 If installation gives a ``fatal error: 'openssl/aes.h' file not found`` 219 see the :doc:`FAQ </faq>` for information about how to fix this issue. 220 221The wheel package on macOS is a statically linked build (as of 1.0.1) so for 222users with pip 8 or above you only need one step: 223 224.. code-block:: console 225 226 $ pip install cryptography 227 228If you want to build cryptography yourself or are on an older macOS version, 229cryptography requires the presence of a C compiler, development headers, and 230the proper libraries. On macOS much of this is provided by Apple's Xcode 231development tools. To install the Xcode command line tools (on macOS 10.9+) 232open a terminal window and run: 233 234.. code-block:: console 235 236 $ xcode-select --install 237 238This will install a compiler (clang) along with (most of) the required 239development headers. 240 241You'll also need OpenSSL, which you can obtain from `Homebrew`_ or `MacPorts`_. 242Cryptography does **not** support Apple's deprecated OpenSSL distribution. 243 244To build cryptography and dynamically link it: 245 246`Homebrew`_ 247 248.. code-block:: console 249 250 $ brew install openssl@1.1 251 $ env LDFLAGS="-L$(brew --prefix openssl@1.1)/lib" CFLAGS="-I$(brew --prefix openssl@1.1)/include" pip install cryptography 252 253`MacPorts`_: 254 255.. code-block:: console 256 257 $ sudo port install openssl 258 $ env LDFLAGS="-L/opt/local/lib" CFLAGS="-I/opt/local/include" pip install cryptography 259 260You can also build cryptography statically: 261 262`Homebrew`_ 263 264.. code-block:: console 265 266 $ brew install openssl@1.1 267 $ env CRYPTOGRAPHY_SUPPRESS_LINK_FLAGS=1 LDFLAGS="$(brew --prefix openssl@1.1)/lib/libssl.a $(brew --prefix openssl@1.1)/lib/libcrypto.a" CFLAGS="-I$(brew --prefix openssl@1.1)/include" pip install cryptography 268 269`MacPorts`_: 270 271.. code-block:: console 272 273 $ sudo port install openssl 274 $ env CRYPTOGRAPHY_SUPPRESS_LINK_FLAGS=1 LDFLAGS="/opt/local/lib/libssl.a /opt/local/lib/libcrypto.a" CFLAGS="-I/opt/local/include" pip install cryptography 275 276If you need to rebuild ``cryptography`` for any reason be sure to clear the 277local `wheel cache`_. 278 279 280.. _`Homebrew`: https://brew.sh 281.. _`MacPorts`: https://www.macports.org 282.. _`openssl-release`: https://ci.cryptography.io/job/cryptography-support-jobs/job/openssl-release-1.1/ 283.. _virtualenv: https://virtualenv.pypa.io/en/latest/ 284.. _openssl.org: https://www.openssl.org/source/ 285.. _`wheel cache`: https://pip.pypa.io/en/stable/reference/pip_install/#caching 286