[Django]-Can't install psycopg2 with pip in virtualenv on Mac OS X 10.7

9👍

Refer to official installation guide of psycopg:

Installing on Mac OS X As a first option, please consider using a
packaged version of Psycopg from Fink or MacPorts.

If you still want to build Psycopg from source, take a look at these
articles.

183👍

Just would like to share. The following code worked for me:

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

I am using macOS Sierra and psql 9.6.1.

I got the lib path from the pg_config command.

👤chuan

58👍

I am using MAC OS CATALINA verison 10.15.5 with python3 and psql (PostgreSQL) 12.3. Here is what worked for me:

Try installing openssl using brew

brew install openssl

after that export these variables in the terminal.

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

verify these variables have been exported by echo $LDFLAGS and after that you are good to go with installation of psycopg2 by typing

pip3 install psycopg2

35👍

First, download Postgres.app.

Then, before running pip install psycopg2, put the binary in your path:

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

NOTICE:

9.3 stands for version and will differ over time.

17👍

Generic LDFLAGS variable set

Just export LDFLAGS before installing it, here is generic command which shall work for OS X (and I believe any LINUX system which has same error):

 LDFLAGS="$(pg_config --ldflags)" pip install psycopg2

9👍

The workaround is to intsall ‘psycopg2-binary’ package

7👍

Install postgres with brew:

brew install postgres

Then, in your virtualenv install psycopg2 with this command:

ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install psycopg2

2👍

Focusing on this line: ld: library not found for -lpq

psycopg2, like most 3rd-party postgres libraries, wants ‘pg_config’ available in your path. I’m guessing that’s your problem.

Type ‘pg_config’ at the command prompt. I hope you see that it’s not found. If not, do a:

sudo find / -name pg_config

to find where it’s at, and then add that location to your path, run ‘pg_config’ and see it succeed, and then finally, re-run pip.

the find command is searching starting at your root dir; it will take a few minutes.

2👍

I was trying so many things and nothing worked. However, if you use Xcode CLI Tools on Mojave and have a problem installing psycopg2, try the following command and try to install psycopg2 again.

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

This was described as a Mojave Issue: Pillowissue

In my case, this solved the issue.

2👍

Try this:

pip install psycopg2-binary

1👍

First this:
pip install psycopg2-binary
and then:
pip install psycopg2 worked.

👤Alina

0👍

The following post helped me get it working:

https://stackoverflow.com/a/10326004/1361851

Had to install “command line tools” for Xcode and then I was able to pip install with the virtualenv just the same as the heroku tutorial.

0👍

I tried all of the above solutions but the only thing that resolved the issue for me is simply updating Xcode CLI utilities through the official App Store.

-1👍

Once I was also facing the same problem on Mac then I figured out that psycopg2 has a dependency on GCC library.

export LDFLAGS="-L/usr/local/opt/openssl/lib"

export CPPFLAGS="-I/usr/local/opt/openssl/include"

Just run this command and then try to run

pip install psycopg2

Hope this will work.

Leave a comment