Can't install psycopg2 with pip in virtualenv on Mac OS X 10.7

I am following Heroku's tutorial to deploy a Django app: http://devcenter.heroku.com/articles/django#prerequisites.

Everything is working fine until I get to this part:

$ pip install Django psycopg2

I can install Django by itself, but the probelm is with psycopg2.

I keep getting this error:

ld: library not found for -lpq

collect2: ld returned 1 exit status

ld: library not found for -lpq

collect2: ld returned 1 exit status

lipo: can't open input file: /var/folders/_4/p6l0y0t51hd4xbq7llbyshmw0000gn/T//cc0L10mI.out (No such file or directory)

error: command 'gcc-4.2' failed with exit status 1

I've installed PostgreSQL 9.1 on my machine.

Also, in the output, there are bunch of lines like this:

gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -g -O2 -DNDEBUG -g -O3 -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.4.4 (dt dec pq3 ext)" -DPG_VERSION_HEX=0x090004 -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -I. -I/usr/include -I/usr/include/postgresql/server -c psycopg/typecast.c -o build/temp.macosx-10.6-intel-2.7/psycopg/typecast.o

I'm not really sure what it means, but I do notice that it has "macosx-10.6" in it so I'm wondering if that could be the issue? I'm on 10.7.

Thanks in advance for your help.


Solution 1:

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.

Solution 2:

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

Solution 3:

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.

Solution 4:

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=`echo $(pg_config --ldflags)` pip install psycopg2