72

I get an error when pip builds wheels for the cryptography package.

Error:

Error

LINK : fatal error LNK1181: cannot open input file 'libssl.lib'
  error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x86\\link.exe' failed with exit status 1181
  ----------------------------------------
  ERROR: Failed building wheel for cryptography
  Running setup.py clean for cryptography
Failed to build cryptography
ERROR: Could not build wheels for cryptography which use PEP 517 and cannot be installed directly

I have already installed OpenSSL and set the environment variables as suggested in this post yet the problem persists. My setup details:

  • System - Windows 10
  • Python - 3.8
  • Pip - 19.3.1
Chirag Bhansali
  • 1,900
  • 1
  • 15
  • 22
  • Building cryptography on Windows is a very involved process and it should not be required unless you're attempting to install an older cryptography version. cryptography ships Python 3.8 windows wheels for cryptography 2.8+. What happens if you run `pip install cryptography` directly? – Paul Kehrer Dec 25 '19 at 02:43
  • Installing it directly works fine. However, when I install it as a part of the requirements.txt, this error shows. How should I disable the option to build wheels? What will be the consequences if I do so? – Chirag Bhansali Dec 25 '19 at 02:49
  • 2
    in my case it was necessary to update pip to the latest version – dreo Dec 25 '19 at 21:43
  • @dreo I have already updated Pip but it still builds wheels for the packages – Chirag Bhansali Dec 26 '19 at 08:00
  • 1
    Does the requirements.txt have cryptography pinned to a version < 2.8? cryptography did not ship wheels for Python 3.8 on Windows before that release (because Python 3.8 wasn't out). On other platforms it can use a special kind of wheel that works across multiple Pythons, but on Windows each Python version requires a new wheel. – Paul Kehrer Dec 30 '19 at 22:43
  • @PaulKehrer The specified version for cryptography is 2.7 – Chirag Bhansali Jan 01 '20 at 17:22
  • You will need to update that to 2.8 to resolve the issue. – Paul Kehrer Jan 08 '20 at 05:07
  • I had the same issue. Just updated pip and it installed without an error. – Ulvi Jan 25 '21 at 17:54
  • I just restarted my computer then the error was fixed. If you still have the error, update your computer then restart you computer then try to build the docker images. – Super Kai - Kazuya Ito Jun 01 '21 at 13:01

15 Answers15

50

In my case (windows 10 + conda) updating pip fixed the problem:

python -m pip install --upgrade pip
vvvvv
  • 25,404
  • 19
  • 49
  • 81
José
  • 1,774
  • 1
  • 17
  • 21
  • docs say to upgrade pip and you'll use the distributed binary https://cryptography.io/en/latest/installation.html#rust – Harry Moreno Mar 31 '21 at 17:40
47

TLDR;

Try using cryptography==3.1.1

Details:

This happened on Python 3.9.0 on Windows 10 PC. I had the following in requirements.txt

cryptography==2.8

I removed the version and kept only cryptography in requirements.txt file like below

cryptography

Saved the requirements.txt and then I ran

pip install -r requirements.txt

It installed successfully. Then I freeze the requirements.txt by running the following command

pip freeze > requirements.txt

Then the requirements.txt got updated with cryptography==3.1.1

navule
  • 3,212
  • 2
  • 36
  • 54
  • I am getting below error: ERROR: Failed building wheel for cryptography Failed to build cryptography ERROR: Could not build wheels for cryptography which use PEP 517 and cannot be installed directly – shary.sharath Mar 10 '21 at 15:37
  • have you followed the approach mentioned? – navule Mar 10 '21 at 17:52
  • Yes @navule, I followed all the steps. – shary.sharath Mar 11 '21 at 07:28
  • Try upgrading pip and re-run the steps mentioned above. To upgrade pip run `python -m pip isntall -U pip`. Let me know if that works. – navule Mar 12 '21 at 08:25
  • I was using snowflake-connector-python 2.2.8 version and it caused the issue. After removing this version number, application ran successfully. I'm not sure how will it behave once published to cloud. Hoping for the best – shary.sharath Mar 12 '21 at 13:45
  • 1
    They started using Rust in v3.4, so to avoid Rust the latest version you can use is `cryptography==3.3.2` – Igor Jerosimić Jul 12 '21 at 06:48
  • 1
    @Igor Jerosimić thx man, this is the only answer that makes sense and acutally helps out. Navule just changes versions randomly w/o having any kind of expanation. – binaryguy Jul 23 '21 at 11:11
  • You can also try setting environment variable `CRYPTOGRAPHY_DONT_BUILD_RUST=1` – SWdV Sep 25 '21 at 23:22
45

Setting cryptography to version 2.8 in requirements.txt fixed the issue.

Abhi
  • 4,872
  • 1
  • 22
  • 20
33

Had also this issue. If you're using alpine, make sure that all dependencies for cryptography are installed. In my case the cargo package was missing and causing the issue.

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

https://cryptography.io/en/latest/installation.html#building-cryptography-on-linux

vvvvv
  • 25,404
  • 19
  • 49
  • 81
lineconnect
  • 352
  • 3
  • 6
16

I faced this issue on macOS Monterey (version 12.2.1) Apple M1 Pro. I followed Cryptography installation documentation and it helped me resolve the issue.

brew install openssl@1.1 rust

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" pip3 install cryptography

Tushar Nitave
  • 519
  • 4
  • 13
14

Ran into this issue, and the solution is actually in the message when pip attempts to install openssl before cryptography:

  generating cffi module 'build/temp.linux-x86_64-3.7/_openssl.c'
  running build_rust
  
      =============================DEBUG ASSISTANCE=============================
      If you are seeing a compilation error please try the following steps to
      successfully install cryptography:
      1) Upgrade to the latest pip and try again. This will fix errors for most
         users. See: https://pip.pypa.io/en/stable/installing/#upgrading-pip
      2) Read https://cryptography.io/en/latest/installation.html for specific
         instructions for your platform.
      3) Check our frequently asked questions for more information:
         https://cryptography.io/en/latest/faq.html
      4) Ensure you have a recent Rust toolchain installed:
         https://cryptography.io/en/latest/installation.html#rust
      5) If you are experiencing issues with Rust for *this release only* you may
         set the environment variable `CRYPTOGRAPHY_DONT_BUILD_RUST=1`.
      =============================DEBUG ASSISTANCE=============================

Simply running the pip update command appeared to work for me:

pip install -U pip

Which updated pip from version 18.0 to version 21.0.1

Andrew Bowman
  • 798
  • 9
  • 21
8

You can use the lastest version with

python3 -m pip install --no-use-pep517 cryptography

worked for me with ubuntu:18.04 on arm32v7 (instead as suggested by the error message install the full rust compiler and build-essentials, or upgrading pip (what had no effect on ubuntu:18.04 on my armbian))

FoxRomeo
  • 139
  • 1
  • 3
7
pip install --upgrade pip

Try to upgrade pip of your environment working fine for me.

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
Amit Dagar
  • 167
  • 2
  • 8
2

I got this error trying to install Scrapy with Python 3.8.1 on Windows 10, but its solved installing the last version of pip (19.3.1 in my case) and all works using pip in this way:

python -m pip install scrapy --user
Camilo Caquimbo
  • 133
  • 1
  • 6
  • Thanks. This also worked for me. My system is Windows 10, Python 3.8.2, Pycharm, and netmiko didn't install from Pycharm's requirement.txt file. I got inside the venv, and run pip install cryptography , which didn't work either. Then I tried python -m pip install cryptography , and it installed without any problem. Afterwards, I got back to Pycharm requirements file, and clicked on netmiko entry to get it installed, and it worked smoothly. – freeAR Apr 23 '20 at 00:55
2

I have faced same issue and tried to install openssl with mentioned step but still was not able to proceed for windows 10. Later I upgraded pip to latest version and tried again and it worked without any issue.

I would recommend to upgrade pip to latest version and give a try before proceeding for installing openssl

  • This worked for me, but I also had to upgrade the pip in my tox directory to enable tox to set up my dependencies. For reference, I'm using OSX Big Sur, python 3.8, and upgraded both pips from 20.3.3 to 21.0. – Blue Jan 26 '21 at 07:05
2

You can try to install cryptography==3.1.1 package instead of cryptography 35 version.

pip install cryptography==3.1.1

If still you are facing challenges in cryptography package integration.

You can resolve that by the following steps.

python -m pip install --upgrade pip

sudo pip install -U pip setuptools
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
1

I had the same issue. pip3 version was 19.* after upgrade it works

sudo -H pip3 install --upgrade pip
0

I had this problem and i just installed rust and problem fixed for me You can install end version of cryptography after installing rust programming languege(dont try pip install rust, its not python package) https://www.rust-lang.org/tools/install

Masoud A.
  • 9
  • 5
0

Just updating pip


python -m pip install --upgrade pip

Andresse Njeungoue
  • 608
  • 1
  • 5
  • 19
0

I did not install the latest version of Scrapy, then it worked for me. Instead of installing version 2.6.3, I installed 2.6.2:

pip install Scrapy==2.6.2

I use PyCharm 2022.2.3 (Professional Edition) on Windows.