6

I have installed PostgreSQL by brew on macOS:

brew install postgresql

Tried these commands, all failed:

  • pipenv install psycopg2
  • pip install psycopg2

Error:

...
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -DPSYCOPG_VERSION=2.8.4 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120001 -DHAVE_LO64=1 -I/Users/user0/.pyenv/versions/3.8.0/include/python3.8 -I. -I/usr/local/include -I/usr/local/include/postgresql/server -c psycopg/adapter_qstring.c -o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_qstring.o
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -DPSYCOPG_VERSION=2.8.4 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120001 -DHAVE_LO64=1 -I/Users/user0/.pyenv/versions/3.8.0/include/python3.8 -I. -I/usr/local/include -I/usr/local/include/postgresql/server -c psycopg/microprotocols.c -o build/temp.macosx-10.14-x86_64-3.8/psycopg/microprotocols.o
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -DPSYCOPG_VERSION=2.8.4 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120001 -DHAVE_LO64=1 -I/Users/user0/.pyenv/versions/3.8.0/include/python3.8 -I. -I/usr/local/include -I/usr/local/include/postgresql/server -c psycopg/microprotocols_proto.c -o build/temp.macosx-10.14-x86_64-3.8/psycopg/microprotocols_proto.o
  In file included from psycopg/microprotocols_proto.c:27:
  In file included from ./psycopg/psycopg.h:37:
  ./psycopg/config.h:81:13: warning: unused function 'Dprintf' [-Wunused-function]
  static void Dprintf(const char *fmt, ...) {}
              ^
  1 warning generated.
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -DPSYCOPG_VERSION=2.8.4 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120001 -DHAVE_LO64=1 -I/Users/user0/.pyenv/versions/3.8.0/include/python3.8 -I. -I/usr/local/include -I/usr/local/include/postgresql/server -c psycopg/typecast.c -o build/temp.macosx-10.14-x86_64-3.8/psycopg/typecast.o
  clang -bundle -undefined dynamic_lookup -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/user0/.pyenv/versions/3.8.0/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/user0/.pyenv/versions/3.8.0/lib build/temp.macosx-10.14-x86_64-3.8/psycopg/psycopgmodule.o build/temp.macosx-10.14-x86_64-3.8/psycopg/green.o build/temp.macosx-10.14-x86_64-3.8/psycopg/pqpath.o build/temp.macosx-10.14-x86_64-3.8/psycopg/utils.o build/temp.macosx-10.14-x86_64-3.8/psycopg/bytes_format.o build/temp.macosx-10.14-x86_64-3.8/psycopg/libpq_support.o build/temp.macosx-10.14-x86_64-3.8/psycopg/win32_support.o build/temp.macosx-10.14-x86_64-3.8/psycopg/solaris_support.o build/temp.macosx-10.14-x86_64-3.8/psycopg/connection_int.o build/temp.macosx-10.14-x86_64-3.8/psycopg/connection_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/cursor_int.o build/temp.macosx-10.14-x86_64-3.8/psycopg/cursor_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/column_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/replication_connection_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/replication_cursor_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/replication_message_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/diagnostics_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/error_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/conninfo_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/lobject_int.o build/temp.macosx-10.14-x86_64-3.8/psycopg/lobject_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/notify_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/xid_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_asis.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_binary.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_datetime.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_list.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_pboolean.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_pdecimal.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_pint.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_pfloat.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_qstring.o build/temp.macosx-10.14-x86_64-3.8/psycopg/microprotocols.o build/temp.macosx-10.14-x86_64-3.8/psycopg/microprotocols_proto.o build/temp.macosx-10.14-x86_64-3.8/psycopg/typecast.o -L/usr/local/lib -lpq -lssl -lcrypto -o build/lib.macosx-10.14-x86_64-3.8/psycopg2/_psycopg.cpython-38-darwin.so
  ld: library not found for -lssl
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  error: command 'clang' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for psycopg2

Also, setting this to shell and then reloading:

export PATH=/usr/local/Cellar/postgresql/12.1/bin:$PATH

doesn't work either.

Geeocode
  • 5,705
  • 3
  • 20
  • 34
rawmain
  • 309
  • 5
  • 15
  • Do you have multiple Python versions ? – RMPR Dec 06 '19 at 06:06
  • @RMPR Yes, I have installed multiple python versions by pyenv. But using 3.8.0 now. What does it matter? – rawmain Dec 06 '19 at 09:43
  • 2
    Does this answer your question? [error install mysqlclient with pip, library not found for -lssl](https://stackoverflow.com/questions/51701051/error-install-mysqlclient-with-pip-library-not-found-for-lssl) – phd Dec 06 '19 at 11:56
  • https://stackoverflow.com/search?q=%5Bpip%5D+ld%3A+library+not+found+for+%22-lssl%22 – phd Dec 06 '19 at 11:56
  • 1
    Does this answer your question? [pg\_config executable not found](https://stackoverflow.com/questions/11618898/pg-config-executable-not-found) – DUDANF Dec 06 '19 at 12:18

3 Answers3

14

First thing first, you need to verify that openssl is installed and correctly linked:

brew install openssl
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

Now, if using python 3.8 means python executable is bound to python 3.8, you may want to try the following workarounds:

  • Workaround #1: Install specifying the interpreter
python -m pip --no-cache install psycopg2

Just to be sure that the version of pip you're calling is indeed the one which is bound to your Python version.

If it still doesn't work, you might be running into this issue in that case you can downgrade your version of Python and try the above command again

  • Workaround #2: Install from source.

First, you need to download the source code hosted on PyPI and run the commands:

tar -xf psycopg2-2.8.4.tar.gz && cd psycopg2-2.8.4 # extract the file
python setup.py build
sudo python setup.py install
RMPR
  • 3,368
  • 4
  • 19
  • 31
  • 1
    Both of two methods got error: `ld: warning: directory not found for option '-L/usr/local/opt/openssl/lib' error: command 'clang' failed with exit status 1`. – rawmain Dec 07 '19 at 01:24
  • Did you verify the openssl thing ? – RMPR Dec 07 '19 at 09:25
  • @rawmain if the answer helped you, don't forget to mark it as valid – RMPR Dec 07 '19 at 22:17
  • 4
    Thanks! After eight hours of error messages, your recommendation to EXPORT finally got psycopg2 to install. Why must every, single, step of python be such a disaster of failure? It's like you need to have the secret handshake to make ANYTHING work. Again. Thanks for that final "mysterious knowledge of the ancients" that got my psycopg2 installed. One more day wasted on python configuration. – frozenjim Jan 09 '20 at 20:33
  • The exports where enough for me. Already had `openssl` installed correctly. Thank you so much! – tbrlpld Jan 16 '21 at 18:18
  • the exports solves the issue on macOS big sur – shivg Aug 04 '21 at 14:43
  • I'm getting those. Any ideas on how to solve? `ld: warning: directory not found for option '-L/usr/local/opt/openssl/lib' ld: warning: directory not found for option '-L/usr/local/Cellar/gsl/1.16/lib/' ld: library not found for -lssl clang: error: linker command failed with exit code 1 (use -v to see invocation) error: command '/usr/bin/clang' failed with exit code 1 [end of output]` Furthermore I just realized that I do not have a local/`opt`/ folder. How to fix that? I'm running a Mac M1 – Ndrslmpk Mar 02 '23 at 06:46
7

The best solution to go to in this case is to install the binary package

  pip install psycopg2-binary

After it, you could still try to re-install an older version of the psycopg2 package like this. This should work if you don't need necessarily the last version.

  pip install psycopg2==2.7.5
Cami Fandino
  • 191
  • 2
  • It also got `error: command 'clang' failed with exit status 1` error. – rawmain Dec 13 '19 at 05:11
  • @rawmain installing the command line tools could help solve the issue, do it like this: `xcode-select --install`. It would be helpful to see the whole error message. But this thread is also similar: https://stackoverflow.com/questions/50940302/installing-mysql-python-causes-command-clang-failed-with-exit-status-1-on-mac – Cami Fandino Dec 15 '19 at 11:10
  • Keep in mind that `psycopg2-binary` is only recommended for development purposes, and should not be used for production. – LondonAppDev Jan 21 '22 at 09:40
0
  • Try downgrade version of your Python3
  • Try use pip3

I solved this problem with that method

svmitin
  • 101
  • 1