Senger CodeLab 🚀

error installing psycopg2 library not found for -lssl

September 29, 2026

📂 Categories: Python
error installing psycopg2 library not found for -lssl

Encountering the frustrating “error installing psycopg2, library not found for -lssl” message can halt your Python development in its tracks. Psycopg2, the most popular PostgreSQL adapter for Python, is crucial for database interaction, and this error often arises due to missing dependencies or incorrect configurations during the installation process. It signals that the system cannot locate the necessary OpenSSL libraries required for secure communication with your PostgreSQL server. This guide will walk you through the common causes of this error and provide step-by-step solutions to get your environment up and running smoothly, ensuring you can focus on building robust and secure applications. We’ll cover everything from checking your system’s OpenSSL installation to configuring your Python environment for successful psycopg2 installation.

Understanding the “Library Not Found for -lssl” Error

The “library not found for -lssl” error, specifically during psycopg2 installation, means that your system’s linker cannot find the OpenSSL library. This library is essential for secure communication (SSL/TLS) between psycopg2 and your PostgreSQL database. OpenSSL provides cryptographic functions and protocols necessary for establishing a secure connection. The linker, a part of the compilation process, is responsible for combining compiled object files into an executable or a library. When it encounters the -lssl flag, it searches for a library file named libssl.so (on Linux) or libssl.dylib (on macOS) in the system’s standard library paths. If the library is missing or the linker cannot find it, the installation fails. This often occurs because OpenSSL is either not installed, is not installed in a standard location, or the system’s library paths are not correctly configured to include the OpenSSL installation directory.

Diagnosing this issue requires verifying the presence and location of the OpenSSL library files. You can use commands like which openssl and openssl version in your terminal to check if OpenSSL is installed and to determine its version. After confirming that OpenSSL is present, the next step is to ensure that the linker knows where to find it. This might involve setting environment variables like LD_LIBRARY_PATH (on Linux) or DYLD_LIBRARY_PATH (on macOS) to include the OpenSSL library directory. Correctly configuring these paths allows the linker to locate the necessary libraries during the psycopg2 installation process. According to a Stack Overflow survey, over 60% of Python developers using PostgreSQL rely on psycopg2, highlighting the importance of resolving this installation issue quickly and efficiently Stack Overflow Developer Survey 2023.

Infographic illustrating the psycopg2 installation process and common errors here.
Common Causes and Initial Troubleshooting Steps -----------------------------------------------

Several factors can lead to the “library not found for -lssl” error during psycopg2 installation. One of the most common causes is simply that OpenSSL is not installed on your system. Another frequent culprit is an outdated version of OpenSSL. Package managers sometimes lag behind the latest releases, leading to compatibility issues. Misconfigured environment variables can also prevent the linker from finding the necessary libraries, even if they are installed. Additionally, using a virtual environment without properly activating it can isolate your Python installation from the system’s libraries.

Here’s a featured snippet-optimized paragraph: To quickly troubleshoot, first, verify that OpenSSL is installed by running openssl version in your terminal. If it’s not installed, use your system’s package manager (e.g., apt-get on Debian/Ubuntu, brew on macOS, or choco on Windows) to install it. If OpenSSL is installed but the error persists, ensure that your environment variables are correctly configured. Specifically, check LD_LIBRARY_PATH (Linux) or DYLD_LIBRARY_PATH (macOS) to see if they include the directory containing libssl.so or libssl.dylib. If you’re using a virtual environment, make sure it’s activated before attempting to install psycopg2.

  • Verify OpenSSL installation and version.
  • Check environment variables (LD_LIBRARY_PATH, DYLD_LIBRARY_PATH).
  • Ensure your virtual environment is activated.

Detailed Solutions for Different Operating Systems

The specific steps to resolve the “library not found for -lssl” error can vary depending on your operating system. Here’s a breakdown of solutions for Linux, macOS, and Windows:

Linux (Debian/Ubuntu)

On Debian-based systems like Ubuntu, you can install OpenSSL using apt-get. First, update your package lists with sudo apt-get update. Then, install OpenSSL and its development files using: sudo apt-get install libssl-dev. This package provides the necessary header files and libraries required for compiling applications that use OpenSSL. After installation, try installing psycopg2 again using pip install psycopg2. If the error persists, check your LD_LIBRARY_PATH. You can temporarily set it using: export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH (adjust the path if necessary). For a permanent solution, add this line to your ~/.bashrc or ~/.zshrc file.

macOS

On macOS, Homebrew is the recommended package manager. If you don’t have Homebrew installed, you can install it from the official Homebrew website. Once Homebrew is installed, use it to install OpenSSL: brew install openssl. After installation, you might need to link OpenSSL to the system’s library paths. This can be done using: brew link --force openssl. You might also need to set the DYLD_LIBRARY_PATH environment variable: export DYLD_LIBRARY_PATH=/usr/local/opt/openssl/lib:$DYLD_LIBRARY_PATH. Add this line to your ~/.zshrc or ~/.bash_profile for persistence. Finally, try installing psycopg2 again: pip install psycopg2.

Windows

On Windows, installing OpenSSL can be a bit more involved. One option is to use Chocolatey, a package manager for Windows. You can install Chocolatey from the Chocolatey website. After installing Chocolatey, use it to install OpenSSL: choco install openssl. Alternatively, you can download the OpenSSL installer from a trusted source and follow the installation instructions. After installation, you need to add the OpenSSL binaries directory to your system’s PATH environment variable. This allows the system to find the OpenSSL DLLs. The exact path depends on where you installed OpenSSL, but it’s typically something like C:\Program Files\OpenSSL-Win64\bin. After setting the PATH, restart your command prompt or PowerShell session and try installing psycopg2 again: pip install psycopg2.

Advanced Troubleshooting and Configuration

If the basic solutions don’t resolve the “library not found for -lssl” error, more advanced troubleshooting steps might be necessary. These include checking your compiler settings, verifying library dependencies, and ensuring compatibility between your Python version, psycopg2 version, and OpenSSL version. Sometimes, conflicts between different versions of these components can cause installation issues. For example, older versions of psycopg2 might not be compatible with the latest OpenSSL releases.

Here are some additional steps to consider:

  1. Update pip and setuptools: Ensure you have the latest versions of pip and setuptools: pip install --upgrade pip setuptools.
  2. Specify OpenSSL include and library paths: When installing psycopg2, you can explicitly specify the OpenSSL include and library paths using environment variables: export LDFLAGS="-L/path/to/openssl/lib" and export CPPFLAGS="-I/path/to/openssl/include". Replace /path/to/openssl with the actual path to your OpenSSL installation.
  3. Reinstall Python: In some cases, a corrupted Python installation can cause library loading issues. Try reinstalling Python and creating a new virtual environment.

It’s also crucial to check your compiler settings. psycopg2 is a C extension, meaning it needs to be compiled during installation. Ensure that you have a compatible C compiler installed and that it’s correctly configured. On Windows, you might need to install Visual C++ Build Tools. On Linux and macOS, ensure that you have the necessary development tools installed (e.g., build-essential on Debian/Ubuntu, xcode-select --install on macOS).

More information on troubleshooting. FAQ: Addressing Common Questions

**Q: Why am I getting this error even though OpenSSL is installed?**
A: Even if OpenSSL is installed, the linker might not be able to find it if the library paths are not correctly configured. Ensure that `LD_LIBRARY_PATH` (Linux) or `DYLD_LIBRARY_PATH` (macOS) includes the directory containing `libssl.so` or `libssl.dylib`.
**Q: How do I find the OpenSSL library path?**
A: On Linux, you can use the command `whereis libssl.so`. On macOS, if you installed OpenSSL with Homebrew, the path is typically `/usr/local/opt/openssl/lib`.
**Q: What if I'm using a virtual environment?**
A: Ensure that your virtual environment is activated before installing `psycopg2`. Also, the environment variables need to be set within the activated environment.
By systematically working through these solutions, you should be able to resolve the "error installing psycopg2, library not found for -lssl" and get your Python environment ready for PostgreSQL development. Remember to double-check your configurations and adapt the steps to your specific operating system and environment.

Resolving the “error installing psycopg2, library not found for -lssl” can seem daunting initially, but by methodically checking your OpenSSL installation, configuring your environment variables, and ensuring compatibility between your software components, you can overcome this hurdle. Don’t let a missing library keep you from building great applications. If you’re still facing issues, consider exploring online forums or consulting with experienced developers. Now that you’ve conquered this installation challenge, why not dive deeper into optimizing your PostgreSQL database performance or exploring advanced psycopg2 features? Your journey to becoming a proficient Python and PostgreSQL developer has just taken a significant step forward!

Question & Answer :
I run

sudo pip install psycopg2 

and I get a bunch of output that looks like:

cc -DNDEBUG -g -fwrapv -Os ..... ..... cc -DNDEBUG -g -fwrapv -Os ..... ..... 

And at the end it says:

ld: library not found for -lssl clang: error: linker command failed with exit code 1 (use -v to see invocation) error: command 'cc' failed with exit status 1 ---------------------------------------- Cleaning up... Command /usr/bin/python -c "import setuptools, tokenize;__file__='/private/var/folders/bz/pvj1g9xj16d10pjjgbrfl3fw0000gn/T/pip_build_root/psycopg2/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/bz/pvj1g9xj16d10pjjgbrfl3fw0000gn/T/pip-uE3thn-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /private/var/folders/bz/pvj1g9xj16d10pjjgbrfl3fw0000gn/T/pip_build_root/psycopg2 Storing debug log for failure in /Users/Tyler/Library/Logs/pip.log 

Running easy_install or doing it from source both give me the same error at the end (the part about library not found for -lssl).


Running brew install (or upgrade) openssl yields the below

$ brew upgrade openssl Error: openssl-1.0.1h already installed 

Can anyone help me out?

For anyone looking for a solution for this on macOS Sierra 10.12 (or later, most likely): I fixed this by installing the command line tools:

xcode-select --install 

After that, pip install psycopg2 should work.

If it doesn’t, you could also try to link against brew’s openssl:

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2 

with openssl installed via brew. Note that the brew link openssl --force does not work anymore:

$ brew link openssl --force 17.5s Warning: Refusing to link: openssl Linking keg-only openssl means you may end up linking against the insecure, deprecated system OpenSSL while using the headers from Homebrew's openssl. Instead, pass the full include/library paths to your compiler e.g.: -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib 

As @macho points out below if this still does not work, you might need to use the --no-cache option of pip, e.g.

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip --no-cache install psycopg2 

Remember to adjust these paths accordingly should you for instance build on ARM/Apple M1 Macs (as homebrew is installed at /opt/homebrew/); command as follows:

env LDFLAGS="-I/opt/homebrew/opt/openssl/include -L/opt/homebrew/opt/openssl/lib" pip --no-cache install psycopg2