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

PythonDjangoHerokuPipPsycopg2

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

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

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

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

Solution 5 - Python

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.

Solution 6 - Python

The workaround is to intsall 'psycopg2-binary' package

Solution 7 - Python

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

Solution 8 - Python

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.

Solution 9 - Python

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.

Solution 10 - Python

Try this:

pip install psycopg2-binary

Solution 11 - Python

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

Solution 12 - Python

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.

Solution 13 - Python

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.

Solution 14 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWarAndPieceView Question on Stackoverflow
Solution 1 - PythonchuanView Answer on Stackoverflow
Solution 2 - PythonmonofalView Answer on Stackoverflow
Solution 3 - PythonandilabsView Answer on Stackoverflow
Solution 4 - PythonAndriy IvaneykoView Answer on Stackoverflow
Solution 5 - PythonFelix YanView Answer on Stackoverflow
Solution 6 - PythonAlex YavorskiyView Answer on Stackoverflow
Solution 7 - PythonHernan RamirezView Answer on Stackoverflow
Solution 8 - PythonsethcallView Answer on Stackoverflow
Solution 9 - PythonIndhu NachadalingamView Answer on Stackoverflow
Solution 10 - PythonAchint SharmaView Answer on Stackoverflow
Solution 11 - PythonAlinaView Answer on Stackoverflow
Solution 12 - PythonemispowderView Answer on Stackoverflow
Solution 13 - PythonhenrycjcView Answer on Stackoverflow
Solution 14 - PythonHarsh NamdevView Answer on Stackoverflow