Explain Codes LogoExplain Codes Logo

Error installing psycopg2, library not found for -lssl

python
pip-install
environment-variables
openssl
Anton ShumikhinbyAnton Shumikhin·Nov 14, 2024
TLDR

To resolve the psycopg2 installation error due to the absent SSL libraries, follow these commands:

  • Ubuntu/Debian: sudo apt-get install libpq-dev libssl-dev
  • Red Hat/CentOS: sudo yum install postgresql-devel openssl-devel
  • macOS: brew install postgresql openssl

For macOS particularly, set the environment variables as:

export LDFLAGS="-L$(brew --prefix openssl)/lib" export CPPFLAGS="-I$(brew --prefix openssl)/include"

Now, you can install psycopg2 using pip:

pip install psycopg2

Alternatively, you can skip the above steps and directly install the binary package:

pip install psycopg2-binary

Before performing these steps, make sure the Python and Postgres headers are installed. If the issue remains, refer to the detailed explanation in the below sections.

Adjusting to macOS and Apple M1

If you are a macOS user, in particular, one with an Apple M1 chip, you might have to modify certain directories and paths to accommodate the shift from Intel to ARM architecture. Post the openssl installation via Homebrew, adjust LDFLAGS and CPPFLAGS to point towards the appropriate location:

export LDFLAGS="-L/opt/homebrew/opt/openssl/lib" # Here lies the treasure of OpenSSL's lib export CPPFLAGS="-I/opt/homebrew/opt/openssl/include" # Knock knock, OpenSSL's include

Also, add the OpenSSL binary path to your PATH environment variable:

export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH" # New road, more OpenSSL

Getting around installation cache

Caching issues might hinder psycopg2 installation at times. To avoid cached packages, use the --no-cache option:

pip install --no-cache-dir psycopg2 # Fresh psycopg2, straight from the oven!

Xcode command line tools in action

Psycopg2 installation requires a C compiler for building from source. Xcode command-line tools dishes this out in macOS:

xcode-select --install # Step right up, Xcode tools!

Special considerations for macOS Catalina onwards

Users on macOS Catalina or newer might need to adjust or set extra environment variables due to changes made in the OS's security and dependency handling.

Dealing with Homebrew OpenSSL

While dealing with Homebrew's OpenSSL, refrain from force linking brew link openssl --force and stick to the safer practice of specifying library paths using environment variables.

Enabling environment variables via pipenv

With pipenv, you can initialize environment variables before the installation to ensure they are picked up during the build process:

pipenv install psycopg2 --dev # Dinner is prepared. Please set the dining table

OpenSSL version - match or perish

Psycopg2 might necessitate a specific version of OpenSSL. Verify your installed OpenSSL version and check if it adheres to psycopg2's requirements:

openssl version # Who am I?

Using global options for build_ext

Use global options for build_ext to specify include and library paths during ‘pip install’, which ensures proper linking to the necessary libraries:

pip install psycopg2 --global-option=build_ext --global-option="-L/opt/homebrew/opt/openssl/lib" --global-option="-I/opt/homebrew/opt/openssl/include" # Just taking the scenic route!