203

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?

RMPR
  • 3,368
  • 4
  • 19
  • 31
tscizzle
  • 11,191
  • 15
  • 54
  • 88
  • Possible duplicate of [python install lxml on mac os 10.10.1](https://stackoverflow.com/questions/30410030/python-install-lxml-on-mac-os-10-10-1) – IamAshay Jun 26 '19 at 21:12

25 Answers25

378

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
Community
  • 1
  • 1
sas
  • 7,017
  • 4
  • 36
  • 50
  • 5
    Actually, `xcode-select --install` may be enough, this should use the system provided `ssl`. I'll update my comment above. – sas Sep 02 '16 at 09:08
  • 29
    xcode-select install is good enough for the system python. But it does not work for virtual environments. – Andrew Martin Sep 02 '16 at 22:35
  • 2
    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 packages – Mr-F Sep 26 '16 at 20:36
  • 3
    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. – webtweakers Sep 30 '16 at 19:23
  • 4
    `xcode-select --install` fixed the issue on Sierra for me, also for virtual environments – Danra Oct 23 '16 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. – freethebees Oct 27 '16 at 11:13
  • Just the `xcode-select --install` fix worked for me, even using a virtualenv. – fletom Dec 01 '16 at 18:50
  • After running `xcode-select --install`, I had to reinstall Python before the build would work without `LDFLAGS`. – Theron Luhn Dec 02 '16 at 18:28
  • didn't work for me, moving to venv instead of virtualenv did work somehow – Dmytro Jan 12 '17 at 14:38
  • 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 Feb 01 '17 at 18:01
  • Also worth noting I needed to update my pinned version of psycopg2 to 2.7.5. – Ben Murden Aug 04 '18 at 10:56
  • 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 works – randy Oct 31 '18 at 16:41
  • The `--no-cache` option helped me - @machow thanks! I also did an update to Catalina, installed newest Xcode and command-line tools. – Bartłomiej Skwira Dec 24 '19 at 11:16
  • 2
    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/openssl@1.1/include -L/usr/local/opt/openssl@1.1/lib" pip install psycopg2` – TheJKFever Apr 01 '20 at 01:13
  • 1
    `xcode-select` wasn't enough. I have to re-link with brew command. Thanks! – Javier Cornejo Alfaro Apr 07 '20 at 14:10
  • This also works for macports, just tweak the paths LDFLAGS="-I/opt/local/include/openssl -L/opt/local/lib" – yolabingo Oct 30 '20 at 04:27
  • If you are trying to install on Apple Silicon, see this answer https://stackoverflow.com/a/66175899/5460959 – McFizz Feb 12 '21 at 16:56
  • `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! – Kate Donaldson Jun 02 '21 at 22:49
128

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
IamAshay
  • 1,377
  • 1
  • 7
  • 16
  • 1
    I already had the xcode tools installed, but this worked for me also! – ruohola Oct 05 '20 at 15:00
  • 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 Dec 15 '20 at 21:10
  • 7
    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 Feb 12 '21 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 Apr 15 '21 at 04:52
  • 3
    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/openssl@1.1/1.1.1k/lib/" export CPPFLAGS="-I/opt/homebrew/Cellar/openssl@1.1/1.1.1k/include" ``` – paniclater May 10 '21 at 04:24
  • 1
    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"``` – isaacparrot Oct 04 '22 at 17:01
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
André Borie
  • 808
  • 9
  • 13
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
Chris Ford
  • 301
  • 3
  • 2
19

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
Khoi Ngo
  • 941
  • 8
  • 16
14

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/openssl@1.1/bin:$PATH"' >> ~/.zshrc

export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/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.

MichaelKleine
  • 159
  • 1
  • 9
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
Isaac Philip
  • 498
  • 1
  • 5
  • 14
  • 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 – Mark Aquino Apr 10 '20 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!

pmneve
  • 596
  • 1
  • 5
  • 18
9

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

Envil
  • 2,687
  • 1
  • 30
  • 42
7

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/openssl@1.1. 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
McFizz
  • 3,013
  • 3
  • 18
  • 26
5

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

worked for me

beyondfloatingpoint
  • 1,239
  • 1
  • 14
  • 23
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.

Yngve Høiseth
  • 570
  • 6
  • 26
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/openssl@1.1/lib/

Note the @1.1 after openssl.

Qback
  • 4,310
  • 3
  • 25
  • 38
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.

Community
  • 1
  • 1
Flimm
  • 136,138
  • 45
  • 251
  • 267
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.

dixon1e
  • 146
  • 1
  • 7
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
DjAlan
  • 71
  • 3
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/openssl@1.1/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"
pip install psycopg2
cpk
  • 809
  • 1
  • 6
  • 20
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

ProfX
  • 236
  • 2
  • 6
  • 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 Sep 01 '19 at 14:55
0

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

Nili Waypa
  • 121
  • 1
  • 5
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.

Christopher Nolan
  • 930
  • 1
  • 11
  • 15
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/openssl@1.1/1.1.1i/include -L/opt/homebrew/Cellar/openssl@1.1/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).

stilManiac
  • 454
  • 3
  • 13
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)

Andre OBrien
  • 160
  • 1
  • 11
-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.

Werner
  • 2,537
  • 1
  • 26
  • 38
ecordo
  • 7
  • 1