How to install PostgreSQL (psycopg2) on macOS (with virtualenv) – ld: library not found for -lssl

I can’t believe I did it. Good Lord.

Problem

My OS is macOS Big Sur 11.2, I just wanted to set up my existing Django project and for that I needed to install psycopg. Either of this commands gave me trouble:

pip install psycopg2
pip install psycopg2-binary

I had this error for hours:

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

Solution

1. Install Homebrew and PostgreSQL

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install postgesql

2. Try and install openssl:

brew install openssl

Yay, let’s celebrate. No? Then try this:

3. Fix openssl:

brew unlink openssl && brew link openssl --force

Then you will see a message

If you need to have openssl@1.1 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"

4. Execute the last two tines of it:

export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"

To make it work in your virtualenv, run point 4 while your virtualenv is activated. In my case, after I set up a virtualenv in Pycharm, I open Terminal (Ctrl + F12 for Linux at least), and before the line I see:

(myvirtualenv) katerina@Mac-Katerina myproject %

And just run

export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"

5. Install it, baby

pip install psycopg2
pip install psycopg2-binary

This is it. We survived it, let’s congratulate each other.

Reference

  1. https://qastack.ru/superuser/1089390/how-to-install-libssl-dev-libffi-dev-on-mac-os

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.