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

49,641

Solution 1

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 2

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 3

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 4

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 5

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
Share:
49,641
WarAndPiece
Author by

WarAndPiece

Updated on July 05, 2022

Comments

  • WarAndPiece
    WarAndPiece almost 2 years

    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.

  • WarAndPiece
    WarAndPiece about 12 years
    hey, thanks for the response. however, when I type 'pg_config', it is found and there isn't a problem.
  • WarAndPiece
    WarAndPiece about 12 years
    Ok, I just installed psycopg2 using macports in the virtualenv. I'm guessing this does the same thing that pip install does in the tutorial? As in, I would just replace this step? Reason I'm asking is because the app is still failing when I launch it. When I type Heroku Logs, this is one of the lines I get: raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
  • Felix Yan
    Felix Yan about 12 years
    Using MacPorts to install won't work inside a virtualenv environment. To get your app working, you should check out those articles about building from source :)
  • WarAndPiece
    WarAndPiece about 12 years
    Ok, I followed the instructions in one of those articles: blog.timc3.com/2010/08/20/psycopg2-os-x-_pqbackendpid, and I got psycopg2 to install on my machine. However, i'm still getting the same error within the virtualenv environment. I'm not too sure how I would build from source within a virtualenv.
  • Felix Yan
    Felix Yan about 12 years
    Did you activated your virtualenv before running the last command python setup.py install ?
  • WarAndPiece
    WarAndPiece about 12 years
    That worked! Thank you! A more explicit version for those wondering: I recreated the hellodjango directory and went through the steps, and instead of doing 'pip install psycopg2', I just downloaded psycopg2-2.4.4.tar.gz and installed it using "python setup.py install".
  • goetz
    goetz over 8 years
    Thanks, I found this reply most useful, as it does not require MacPorts or any other tools.
  • Jorge Leitao
    Jorge Leitao about 7 years
    this was your first answer to SO, and it works great. Thank you for taking the time to put it here. This solution works to install psycopg2 within a virtualenv with postgres and dependencies installed with brew.
  • numbers are fun
    numbers are fun about 5 years
    pg_config --ldflags will give you the specific path for your setup.
  • Tim
    Tim about 5 years
    Works for me on macos 10.14.4 with python 3.7.2 and postgres 11.2. Thanks!
  • semore_1267
    semore_1267 about 5 years
    @Chuan's answer working on for me on OS Mojave in Virtualbox. Was running into a very nasty psycopg2 wheel build error. exporting LDFLAGS via pg_config --ldflags worked
  • Sam
    Sam almost 5 years
    This solution works for me in Mojave 10.14.3. Thanks!
  • Maor
    Maor almost 5 years
    Worked for me, ran that command outside the virtualenv, activated the env again, and installed with no issues
  • wonderwhy
    wonderwhy almost 5 years
    Works in Mojave 10.14.4 as well with Python 3.7 in virtualenv. Thank you
  • trinth
    trinth over 4 years
    This worked for me on OS X Catalina with postgresql installed via Brew and pip-installing pyscopg2 in a virtualenv. LDFLAGS=$(pg_config --ldflags) pip install psycopg2
  • Michal Krejčí
    Michal Krejčí over 4 years
    Works in Catalina with psql from brew, also I have used pipenv and not pip and worked too
  • Anna Ira Hurnaus
    Anna Ira Hurnaus about 4 years
    Hi Harsh, would you mind explaining a bit more how this solves the problem?
  • Harsh Namdev
    Harsh Namdev about 4 years
    @AnnaIraHurnaus Using the export command, the PATH variable is going to hold the newly added directory. This sets the path of the GCC which is required in installing psycopg2 . Hope you got it.
  • vincedgy
    vincedgy about 4 years
    With last version of psycopg2 (2.8.4) on last version of MAC OS X (v10.15.4) env LDFLAGS='-L/usr/local/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/readline/lib' pip install psycopg2==2.8.4
  • Benjamin Breton
    Benjamin Breton over 3 years
    Works on Catalina (10.15.7) with python 3.7.8 on virtualenv
  • Nick ODell
    Nick ODell almost 3 years
    The LDFLAGS environment variable was enough for me - I didn't need the CPPFLAGS one too.
  • defraggled
    defraggled over 2 years
    These commands ran beautifully, but python still could not find psycopg2. I had to then run pip install psycopg2-binary which finally worked. There's probably some redundant operations here. I don't care. It finally works.
  • jtlz2
    jtlz2 about 2 years
    Fink! MacPorts! Retro! :)
  • halil
    halil about 2 years
    this is better as it works for openssl installed in different paths