Fixing psycopg2 Installation Errors on Ubuntu and Mac: A Simple Guide

Abdullah zulfiqar
Django Unleashed
Published in
2 min readJan 25, 2024

Some time ago, PyPI started providing easy-to-use binary packages for Windows, which means Windows users shouldn’t face this problem anymore. However, for Linux and Mac users who come across this post while searching online, we have solutions below.

Option 1: You can install the psycopg2-binary package from PyPI instead. psycopg2-binary comes with Python wheels that are compatible with both Linux and MacOS.

poetry add psycopg2-binary  # FOR POETRY USERS
pip install psycopg2-binary

However, if you still encounter problems, you can consider trying out option 2 below.

Option 2: you need to install the things required to build the psycopg2 package from its source code.

For Debian/ubunto:

For default python 3:

sudo apt install libpq-dev python3-dev

For Python 3.x (substitute x in command)

sudo apt install libpq-dev python3.x-dev

For Python 2:

sudo apt install libpq-dev python-dev

If that’s not sufficient, you may also have to install:

sudo apt install build-essential

For Debian and Ubuntu, there is also a another option to consider. Run the command below before installing psycopg2 again.

sudo apt install postgresql-server-dev-all

For MacOs:

brew install postgresql

And all other options are here: http://www.postgresql.org/download/macosx/

click to check Credit: joar answer at stackoverflow

--

--

Abdullah zulfiqar
Django Unleashed

Software Engineer Python | Full Stack Architect | Python Engineer | Django | Flask | FastAPI | AI | Freelance