error installing psycopg2, library not found for -lssl
Asked Answered
P

25

205

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?

Phoney answered 9/10, 2014 at 21:17 Comment(1)
Possible duplicate of python install lxml on mac os 10.10.1Nunn
P
391

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
Pickford answered 31/8, 2016 at 8:39 Comment(18)
Actually, xcode-select --install may be enough, this should use the system provided ssl. I'll update my comment above.Pickford
xcode-select install is good enough for the system python. But it does not work for virtual environments.Breach
After just installing sierra and creating a new virtualenv for a new project. I found that using the xcode command wasn't enough and needed to use the additional command to link to brew's openssl packagesSamekh
Just got macOS Sierra 10.12, just did pip install psycopg2 and just received that same error message: 'ld: library not found for -lssl'. xcode-select --install fixed the problem.Suffruticose
xcode-select --install fixed the issue on Sierra for me, also for virtual environmentsLowboy
xcode-select --install did not work for me. I already had the comand line tools installed. However, env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2 did still work.Profusive
Just the xcode-select --install fix worked for me, even using a virtualenv.Contestation
After running xcode-select --install, I had to reinstall Python before the build would work without LDFLAGS.Amenity
didn't work for me, moving to venv instead of virtualenv did work somehowBankrupt
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 psycopg2Pitchdark
Also worth noting I needed to update my pinned version of psycopg2 to 2.7.5.Tracheostomy
when I try this command "brew link openssl --force",the shell prompt 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"' >> ~/.bash_profile 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" then I set up the exports above ,and run "pip install *** " and it worksScurry
The --no-cache option helped me - @Pitchdark thanks! I also did an update to Catalina, installed newest Xcode and command-line tools.Sidereal
For some reason my brew installed openssl had "@1.1" appended to the path. I had to use this to get it to work: env LDFLAGS="-I/usr/local/opt/[email protected]/include -L/usr/local/opt/[email protected]/lib" pip install psycopg2Gauvin
xcode-select wasn't enough. I have to re-link with brew command. Thanks!Endopeptidase
This also works for macports, just tweak the paths LDFLAGS="-I/opt/local/include/openssl -L/opt/local/lib"Vanadous
If you are trying to install on Apple Silicon, see this answer https://mcmap.net/q/126709/-error-installing-psycopg2-library-not-found-for-lsslMothy
env LDFLAGS="-I/opt/homebrew/opt/openssl/include -L/opt/homebrew/opt/openssl/lib" pip --no-cache install psycopg2 is the one that did it for me, thank you!Tadio
N
133

If you have OpenSSL installed from brew (brew install openssl)

The following works:

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
pip install psycopg2
Nunn answered 20/5, 2019 at 21:26 Comment(7)
I already had the xcode tools installed, but this worked for me also!Investigation
This worked for me on Catalina using a virtual env. Other answers did not work for me as xcode-select was already installed, openssl was a decent version, and setting LD_LIBRARY_FLAG had no effect.Tremulous
If you are on Apple Silicon and have openssl installed via homebrew your paths will be different. You can find where openssl is installed with brew --prefix openssl.Mothy
I had a large pip install process that had psycopg2 in it. This was the only answer that was able to make that install process work.Teniers
This worked for me, but I'm on an M1 Mac and so had to set the env variables accordingly using openssl installed via home-brew. ``` export LDFLAGS="-L/opt/homebrew/Cellar/[email protected]/1.1.1k/lib/" export CPPFLAGS="-I/opt/homebrew/Cellar/[email protected]/1.1.1k/include" ```Earflap
Extending @McFizz's tip, here's the copypasta for Intel or Apple Silicon: export LDFLAGS="-L$(brew --prefix openssl)/lib" export CPPFLAGS="-I$(brew --prefix openssl)/include"Miru
@isaacparrot's copy paste worked for meBristow
A
38

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
Amatory answered 9/4, 2017 at 17:10 Comment(0)
P
30

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
Pewter answered 11/5, 2020 at 2:17 Comment(0)
S
20

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
Sigmatism answered 7/11, 2021 at 10:4 Comment(3)
Thanks for sharing, this works for me! MBP 16" M1Max.Jumpoff
MBP 16" M1Pro worked like a charm. Thanks man.Catchascatchcan
I have spent so much time trying to solve an issue with psycopg installation, this was the only solution that finally helped! Thank you so much!Waist
E
15

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.

Earthshaker answered 12/5, 2021 at 19:17 Comment(0)
D
12

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
Dynah answered 15/5, 2019 at 9:59 Comment(1)
most people probably don't use zsh but anyways, I just ran echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc and that was enough to fix the issue without changing the LDFLAGS and CPPFLAGS environment varsLinkboy
M
12

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 from 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
Mothy answered 12/2, 2021 at 16:54 Comment(2)
Worked for me on MacOS Ventura! Thanks!Robbery
Worked for me on MacOS Sonoma 14^. Thanks!Peterpeterborough
S
10

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!

Scenography answered 22/8, 2019 at 22:39 Comment(0)
D
10

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

Dispermous answered 19/9, 2019 at 12:5 Comment(1)
I like leveraging $(brew --prefix openssl) here.Aubigny
C
5

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

worked for me

Cheeseparing answered 11/7, 2020 at 9:35 Comment(0)
B
4

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.

Butte answered 27/8, 2019 at 17:1 Comment(0)
F
4

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/[email protected]/lib/

Note the @1.1 after openssl.

Figurant answered 27/8, 2020 at 13:51 Comment(0)
B
2

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.

Bulwark answered 14/6, 2019 at 8:14 Comment(0)
M
1

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.

Mechanistic answered 3/3, 2019 at 3:52 Comment(0)
B
1

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
Bakken answered 18/3, 2020 at 23:16 Comment(0)
M
1

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
Mcgruter answered 24/2, 2021 at 7:2 Comment(0)
A
0

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: How to update Xcode from command line, which references: https://forums.developer.apple.com/thread/104296

Anabiosis answered 1/9, 2019 at 14:21 Comment(1)
A few further notes: I had the Xcode and the command line tools installed. The xcode-select --install command showed that I already had them installed, as did xcode-select -p, and echo $? returned a 0, which also indicated they were installed. I'm using pyenv and pyenv-virtualenv, so I installed psycopg2 into a virtual environment.Anabiosis
C
0

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

Clyte answered 22/1, 2020 at 16:10 Comment(0)
B
0

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

'''


Balfore answered 11/2, 2020 at 8:57 Comment(0)
A
0

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.

Albumin answered 20/10, 2020 at 14:52 Comment(0)
M
0

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

Mercurial answered 18/2, 2021 at 0:33 Comment(0)
D
0

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)

Degeneration answered 28/11, 2021 at 22:33 Comment(0)
A
-1

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

Aldwin answered 24/9, 2020 at 15:4 Comment(0)
F
-5

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.

Froma answered 14/1, 2015 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.