error installing psycopg2, library not found for -lssl

PythonPostgresqlPsycopg2

Python Problem Overview


I run

sudo pip install psycopg2

and I get a bunch of output that looks like:

cc -DNDEBUG -g -fwrapv -Os .....
.....
cc -DNDEBUG -g -fwrapv -Os .....
.....

And at the end it says:

ld: library not found for -lssl

clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: command 'cc' failed with exit status 1

----------------------------------------
Cleaning up...
Command /usr/bin/python -c "import setuptools, tokenize;__file__='/private/var/folders/bz/pvj1g9xj16d10pjjgbrfl3fw0000gn/T/pip_build_root/psycopg2/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/bz/pvj1g9xj16d10pjjgbrfl3fw0000gn/T/pip-uE3thn-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /private/var/folders/bz/pvj1g9xj16d10pjjgbrfl3fw0000gn/T/pip_build_root/psycopg2
Storing debug log for failure in /Users/Tyler/Library/Logs/pip.log

Running easy_install or doing it from source both give me the same error at the end (the part about library not found for -lssl).


Running brew install (or upgrade) openssl yields the below

$ brew upgrade openssl
Error: openssl-1.0.1h already installed

Can anyone help me out?

Python Solutions


Solution 1 - Python

For anyone looking for a solution for this on macOS Sierra 10.12 (or later, most likely): I fixed this by installing the command line tools:

xcode-select --install

After that, pip install psycopg2 should work.

If it doesn't, you could also try to link against brew's openssl:

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2

with openssl installed via brew. Note that the brew link openssl --force does not work anymore:

$ brew link openssl --force                                                                                 17.5s
Warning: Refusing to link: openssl
Linking keg-only openssl means you may end up linking against the insecure,
deprecated system OpenSSL while using the headers from Homebrew's openssl.
Instead, pass the full include/library paths to your compiler e.g.:
  -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib

As @macho points out below if this still does not work, you might need to use the --no-cache option of pip, e.g.

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip --no-cache install psycopg2

Remember to adjust these paths accordingly should you for instance build on ARM/Apple M1 Macs (as homebrew is installed at /opt/homebrew/); command as follows:

env LDFLAGS="-I/opt/homebrew/opt/openssl/include -L/opt/homebrew/opt/openssl/lib" pip --no-cache install psycopg2

Solution 2 - Python

I had OpenSSL installed from brew (brew install openssl)

The following worked for me:

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
pip install psycopg2

Solution 3 - Python

Using brew link openssl is dangerous as it might mess up your system by symlinking Homebrew's OpenSSL headers while the actual libraries will remain the system-supplied ones, causing all kinds of issues. Homebrew actually warns you against this if you try (and other answers suggest linking won't solve the problem anymore anyway):

$ brew link openssl
Warning: Refusing to link: openssl
Linking keg-only openssl means you may end up linking against the insecure,
deprecated system OpenSSL while using the headers from Homebrew's openssl.
Instead, pass the full include/library paths to your compiler e.g.:
  -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib

Following this advice here's the pip command you need to use:

$ pip install -r requirements.txt --global-option=build_ext --global-option="-I/usr/local/opt/openssl/include" --global-option="-L/usr/local/opt/openssl/lib"

For pipenv, I am not aware of any command-line attributes you can pass to it, however you can export the aforementioned paths as environment variables prior to running pipenv install:

$ export LDFLAGS="-L/usr/local/opt/openssl/lib" export CPPFLAGS="-I/usr/local/opt/openssl/include"
$ pipenv install

Solution 4 - Python

With MacOS Catalina 10.15.4, the following was the only command that worked for me:

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2

Solution 5 - Python

What worked for me was the hint provided in the command to link openssl,

$ brew link openssl
Warning: Refusing to link macOS-provided software: openssl
If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

$ export LDFLAGS="-L/usr/local/opt/openssl/lib"
$ export CPPFLAGS="-I/usr/local/opt/openssl/include"

$ pip install psycopg2
Collecting psycopg2
  Using cached https://files.pythonhosted.org/packages/23/7e/93c325482c328619870b6cd09370f6dbe1148283daca65115cd63642e60f/psycopg2-2.8.2.tar.gz
Installing collected packages: psycopg2
  Running setup.py install for psycopg2 ... done
Successfully installed psycopg2-2.8.2

Solution 6 - Python

On mojave I added these to the .bash_profile

export PATH="/usr/local/opt/openssl/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/curl/lib -L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/curl/include -I/user/local/opt/openssl/include"

was then able to install psycopg 2.8.3 in a python 3.7.4 virtualenv.

This after reinstalling xcode and the command line tools.

All the answers above helped!

Solution 7 - Python

Solution for MacOS 12 Monterey - M1 Chipset:

xcode-select --install
brew install openssl
echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc
echo 'export LIBRARY_PATH=$LIBRARY_PATH:/opt/homebrew/opt/openssl@3/lib/' >> ~/.zshrc

Solution 8 - Python

Solution for MacOS BigSur (May 2021)

I was having a similar issue. But just befor kicking my pc of the desk I found a solution, that worked for me on MacOS BigSur:

My OpenSSL installation was corrupted.

brew reinstall openssl

After the reinstallation is completed it will spit out 3 commands you need to execute:

The commands will look like this:

DONT JUST COPY

echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc

export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib"
export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"

This worked for me! There is no guarantee that this will work for you too.

brew reinstall might not be the best solution, but it worked and provided the necessary commands.

Solution 9 - Python

This's the problem of new macOs version, where pip cannot install cryptography. What fixed my problem is to provide the env to the install command:

brew install openssl
env LDFLAGS="-L$(brew --prefix openssl)/lib" CFLAGS="-I$(brew --prefix openssl)/include" <YOUR COMMAND HERE>

You can replace <YOUR COMMAND HERE> with pip install cryptography, or pip install <SOMETHING THAT REQUIRES cryptography> for example.

Credit to this article: Fixing macOS Sierra fatal error: 'openssl/opensslv.h' or 'openssl/aes.h' file not found

Solution 10 - Python

Using Fish, the following two commands solved this issue for me after installing OpenSSL with Homebrew.

set -gx LDFLAGS "-L/usr/local/opt/openssl/lib"
set -gx CPPFLAGS "-I/usr/local/opt/openssl/include"

Use brew info openssl to get up-to-date info.

Solution 11 - Python

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

worked for me

Solution 12 - Python

If the export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/ command doesn't change anything for you, then you may also need to specify openssl version in this path like

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl@1.1/lib/

Note the @1.1 after openssl.

Solution 13 - Python

If you:

  • Are trying to install psycopg2 inside of a Python venv AND
  • Are trying to install psycopg2 on Apple Silicon (new ARM CPUs)

Then, as IamAshay directed in their answer, we need to export some variables to point to our OpenSSL installation.

The install directory on Apple Silicon is apparently different ofrom Apple Intel machines. Regardless, to find where OpenSSL is installed (via homebrew) on your system, you can run

brew --prefix openssl

In my case, we have /opt/homebrew/opt/[email protected]. So this means that the variables we need to export would be

export LDFLAGS="-L/opt/homebrew/opt/openssl/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl/include"

Then we can run

pip install psycopg2

Solution 14 - Python

Recently had this problem in High Sierra, having just installed Python 3.7 in a virtualenv.

The solution is to use a later version of psycopg2. Version 2.7.7 worked, where 2.7.1 did not.

Solution 15 - Python

Instead of installing psycopg2, install psycopg2-binary, from the same authors:

pip install psycopg2-binary

This is what the documentation says about this PyPI package:

> You can [...] obtain a stand-alone package, not requiring a compiler or external libraries, by installing the psycopg2-binary package from PyPI: > > $ pip install psycopg2-binary > > The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources.

Solution 16 - Python

I installed OpenSSL using MacPorts therefore directories are not like those of Brew.

sudo port install openssl

I found the directories by doing:

port contents openssl | grep lib
port contents openssl | grep include

Then I exported the variables:

export LDFLAGS="-L/opt/local/lib"
export CPPFLAGS="-I/opt/local/include/openssl"

You might also have to:

xcode-select --install

Solution 17 - Python

If using homebrew, it may not be the best idea to force link openssl. As some above answers outlined, the following worked for me inside a venv:

export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib"
export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"
pip install psycopg2

Solution 18 - Python

I was having this issue on Mojave. Mojave does not create a /usr/include directory, which psycopg2 needs to install. This was not obvious. I found the solution here: https://stackoverflow.com/questions/34617452/how-to-update-xcode-from-command-line, which references: https://forums.developer.apple.com/thread/104296

Solution 19 - Python

I had this same error and got it to resolve after I pip installed cython

Solution 20 - Python

Running PyCharm from conda environment, solved my issue using:

--> conda install psycopg2
The following packages will be UPDATED: ...

...
Proceed ([y]/n)? 
--> y
--> pip3 install psycopg2
Installing collected packages: psycopg2
Running setup.py install for psycopg2 ... done
Successfully installed psycopg2-2.8.4

'''

Solution 21 - Python

I know you are asking for development environment but if you are deploying on server say, Heroku. Just add below line in the requirements.txt of your project.

django-heroku==0.3.1

As this package itself will install the required packages like psycopg2 on server deployment. So let heroku server handle this.

Solution 22 - Python

In my case, openssl is located inside homebrew directory and suggested answers don't work.

Bad:

LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib"

Good:

LDFLAGS="-I/opt/homebrew/Cellar/[email protected]/1.1.1i/include -L/opt/homebrew/Cellar/[email protected]/1.1.1i/lib" pip install psycopg2

Tip:

You can easily find homebrew's openssl directory using brew --prefix openssl. Make sure to include version folder (1.1.1i in my case).

Solution 23 - Python

I tried most of the other answers here without success. The only thing that worked for me was updating from psycopg2 from 2.8 to 2.9.2 (running MacOS 12 with Apple M1)

Solution 24 - Python

Tried everything listed here, then switched shell from zsh to bash, then it worked.

Solution 25 - Python

I've managed to fix it by using:

brew unlink openssl && brew link openssl --force

I am not sure how this differs from the brew uninstall/upgrades that I did on OpenSSL in prior attempts I've made. My assumption is that these operations left some of the "faulty" shared libraries which were preventing this from working. Note that this also fixed issues with installing python cryptography module.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontscizzleView Question on Stackoverflow
Solution 1 - PythonsasView Answer on Stackoverflow
Solution 2 - PythonIamAshayView Answer on Stackoverflow
Solution 3 - PythonAndré BorieView Answer on Stackoverflow
Solution 4 - PythonChris FordView Answer on Stackoverflow
Solution 5 - PythonIsaac PhilipView Answer on Stackoverflow
Solution 6 - PythonpmneveView Answer on Stackoverflow
Solution 7 - PythonKhoi NgoView Answer on Stackoverflow
Solution 8 - PythonMichaelKleineView Answer on Stackoverflow
Solution 9 - PythonEnvilView Answer on Stackoverflow
Solution 10 - PythonYngve HøisethView Answer on Stackoverflow
Solution 11 - PythonbeyondfloatingpointView Answer on Stackoverflow
Solution 12 - PythonQbackView Answer on Stackoverflow
Solution 13 - PythonMcFizzView Answer on Stackoverflow
Solution 14 - Pythondixon1eView Answer on Stackoverflow
Solution 15 - PythonFlimmView Answer on Stackoverflow
Solution 16 - PythonDjAlanView Answer on Stackoverflow
Solution 17 - PythoncpkView Answer on Stackoverflow
Solution 18 - PythonProfXView Answer on Stackoverflow
Solution 19 - PythonNili WaypaView Answer on Stackoverflow
Solution 20 - Pythoncarl du PlessisView Answer on Stackoverflow
Solution 21 - PythonChristopher NolanView Answer on Stackoverflow
Solution 22 - PythonstilManiacView Answer on Stackoverflow
Solution 23 - PythonAndre OBrienView Answer on Stackoverflow
Solution 24 - PythonJoost HolslagView Answer on Stackoverflow
Solution 25 - PythonecordoView Answer on Stackoverflow