close

[Solved] Cannot install psycopg2 Ubuntu

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Cannot install psycopg2 Ubuntu in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Cannot install psycopg2 Ubuntu Error Occurs?

Today I get the following error Cannot install psycopg2 Ubuntu in python.

How To Solve Cannot install psycopg2 Ubuntu Error ?

  1. How To Solve Cannot install psycopg2 Ubuntu Error ?

    To Solve Cannot install psycopg2 Ubuntu Error I was testing my application on a ubuntu docker image that only has python2.7.6 installed (as happens in most big orgs)

  2. Cannot install psycopg2 Ubuntu

    To Solve Cannot install psycopg2 Ubuntu Error I was testing my application on a ubuntu docker image that only has python2.7.6 installed (as happens in most big orgs)

Solution 1

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

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

If on Ubuntu 20.04

sudo apt-get install build-essential

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

Solution 2

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read