How to install the psycopg2 Python package in WSL 2

This post is a quick note to all about how to install psycopg2 in Linux. I used WSL 2 running Ubuntu 20.04.


I was trying to set up a toy project to learn Django using PostgreSQL. When I tried to install the recommend psycopg2 package from pip, I received two error messages.

The first error I received was: error: invalid command 'bdist_wheel' . My second error was related to C / C++ and was in my log files:

In file included from psycopg/adapter_asis.c:28:

./psycopg/psycopg.h:35:10: fatal error: Python.h: No such file or directory
  35  |  #include <Python.h>
      |           ^~~~~~~~~~

compilation terminated.

The Steps

  1. I installed the following dependencies: python-dev , python3-dev.
  2. I installed wheels using pip3 install wheel .
  3. I installed python[your version]-dev using Ubuntu's sudo command.


The root cause of the problem was that psycopg2 was looking for headers for Python 3.8.5. Since I am using Python 3.9.5, the headers could not be located and psycopg2 could not install. After installing python3.9-dev , psycopg2 successfully installed on my system.

I hope that this short post saves people time and energy.

Additional references: