"Failed building wheel for psycopg2" - MacOSX using virtualenv and pip
Asked Answered
B

20

93

I'm attempting to make a website with a few others for the first time, and have run into a weird error when trying to use Django/Python/VirtualEnv. I've found solutions to this problem for other operating systems, such as Ubuntu, but can't find any good solutions for Mac.

This is the relevant code being run:

virtualenv -p python3 venv
source venv/bin/activate
pip install -r requirements.txt

After running that block, I get the following errors:

AssertionError


Failed building wheel for django-toolbelt Running setup.py bdist_wheel for psycopg2

...

AssertionError


Failed building wheel for psycopg2 Failed to build django-toolbelt psycopg2

I believe I've installed the "django-toolbelt" and "psycopg2", so I'm not sure why it would be failing.

The only difference I can think of is that I did not use the command

sudo apt-get install libpq-dev

as was instructed for Ubuntu usage as I believe that installing postgresql with brew took care of the header.

Thanks for any help or insight!

Boylston answered 16/12, 2015 at 5:52 Comment(3)
My answer was found here: #26288542Ore
This solution worked for wheel, venv & pip - https://mcmap.net/q/53437/-why-is-python-setup-py-saying-invalid-command-39-bdist_wheel-39-on-travis-ciBayles
running sudo apt-get install libpq-dev and pip install psycopg2-binary worked for me, also after that i could run pipenv install psycopg2Labuan
U
113

For MacOS users

After trying all the above methods (which did not work for me on MacOS 10.14), that one worked :

  • Install openssl with brew install openssl if you don't have it already.
  • add openssl path to LIBRARY_PATH :
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
  • install psycopg2 with pip pip3 install psycopg2
Unpen answered 11/4, 2020 at 15:55 Comment(4)
Worked for me after ld: library not found for -lsslRickrack
tried all above answers, after that I applied this answer. worked on Big Sur 11.2.3 thanks a lot 🙏 Note: it worked after I ran pip3 install -r requirements.txtVinyl
For macOS on Apple Silicon, use export LIBRARY_PATH=$LIBRARY_PATH:/opt/homebrew/opt/openssl/lib instead.Innumerable
I faced same problem specially in mac OS.X, yes, it's actually a SSL problem. Fixed after exporting the ssl.Duiker
O
95

I had the same problem on Arch linux. I think that it's not an OS dependant problem. Anyway, I fixed this by finding the outdated packages and updating then.

pip uninstall psycopg2
pip list --outdated
pip install --upgrade wheel
pip install --upgrade setuptools
pip install psycopg2
Orator answered 17/1, 2016 at 17:52 Comment(4)
Worked like a charm for me on OS X, thanks! (OS X 10.11.2, Python 3.5.1, pip 7.1.2)Cacka
Worked for me as well. I believe for my specific virtualenv that the wheel package was out of date, as setuptools was fine. So it may just be necessary to run pip install --upgrade wheel.Nauseate
Worked like a charm on Ubuntu 18.04Tactful
Worked on Ubuntu 20.4Sailing
W
62

I was also getting same error. Using Python 3.7.3 and pip 19.1.1.

I used following command.

pip install psycopg2-binary==2.8.3
Welter answered 24/6, 2019 at 7:14 Comment(1)
For python 3.8 use pip install psycopg2-binarySruti
L
22

TDLR

If you aren't used to installing Python C-extensions, and psycopg2 isn't a core part of your work, try

pip install psycopg2-binary

Building Locally

psycopg2 is a C-extension, so it requires compilation when being installed by pip. The Build Prerequisites section of the docs explain what must be done to make installation via pip possible. In summary (for psycopg 2.8.5):

  • a C compiler must be installed on the machine
  • the Python header files must be installed
  • the libpq header files must be installed
  • the pg_config program must be installed (it usually comes with the libpq headers) and on $PATH.

With these prerequisites satisfied, pip install psycopg2 ought to succeed.

Installing pre-compiled wheels

Alternatively, pip can install pre-compiled binaries so that compilation (and the associated setup) is not required. They can be installed like this:

pip install psycopg2-binary

The docs note that

The psycopg2-binary package is meant for beginners to start playing with Python and PostgreSQL without the need to meet the build requirements.

but I would suggest that psycopg2-binary is often good enough for local development work if you are not using psycopg2 directly, but just as a dependency.

Concluding advice

Read the informative installation documentation, not only to overcome installation issues but also to understand the impact of using the pre-compiled binaries in some scenarios.

Led answered 11/4, 2020 at 16:58 Comment(0)
C
18

I had same problem and this appears to be a Mojave Issue, I was able to resolve with:

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
Collapse answered 15/6, 2019 at 4:55 Comment(1)
Thanks @farzad-farazmand. Helped me to to install pip install psycopg2==2.5.4 after running you above command on macOS 10.14.6.Congo
K
17

For MacOS users, this question has the correct solution:

install command line tools if necessary:

xcode-select --install

then

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2

Knop answered 12/6, 2020 at 9:16 Comment(0)
M
15

For Mac OS X users:

1. First check your postgresql path by running this command in terminal:

pg_config

If this fails lookup how to add pg_config to your path.

2. Next install Xcode Tools by running this command in terminal:

xcode-select --install

If you have both those sorted out now try to install psycopg2 again

Mcgary answered 30/10, 2016 at 15:51 Comment(1)
Both are fine but still got the problemBespeak
C
11

I was also facing the same after running all the above commands, but the following two commands worked for me:

  1. Instead of pip, use this:
sudo apt-get install libpq-dev
  1. then run this command:
pip install psycopg2
Cricoid answered 5/10, 2021 at 19:37 Comment(1)
apt recommends postgresql-doc-12 instead of libpq-dev now. psycopg2 installs successfully with either of these installed.Edmee
E
6

On OS X, I was able to solve this by simply upgrading wheel before installing psycopg2:

pip install --upgrade wheel
Emrich answered 1/5, 2016 at 18:25 Comment(0)
S
5

For OSX Sierra users, it seems that an xcode update is the solution: Can't install psycopg2 package through pip install... Is this because of Sierra?

Salutation answered 7/11, 2016 at 18:52 Comment(1)
I tried close to 10 different things (including building postgres from source, linking the libs, etc. to my path, practically reimaging my machine) this was all it was!Kathleenkathlene
P
4

I tried all the above solutions but they did not work for me. What I did was change the psycopg2 version in my requirements.txt file from psycopg2==2.7.4 to psycopg2==2.7.6

Pantaloon answered 3/2, 2020 at 15:50 Comment(2)
Are you describing what you did but then failed? That would not answer this question and be more of a new question. Or is this an answer, as in "Everything described here failed, but the folowing works."?Hyperdulia
@Yunnosch, I think they are saying that the proposed solutions didn't work, so they posted what did work for them. That said, they basically have the same solution as Utsav Preet from July 7, 2019.Bart
S
2

Fixed by installing python3.7-dev: sudo apt install python3.7-dev, based on the link.

  • Python: 3.7
  • Ubuntu: 20.04.3 LTS
Sherillsherilyn answered 3/2, 2022 at 14:25 Comment(1)
Thanks to this I realised I had to use python3.12-dev.Wot
C
1

Is your error message complete? the most encountered reason for failing to install psycopg2 on mac from pip is pg_config is not in path. by the way, using macports or fink to install psycopg2 is more recommended way, so you don't have to worry about pg_config, libpq-dev and python-dev.

plus, are using Python 3.5? then upgrage your wheel to > 0.25.0 using pip.

Clayton answered 11/1, 2016 at 3:18 Comment(0)
U
1

I faced the same issue, but the answers above didn't work for me. So this is what I did in my requirements.txt psycopg2-binary==2.7.6.1 and it worked fine

Ugo answered 7/7, 2019 at 15:54 Comment(0)
P
1

I had this issue on several packages, including psycopg2, numpy, and pandas. I simply removed the version from the requirements.txt file, and it worked.

So instead of psycopg2-binary==2.7.6.1 I just had psycopg2-binary.

Petrolic answered 9/6, 2020 at 19:24 Comment(1)
I had these packages in my project and have problem to active pipenv virtual environment after updating to python 3.8, all my attempts failed to activate pipenv virtual environment, but after removing version number after pandas, psycopg2 and psycopg2-binary in requirements.txtIlailaire
M
1
sudo apt install libpq-dev python3.X-dev 

where X is the sub version,

these should be followed by :

  pip install --upgrade wheel
  pip install --upgrade setuptools
  pip install psycopg2

Enjoy !!!

Metathesis answered 2/9, 2021 at 13:15 Comment(1)
This was it! The python3.X-dev was the one thing that got it working for me on Linux Mint! I was so frustrated! Thanks!Homeland
W
1

I though the LIBRARY_PATH would work but unfortunately it didn't.

Using Homebrew on MacOS Silicon, the following workaround did the trick for me:

LDFLAGS=-L/opt/homebrew/opt/openssl/lib CPPFLAGS=-I/opt/homebrew/opt/openssl/include pip install -r requirements.txt # or pip install psycopg2-binary

Wren answered 15/5, 2023 at 9:52 Comment(0)
R
0

I know you are asking for development environment but if you are deploying on server say, Heroku. Just add below line in the requirements.txt of your project.

django-heroku==0.3.1

As this package itself will install the required packages like psycopg2 on server deployment.So let the server(heroku) should take care of it.

Rahel answered 20/10, 2020 at 14:53 Comment(0)
A
0

I solved my problem by updating/installing vs_BuildTools. The link to the software was given in the error itself. Error Image

Abbess answered 1/11, 2021 at 9:16 Comment(2)
Please post the error logs as text, instead of posting the link to picture.Tamworth
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Finesse
C
0

On windows I've managed to solve this by adding C:\Program Files\PostgreSQL\15\lib to LIB env variable

Cur answered 28/10, 2023 at 13:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.