Can't install psycopg2 package through pip install on MacOS
Asked Answered
P

19

110

I am working on a project for one of my lectures and I need to download the package psycopg2 in order to work with the postgresql database in use. Unfortunately, when I try to pip install psycopg2 the following error pops up:

ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit status 1
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit status 1

Why is this happening?

Phagocytosis answered 29/9, 2016 at 10:29 Comment(0)
S
353

I fixed this by installing Command Line Tools

xcode-select --install

then installing openssl via Homebrew and manually linking my homebrew-installed openssl to pip:

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

on macOS Sierra 10.12.1

Similitude answered 30/9, 2016 at 22:1 Comment(5)
The xcode install was enough for me to get it working. Good looking out.Podgorica
I also got it after dev-tools installGard
for me also worked , thanks and what will be the permanent solution?Dodecasyllable
Works on Catalina as wellJacobo
as of May 2022 and brew, this works for me: LDFLAGS="-I/opt/homebrew/opt/openssl/include -L/opt/homebrew/opt/openssl/lib" pip install psycopg2-binarySesquiplane
C
64

Try installing it with:

pip install psycopg2-binary
Crumpled answered 22/5, 2019 at 11:28 Comment(5)
Is this interchangeable with psycopg2?Peroxide
@Peroxide yes it isCrumpled
Make sure your pip is up to date also pip install -U pip or you will get errors like vcvarsall.bat not foundPraseodymium
pypi.org/project/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."Viyella
this is a good answer for fairly recent psycopg2, but older versions (e.g. 2.8.6) are not provided by the packager and the wheel must be built locally.Cousin
S
48
brew install postgresql

If postgresql exist then run:

brew upgrade postgresql
sudo pip install psycopg2

In venv:

env LDFLAGS='-L/usr/local/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/readline/lib' pip install psycopg2==2.8.4

If you need use only the command pip install psycopg2 export the path in macOSX:

export LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib"
Spoilage answered 23/8, 2019 at 7:35 Comment(3)
This worked perfectly for me, using brew install and then type in the "env =" in my terminal. However this is complete dark magic, can anyone explain why this was necessary and what it actually did? Thanks! For information, in May 2020 the mac version is Catalina.Blastoderm
This is the only trick that worked for me :)Acquainted
brew install postgresql did the trick for me!Undersigned
S
22

I fixed it with:

brew reinstall openssl
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
Sinister answered 6/11, 2019 at 2:32 Comment(2)
This worked for me as well, on Catalina. I'm using Python version 3.8.2, where I have pyenv managing my Python environments and installations. Your solution worked from me, but I executed: $ python -m pip install psycopg2 instead of how others used $ pip install psycopg2, which we might not know which interpreter we are using and installing. This will ensure pip we will be using and installing with my default python interpreter, which is version 3.8.2 for me.Desertion
That export will need be added to my .bashrc forever?Muskellunge
K
13

Running into a similar problem using pipenv install psycopg2:

ld: library not found for -lssl', ' clang: error: linker command failed with exit code 1 (use -v to see invocation)', " error: command 'clang' failed with exit status 1", '
----------------------------------------',

I tried all of the recommendations above and none of them worked. I'd faced this problem in another virtualenv a few months ago and remembered reading something about this being a psycopg2 version issue. So I tried installing with 2.7.1 and 2.8.3 and both still failed. Somehow version 2.7.7 worked:

pipenv install psycopg2==2.7.7

Wish I had time to look into this further to understand what's really happening. But for now I don't and this worked.

Kraemer answered 4/9, 2019 at 4:1 Comment(0)
R
12

For Mac OS 11.06 BigSur M1 ship, installing under a pyenv

brew reinstall libpq
brew reinstall openssl

Or install if its not already the case

brew info libpq
brew info openssl

Combine the 2 exported output logically, from

For compilers to find openssl@3 you may need to set:

export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"

export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"

... export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"

export CPPFLAGS="-I/opt/homebrew/opt/libpq/include"

into

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

Then you can reinstall psycopg2, forcing the recompile, otherwise you will have this problem at runtime

pip uninstall psycopg2 
pip install psycopg2 --no-cache-dir
Recommendatory answered 1/10, 2021 at 8:55 Comment(0)
A
7
  1. Install/update Xcode developer tools

    xcode-select --install
    
  2. Query postgres path

    find / -name pg_config  2>/dev/null
    
  3. Install psycopg2, use the path you got in step 2. Mine was '/usr/local/Cellar/postgresql/9.5.0/bin/pg_config'

    PATH=$PATH:/usr/local/Cellar/postgresql/9.5.0/bin/ pip install psycopg2
    
Averett answered 14/10, 2016 at 9:0 Comment(0)
V
4

Well, tons of different options didn't work for me, but finally!

First, try this:

brew reinstall openssl

At the end of the reinstallation process you will get basically the following lines within Caveats section, which I've put in the zshrc config file since I use macOS Big Sur (version 11.1) and zsh terminal.

Open zshrc or other config file specific to your system:

nano ~/.zshrc

Paste the following lines at the end (or change these commands accordingly to the Caveats you've got), just keep in mind that OpenSSL provided by Homebrew might conflict with the system's one (I've got a warning that macOS provides LibreSSL) but it worked well anyway:

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

Save the file and exit by pressing ^X and confirming with Yes.

Hopefully you can now install psycopg2:

pip3 install psycopg2-binary

I'm still not sure what was the root cause of the problem, it just appeared suddenly when I tried to reinstall pipenv for my project (maybe some recent system updates).

Vacillate answered 30/1, 2021 at 18:33 Comment(1)
Same here! Your suggestion was worked! Thanks a lot for saving my day.Indue
L
2

It looks like the openssl package is not installed. Try installing it and pip install again. I'm not a macos user, but I believe that brew simplifies package management on that platform.

You might also need to install the Python development and postgresql development packages.

Lexi answered 29/9, 2016 at 11:39 Comment(0)
G
2

This works for me (on a terminal):

find / -name pg_config  2>/dev/null

then add the path:

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.5/bin/

os version: MacOs Sierra

I hope this helps.

Geulincx answered 3/2, 2017 at 23:34 Comment(2)
This assumes you have installed Postgres.app on your mac and not using mac ports, home brew, the EnterpriseDB installer or another way of packaging/installing your postgresql server on your mac, yes?Higgs
Oh also the original question was about missing -lssl library and then "clang", but not the pg_config tool. Clang is in Xcode, the other thing is in the openssl library. user @Similitude has the right answer to this question. (I got here looking for the pg_config issue, so your answer helped me a ton, but it's not what the question asked).Higgs
A
1

Old, but for folks using pipenv, I resolved this issue by looking at my dependency graph pipenv graph --reverse and noticing it required psycopg2-binary==2.8.2. Running pipenv install psycopg2-binary resolved the issue.

Ani answered 26/5, 2019 at 15:7 Comment(0)
F
1
env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" \
brew upgrade openssl

Then I could install psycopg2

Freak answered 29/10, 2019 at 12:48 Comment(0)
V
1

try this -->pip install psycopg2-binary

Vosges answered 10/8, 2020 at 6:29 Comment(1)
Welcome to Stack Overflow! Please note you are answering a very old and already answered question. Here is a guide on How to Answer.Couture
S
1

On my scenario, it was performed on:

ProductName:    macOS
ProductVersion: 12.1
BuildVersion:   21C52
  • I proceed to install the following libraries by brewcommand:
brew install openssl libpq
  • Set specific variables:
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib -L/opt/homebrew/opt/openssl@3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libpq/include -I/opt/homebrew/opt/openssl@3/include"
  • Proceed to install the python package psycopg2-binary via pip3:
pip3 install psycopg2-binary

If you are wondering why it is necessary to set those specific variables, take a look in the docs of the package via brew info command:

brew info openssl
brew info libpq

When the outcome is displayed, look for this sentence:

...
For compilers to find libpq you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/libpq/include"
...
For compilers to find openssl@3 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"
...

Thats why, when pip is installing a package, needs a reference where to look for the libraries/compilers.

A perfect place to set this variables would be ~/.bashrc, for future installations regarding postgres and ssl:

export LDFLAGS="-L/opt/homebrew/opt/libpq/lib -L/opt/homebrew/opt/openssl@3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libpq/include -I/opt/homebrew/opt/openssl@3/include"
Solothurn answered 9/7, 2022 at 9:42 Comment(0)
B
0

In case you don't want to faff around with brew, here's the "MacOS-only solution":

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

After this, pip will gladly install psycopg2 from the source. Works with "Mojave" (macOS 10.14).

Blanche answered 11/3, 2020 at 12:37 Comment(0)
V
0

The only thing that worked for me was:

pip install psycopg2==2.7.7
Volitive answered 11/1, 2021 at 5:41 Comment(0)
F
0

Try installing: pip install psycopg2-binary it works

Fingerling answered 17/2, 2021 at 8:20 Comment(0)
O
0

I ran into the same error on macOS Big Sur (11.5.1) and Python 3.9.6.

It turned out, the psycopg2 (as well as pandas) versions I was using were too old. The error disappeared and pip install started working fine after upgrading the above packages.

Ref: https://github.com/Azure/azure-cli/issues/12854#issuecomment-619213863

In general, if the solutions suggested here by others don't help, try upgrading the troubling python package.

Orsino answered 8/9, 2021 at 18:53 Comment(0)
S
-4

As per current/todey condition - only solution for this is - uninstall your python 3.8 or 3.9 or so on and install ptyhon 3.7.4

Sacksen answered 23/5, 2020 at 13:53 Comment(2)
Given this question was asked in 2016, I doubt very much that "uninstall Python 3.8/3.9 and install Python 3.7" is a solution to the OP's problemLythraceous
This is so wrong. I ran @Apurva's answer and it worked for me. I'm using Catalina, and I have multiple Python versions. https://mcmap.net/q/24266/-can-39-t-install-psycopg2-package-through-pip-install-on-macosDesertion

© 2022 - 2024 — McMap. All rights reserved.