Skip to content

Instantly share code, notes, and snippets.

@misterEggroll
Created June 20, 2023 06:27
Show Gist options
  • Save misterEggroll/70cb122c62a26dece523712b3aef3669 to your computer and use it in GitHub Desktop.
Save misterEggroll/70cb122c62a26dece523712b3aef3669 to your computer and use it in GitHub Desktop.
Install psycopg2 on M1 Mac

The Problem

This is what I encounter when installing psycopg2 on my M1 Mac.

      clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include "-DPSYCOPG_VERSION=2.9.6 (dt dec pq3 ext lo64)" -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=150003 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -I/Users/<user>/.pyenv/versions/iproxy/include -I/Users/<user>/.pyenv/versions/3.10.11/include/python3.10 -I. -I/opt/homebrew/opt/libpq/include -I/opt/homebrew/opt/libpq/include/postgresql/server -c psycopg/win32_support.c -o build/temp.macosx-13.4-arm64-cpython-310/psycopg/win32_support.o
      In file included from psycopg/win32_support.c:28:
      In file included from ./psycopg/psycopg.h:38:
      ./psycopg/config.h:69: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/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include "-DPSYCOPG_VERSION=2.9.6 (dt dec pq3 ext lo64)" -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=150003 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -I/Users/<user>/.pyenv/versions/iproxy/include -I/Users/<user>/.pyenv/versions/3.10.11/include/python3.10 -I. -I/opt/homebrew/opt/libpq/include -I/opt/homebrew/opt/libpq/include/postgresql/server -c psycopg/xid_type.c -o build/temp.macosx-13.4-arm64-cpython-310/psycopg/xid_type.o
      clang -bundle -undefined dynamic_lookup -L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/readline/lib -L/Users/<user>/.pyenv/versions/3.10.11/lib -Wl,-rpath,/Users/<user>/.pyenv/versions/3.10.11/lib -L/opt/homebrew/lib -Wl,-rpath,/opt/homebrew/lib -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/readline/lib -L/Users/<user>/.pyenv/versions/3.10.11/lib -Wl,-rpath,/Users/<user>/.pyenv/versions/3.10.11/lib -L/opt/homebrew/lib -Wl,-rpath,/opt/homebrew/lib -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib build/temp.macosx-13.4-arm64-cpython-310/psycopg/adapter_asis.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/adapter_binary.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/adapter_datetime.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/adapter_list.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/adapter_pboolean.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/adapter_pdecimal.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/adapter_pfloat.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/adapter_pint.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/adapter_qstring.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/aix_support.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/bytes_format.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/column_type.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/connection_int.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/connection_type.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/conninfo_type.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/cursor_int.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/cursor_type.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/diagnostics_type.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/error_type.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/green.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/libpq_support.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/lobject_int.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/lobject_type.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/microprotocols.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/microprotocols_proto.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/notify_type.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/pqpath.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/psycopgmodule.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/replication_connection_type.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/replication_cursor_type.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/replication_message_type.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/solaris_support.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/typecast.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/utils.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/win32_support.o build/temp.macosx-13.4-arm64-cpython-310/psycopg/xid_type.o -L/Users/<user>/.pyenv/versions/3.10.11/lib -L/opt/homebrew/opt/libpq/lib -lpq -lssl -lcrypto -o build/lib.macosx-13.4-arm64-cpython-310/psycopg2/_psycopg.cpython-310-darwin.so
      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]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for psycopg2
  Running setup.py clean for psycopg2
Failed to build psycopg2
ERROR: Could not build wheels for psycopg2, which is required to install pyproject.toml-based projects

This is what I did to solve the problem:

1. Reinstall Openssl

% brew reinstall openssl                                                                          
==> Fetching openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/manifests/3.1.1
Already downloaded: /Users/<username>/Library/Caches/Homebrew/downloads/a3831d258a8ccc63638c4ae11c47987af956f6db04046977e1583c1410df00a8--openssl@3-3.1.1.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/blobs/sha256:ec0a9f2f744809e81aca2a8953479b490b2c5341238615540c15c74019187ac0
Already downloaded: /Users/<username>/Library/Caches/Homebrew/downloads/10e5960204046ee0ca02040ea350c3d7134158d7b491e148ae41c5ef58b3d475--openssl@3--3.1.1.arm64_ventura.bottle.tar.gz
==> Reinstalling openssl@3 
==> Pouring [email protected]_ventura.bottle.tar.gz
🍺  /opt/homebrew/Cellar/openssl@3/3.1.1: 6,495 files, 28.4MB
==> Running `brew cleanup openssl@3`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

2. Find out folder locations used to set LDFLAGS

After this, I checked where the bin and include folders are for openssl.

% ls /opt/homebrew/Cellar/openssl@3/3.1.1
AUTHORS.md              INSTALL_RECEIPT.json    NEWS.md                 bin                     lib
CHANGES.md              LICENSE.txt             README.md               include                 share

3. Install psycopg2 using LDFLAGS

Now that you know the folder locations, install psycopg2 using env vars:

% env LDFLAGS="-I/opt/homebrew/Cellar/openssl@3/3.1.1/include -L/opt/homebrew/Cellar/openssl@3/3.1.1/lib" pip install psycopg2
Collecting psycopg2
  Using cached psycopg2-2.9.6.tar.gz (383 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: psycopg2
  Building wheel for psycopg2 (setup.py) ... done
  Created wheel for psycopg2: filename=psycopg2-2.9.6-cp310-cp310-macosx_13_0_arm64.whl size=145782 sha256=701cefbc36bec6bf31751dc21888ee2d96072af99ade9c45a9c7c687d3a7461e
  Stored in directory: /Users/<user>/Library/Caches/pip/wheels/a2/65/83/78e6f42d3b8e22115e894576b71799d96ab5a790b8f7bcfa85
Successfully built psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2-2.9.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment