74

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
  • 1,007
  • 1
  • 10
  • 10

14 Answers14

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.

Marat
  • 15,215
  • 2
  • 39
  • 48
chuan
  • 1,831
  • 1
  • 8
  • 2
  • 7
    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`. – Jorge Leitao Feb 17 '17 at 14:44
  • 24
    `pg_config --ldflags` will give you the specific path for your setup. – numbers are fun Apr 16 '19 at 23:42
  • 2
    Works for me on macos 10.14.4 with python 3.7.2 and postgres 11.2. Thanks! – Tim Apr 18 '19 at 09:12
  • @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 – semore_1267 May 01 '19 at 05:32
  • 4
    This solution works for me in Mojave 10.14.3. Thanks! – Sam May 30 '19 at 17:36
  • Works in Mojave 10.14.4 as well with Python 3.7 in virtualenv. Thank you – wonderwhy Aug 14 '19 at 11:55
  • 1
    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` – trinth Oct 23 '19 at 01:10
  • Works in Catalina with psql from brew, also I have used `pipenv` and not `pip` and worked too – Amélie Krejčí Jan 11 '20 at 10:24
  • 4
    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` – vincedgy Apr 01 '20 at 16:24
  • Works on Catalina (10.15.7) with python 3.7.8 on virtualenv – Benjamin Breton Oct 26 '20 at 10:22
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
monofal
  • 1,928
  • 1
  • 13
  • 15
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.

sudo make install
  • 5,629
  • 3
  • 36
  • 48
andilabs
  • 22,159
  • 14
  • 114
  • 151
  • Thanks, I found this reply most useful, as it does not require MacPorts or any other tools. – goetz Feb 14 '16 at 10:07
  • Thank you, it worked for me. For the future reference for those who might need it: macOS 10.15, 2020 Intel macbook pro. Postgres 14. – user2991036 Sep 12 '22 at 15:32
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
tekumara
  • 8,357
  • 10
  • 57
  • 69
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
9

The workaround is to intsall 'psycopg2-binary' package

Alex Yavorskiy
  • 124
  • 1
  • 4
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.

Felix Yan
  • 14,841
  • 7
  • 48
  • 61
  • 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)` – WarAndPiece Mar 13 '12 at 06:08
  • 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 :) – Felix Yan Mar 13 '12 at 06:11
  • Ok, I followed the instructions in one of those articles: http://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. – WarAndPiece Mar 13 '12 at 07:04
  • Did you activated your virtualenv before running the last command `python setup.py install` ? – Felix Yan Mar 13 '12 at 07:34
  • 1
    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". – WarAndPiece Mar 13 '12 at 16:22
  • Fink! MacPorts! Retro! :) – jtlz2 Mar 14 '22 at 19:07
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
henrycjc
  • 663
  • 7
  • 20
Hernan Ramirez
  • 625
  • 10
  • 11
  • 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. – defraggled Jan 05 '22 at 09:04
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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    Worked for me, ran that command outside the virtualenv, activated the env again, and installed with no issues – Maor Aug 12 '19 at 13:58
2

Try this:

pip install psycopg2-binary
Achint Sharma
  • 355
  • 4
  • 11
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.

sethcall
  • 2,837
  • 1
  • 19
  • 22
1

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

Saravanan G
  • 581
  • 1
  • 9
  • 26
Alina
  • 11
  • 1
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.

Community
  • 1
  • 1
emispowder
  • 1,787
  • 19
  • 21
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.

henrycjc
  • 663
  • 7
  • 20
-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.

Community
  • 1
  • 1
  • Hi Harsh, would you mind explaining a bit more how this solves the problem? – Anna Ira Hurnaus Feb 26 '20 at 11:59
  • @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. – Harsh Namdev Feb 27 '20 at 12:32