[Django]-Cannot install psycopg2 Ubuntu

88๐Ÿ‘

โœ…

Youโ€™ve installed the python-dev libraries for Python 3. Your psycopg2 install is using python 2.7

If on Ubuntu 20.04 or above

sudo apt-get install build-essential

Then run the other steps for either Python 2 or 3:

If you want to use Python 3:

Make sure youโ€™ve installed python3-pip

sudo apt-get install python3-pip

Then:

pip3 install psycopg2

If that still fails, try installing the development headers for postgresql:

sudo apt install python3-dev libpq-dev
pip3 install psycopg2

If on Python 2

sudo apt update
sudo apt-get install postgresql postgresql-contrib
sudo apt-get install libpq-dev
sudo apt-get install python-dev
sudo apt-get install python-pip
pip2 install psycopg2-binary

22๐Ÿ‘

Note that on many distros, the development headers needed for compiling against libraries are not installed by default. For psycopg2 on Ubuntu youโ€™ll need the python3 and postgresql headers:

sudo apt install python3-dev libpq-dev
pip3 install psycopg2

These can be installed in your activated virtual environment.

๐Ÿ‘คGitau Harrison

18๐Ÿ‘

I was testing my application on a ubuntu docker image that only has python2.7.6 installed (as happens in most big orgs)

If you are using python2.x (though you should plan to move to 3.x asap), check the below dependencies:

sudo apt update
sudo apt-get install postgresql postgresql-contrib
sudo apt-get install libpq-dev # this is required as psycopg2 uses pg_config
sudo apt-get install python-dev
sudo apt-get install python-pip

Now install psycopg2 using:

pip2 install psycopg2-binary
๐Ÿ‘คForeverLearner

16๐Ÿ‘

I had the same error trying to install it in a virtualenv (with python3)

I solved it by installing a previous version of psycopg2.

pip install psycopg2==2.7.5

๐Ÿ‘คANDRESMA

7๐Ÿ‘

This solved mine. I am using Python 3.8.2, Ubuntu 20.04 LTS:

sudo apt-get install python3-dev
sudo apt-get install python3-pip
pip install psycopg2
๐Ÿ‘คShabeer

5๐Ÿ‘

To install psycopg2 in ubuntu or mate 20 you need first to install:

sudo apt install libpq-dev

and then:

pip3 install psycopg2

๐Ÿ‘คFabian Mesaglio

4๐Ÿ‘

In my case, I was facing this problem when I ran pip install -r requirements.txt to install all packages for a Django project on an Ubuntu machine, I ran into this error and many other installation errors.

To solve this one, I ran the following commands:

sudo apt install postgresql postgresql-contrib  

sudo apt install libpq-dev

sudo apt install python3-dev

sudo apt install python3-pip

sudo apt install python3-psycopg2

pip3 install psycopg2

pip3 install psycopg2-binary

Plus, also check if the Ubuntu and Python and Psycopg versions are compatible together.

Also, @Arghya Bhattacharya answers pip install aiopg, solve the issue when i ran into it the second time.

๐Ÿ‘คSingh

1๐Ÿ‘

I had to install this one as well on my Ubuntu 20.04 LTS:

sudo apt-get install build-essential
๐Ÿ‘คShahar Gino

1๐Ÿ‘

I faced same issue on my ubuntu 18.04 LTS OS.
Also, face some issues in Pillow. In both cases (psycopg2 and Pillow) these command solved my issue.

sudo apt install -y build-essential libssl-dev libffi-dev python3-dev libjpeg-dev libjpeg8-dev

sudo apt install libpq-dev

sudo apt-get install python3-pip

Note: I have installed psycopg2 in python=3.8.5 environment.

๐Ÿ‘คbelal_bh

0๐Ÿ‘

make sure you are using the correct psycopg version for the python version.

Example for python 3.8.

python 3.8, the supported version is psycopg 2.8.4.

Reference ubuntu 20.04 + python 3.8 , pip install psycopg2==2.7.3.2 error #1106

๐Ÿ‘คAllahbakash.G

-2๐Ÿ‘

I solved this issue using another package which itself internally installs psycopg2.

pip install aiopg

Leave a comment