Solving problem with installing psycopg2-binary to Apple M1-powered machine

Solving problem with installing psycopg2-binary to Apple M1-powered machine
Photo by Caspar Camille Rubin / Unsplash

Recently I got some surprisingly problematic issue with installing psycopg2-binary Python package via pip on Apple M1 machine.

I spent few hours in investigations and attempts to solve the problem and finally assembled solution from several various sources.

Here are the steps:

  1. Install postgres via its website or
brew install postgresql
  1. Install such standalone apps complimentary to Postgres as pg_dump, pg_restore, pg_config
brew install libpq

3. Install openssl

brew install openssl

4. Link openssl to symlink openssl to /usr/local/bin/ so other system software would find it

brew link --force openssl

5. Specify clang compiler flags for successful build of the package

export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"

export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"

At the moment of writing, I had openssl 3 installed on my computer, so I used these paths as compiler flags.

6. Finally, run the pip command

pip install psycopg2-binary