Debian / Ubuntu: Fatal error: Python.h: No such file or Directory

See all Troubleshooting related FAQ
Wwhen I’am trying to install 3rd party python apps using pip command and getting the following error:
gcc -pthread -fno-strict-aliasing -fwrapv -Wall -Wstrict-prototypes -fPIC -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/usr/include/python2.7 -c src/MD2.c -o build/temp.linux-x86_64-2.7/src/MD2.o
src/MD2.c:31:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1

So where can I find Python.h file and how do I fix this problem under Debian or Ubuntu Linux sever based system?

Python.h is nothing but a header file. It is used by gcc to build applications. You need to install a package called python-dev or python-${VERSION}-dev. This package includes header files, a static library and development tools for building Python modules, extending the Python interpreter or embedding Python in applications. Let us see how to fix, “Fatal error: Python.h: No such file or Directory” error on a Debian or Ubuntu Linux system.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux terminal
Category Package Manager
OS compatibility Debian Mint Pop!_OS Ubuntu
Est. reading time 4 minutes

Debian / Ubuntu: Fatal error: Python.h: No such file or Directory

Currently, most systems have both Python 2.x and 3.x installed. To find the python version, use the type command or command command as follows:
$ type -a python
$ type -a python2
$ type -a python3
$ ls -l /usr/bin/python*
$ ls -l /usr/bin/python* | grep -v '^l'

Now you know the installed Python path on your Debian or Ubuntu Linux and then print the Python version as follows by tying the full shell path for Python:
$ /usr/bin/python2 -V
$ /usr/bin/python3 -V

Use the apt command or apt-cache command:
$ apt-cache search '^python.-*dev'
$ apt search '^python[0-9]*-dev'

You can also use the following grep command to list all possible header files for Python version:
$ apt-cache search ^python | grep -iw header
Finally, you can list header file as per Python version too. For instance, if Python version is 3.8, then
$ apt-cache search ^python | grep -iw '3.8'
$ apt-cache search ^python | grep -i header | grep -iw '3.8'

Finding Python header file packag to install Python.h file

Click to enlarge

Let us install the python version 2.7 or 3.8 header file to eliminate “Fatal error: Python.h: No such file or Directory“. Try the apt-get command or apt-get command as follows to get updates for your Debian and Ubuntu:
$ sudo apt-get update

For Python version 2.x+

$ sudo apt-get install python-dev
$ sudo apt-get install python2-dev
Sample outputs (for my python 2.x):

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libexpat1-dev libssl-dev libssl-doc python2.7-dev zlib1g-dev
The following NEW packages will be installed:
  libexpat1-dev libssl-dev libssl-doc python-dev python2.7-dev zlib1g-dev
0 upgraded, 6 newly installed, 0 to remove and 2 not upgraded.
Need to get 32.5 MB of archives.
After this operation, 48.2 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 precise-updates/main libexpat1-dev amd64 2.0.1-7.2ubuntu1.1 [216 kB]
Get:2 precise/main zlib1g-dev amd64 1: [165 kB]
Get:3 precise-updates/main libssl-dev amd64 1.0.1-4ubuntu5.5 [1,525 kB]
Get:4 precise-updates/main libssl-doc all 1.0.1-4ubuntu5.5 [1,034 kB]
Get:5 precise-updates/main python2.7-dev amd64 2.7.3-0ubuntu3.1 [29.5 MB]
Get:6 precise/main python-dev amd64 2.7.3-0ubuntu2 [1,088 B]
Fetched 32.5 MB in 1s (21.4 MB/s) 
Selecting previously unselected package libexpat1-dev.
(Reading database ... 55152 files and directories currently installed.)
Unpacking libexpat1-dev (from .../libexpat1-dev_2.0.1-7.2ubuntu1.1_amd64.deb) ...
Selecting previously unselected package zlib1g-dev.
Unpacking zlib1g-dev (from .../zlib1g-dev_1%3a1.2.3.4.dfsg-3ubuntu4_amd64.deb) ...
Selecting previously unselected package libssl-dev.
Unpacking libssl-dev (from .../libssl-dev_1.0.1-4ubuntu5.5_amd64.deb) ...
Selecting previously unselected package libssl-doc.
Unpacking libssl-doc (from .../libssl-doc_1.0.1-4ubuntu5.5_all.deb) ...
Selecting previously unselected package python2.7-dev.
Unpacking python2.7-dev (from .../python2.7-dev_2.7.3-0ubuntu3.1_amd64.deb) ...
Selecting previously unselected package python-dev.
Unpacking python-dev (from .../python-dev_2.7.3-0ubuntu2_amd64.deb) ...
Processing triggers for man-db ...
Setting up libexpat1-dev (2.0.1-7.2ubuntu1.1) ...
Setting up zlib1g-dev (1: ...
Setting up libssl-dev (1.0.1-4ubuntu5.5) ...
Setting up libssl-doc (1.0.1-4ubuntu5.5) ...
Setting up python2.7-dev (2.7.3-0ubuntu3.1) ...
Setting up python-dev (2.7.3-0ubuntu2) ...

For Python version 3.x+

$ sudo apt-get install python3-dev
That is all. Now, you can compile or build any python based modules without issues. The main thing is to find the correct package name that matches your installed Python version on a Debian or Ubuntu Linux.

Summing up

The python.h header file is a traditional part of the Python programming language’s distribution, so it should be included when you install Python on your system.

  1. First, you find the python path:
    $ type -a python
    $ type -a python2
    $ type -a python3
  2. Then you print the Python version to match python.h header file
    $ /usr/bin/python -V
    $ /usr/bin/python2 -V
    $ /usr/bin/python3 -V
  3. Next, search for the header file package:
    $ apt-cache search '^python.*-dev'
    $ apt-cache search '^python.*-dev' | grep -i header | grep -iw '3.8'
  4. The following apt command will install the Python development files, including the python.h header file for Python version 3.
    $ sudo apt install python3-dev
  5. For Python version 2, try:
    $ sudo apt install python2-dev

Once you have the Python dev package installed, you should be able to find the python.h file includes a directory of your Python installation. For example:
$ find / -type f -iname 'python.h' -ls 2>/dev/null
Here is the output indicating that Python.h is located under /usr/include/python3.8/ or /usr/include/python2.7/ directory:

 11275176      4 -rw-r--r--   1 root     root         3615 Mar 13 15:56 /usr/include/python3.8/Python.h
 11284532      8 -rw-r--r--   1 root     root         4375 Jul 29  2022 /usr/include/python2.7/Python.h
     4464      4 -rw-r--r--   1 root     root         3615 Nov 14 18:29 /snap/certbot/2772/usr/include/python3.8/Python.h
     4464      4 -rw-r--r--   1 root     root         3615 Nov 14 18:29 /snap/certbot/2836/usr/include/python3.8/Python.h

Please note that I appended 2>/dev/null to the find command to avoid permission denied message spam on the screen while searching with find command.

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

31 comments… add one
  • Patrik Apr 30, 2016 @ 6:27


  • Boo Feb 6, 2017 @ 11:54

    For those who are still stuck, try to also install python3.5-dev. Was the problem for me !

    • RozanovKa May 23, 2017 @ 10:14

      python3.6-dev already, but it works for me too :)

  • Konstantinos Ameranis Mar 27, 2023 @ 16:59

    Had an issue with installing numpy on python3.8 with Ubuntu 18.04 in March 2023 (I know, I know, dist-upgrading is on my todo list) and this article went straight to the correct idea. Needed `python3.8-dev` instead of just `python-dev` since the latter is for 3.7

  • ikomrad Sep 30, 2023 @ 17:26

    Has anyone installed python3.9-devel on Rocky Linux? DNF says it cannot find that package.

    It also cannot find
    or the “-dev” packages, either.

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Oct 1, 2023 @ 8:56

      It is called python3-devel:

      sudo dnf list python3-devel
      Last metadata expiration check: 3:32:22 ago on Sun Oct  1 05:23:32 2023.
      Available Packages
      python3-devel.i686                   3.9.16-1.el9_2.1                 appstream
      python3-devel.x86_64                 3.9.16-1.el9_2.1                 appstream

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.