207

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?

1

25 Answers 25

397

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
18
  • 5
    Actually, xcode-select --install may be enough, this should use the system provided ssl. I'll update my comment above.
    – sas
    Commented Sep 2, 2016 at 9:08
  • 29
    xcode-select install is good enough for the system python. But it does not work for virtual environments. Commented Sep 2, 2016 at 22:35
  • 4
    xcode-select --install fixed the issue on Sierra for me, also for virtual environments
    – Danra
    Commented Oct 23, 2016 at 13:04
  • 11
    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. Commented Oct 27, 2016 at 11:13
  • 9
    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
    – machow
    Commented Feb 1, 2017 at 18:01
136

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
8
  • 3
    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.
    – sh37211
    Commented Dec 15, 2020 at 21:10
  • 11
    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.
    – McFizz
    Commented Feb 12, 2021 at 16:40
  • 2
    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.
    – RobKohr
    Commented Apr 15, 2021 at 4:52
  • 4
    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" ```
    – paniclater
    Commented May 10, 2021 at 4:24
  • 7
    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" Commented Oct 4, 2022 at 17:01
37

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
0
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
0
24

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
4
  • 2
    Thanks for sharing, this works for me! MBP 16" M1Max. Commented Apr 17, 2022 at 5:29
  • 1
    MBP 16" M1Pro worked like a charm. Thanks man. Commented Sep 12, 2022 at 14:19
  • 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!
    – Daniil
    Commented Jan 24, 2023 at 14:59
  • It worked for me as well. Thanks man Commented Jul 27 at 12:22
16

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.

1
  • Worked on M1 Chip OS Sonoma 14.4 ! Thanks!
    – Wold
    Commented May 9 at 16:05
13

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
2
  • 1
    Worked for me on MacOS Ventura! Thanks!
    – thakreyn
    Commented Jun 19, 2023 at 12:12
  • Worked for me on MacOS Sonoma 14^. Thanks! Commented Jan 7 at 23:11
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
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 vars Commented Apr 10, 2020 at 15:24
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!

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

1
  • I like leveraging $(brew --prefix openssl) here.
    – Joe
    Commented Aug 30, 2022 at 20:01
5

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

worked for me

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

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.

3

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.

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.

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

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.
    – ProfX
    Commented Sep 1, 2019 at 14:55
0

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

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

'''


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.

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

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)

-1

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

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.