Error installing psycopg2, library not found for -lssl
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:
Now, you can install psycopg2
using pip:
Alternatively, you can skip the above steps and directly install the binary package:
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:
Also, add the OpenSSL binary path to your PATH environment variable:
Getting around installation cache
Caching issues might hinder psycopg2 installation at times. To avoid cached packages, use the --no-cache
option:
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:
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:
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:
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:
Was this article helpful?