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

I am trying to install 3rd party python apps using pip command and getting the following error:

ADVERTISEMENTS

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

Tutorial details
DifficultyEasy (rss)
Root privilegesYes
RequirementsNone
TimeN/A
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. This package includes header files, a static library and development tools for building Python modules, extending the Python interpreter or embedding Python in applications. To install this package, enter:

For Python version 2.x+

$ sudo apt-get install python-dev
OR
# apt-get install python-dev

For Python version 3.x+

$ sudo apt-get install python3-dev
OR
# apt-get install python3-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 http://mirrors.service.networklayer.com/ubuntu/ precise-updates/main libexpat1-dev amd64 2.0.1-7.2ubuntu1.1 [216 kB]
Get:2 http://mirrors.service.networklayer.com/ubuntu/ precise/main zlib1g-dev amd64 1:1.2.3.4.dfsg-3ubuntu4 [165 kB]
Get:3 http://mirrors.service.networklayer.com/ubuntu/ precise-updates/main libssl-dev amd64 1.0.1-4ubuntu5.5 [1,525 kB]
Get:4 http://mirrors.service.networklayer.com/ubuntu/ precise-updates/main libssl-doc all 1.0.1-4ubuntu5.5 [1,034 kB]
Get:5 http://mirrors.service.networklayer.com/ubuntu/ precise-updates/main python2.7-dev amd64 2.7.3-0ubuntu3.1 [29.5 MB]
Get:6 http://mirrors.service.networklayer.com/ubuntu/ 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:1.2.3.4.dfsg-3ubuntu4) ...
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) ...

Now, you can compile or build any python based modules.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
28 comments… add one
  • Andy Jan 19, 2013 @ 10:59

    Thanks for this! Also I had to

    rm -rf ~/build

    To get it to work again – because the previous results had been cached!

  • Onok Feb 15, 2013 @ 21:36

    That saved me :) Thanks!

  • Derrick Coetzee Apr 1, 2013 @ 12:03

    In my case (building pyskein) I needed the python3-dev package.

    • Kunal Feb 1, 2015 @ 10:14

      Thanks for the tip on the python3 bit. I was running the command listed on and linux was telling me everything was up to date.

  • Danny Verbeek May 30, 2013 @ 13:48

    Tnx for your post

  • scliart Jun 8, 2013 @ 21:44

    Thank you.
    Really helped me. :D

  • Kevin Jun 11, 2013 @ 12:29

    For Fedora it is:

    yum install python-devel

  • Valerie Deane Jul 11, 2013 @ 15:18

    Does not work for me. I get the error “no installation candidate”.

  • amr negm Sep 15, 2013 @ 12:19

    thank you for help

  • Rohith Nov 13, 2013 @ 17:31

    Thank you :) ..~

  • Musa Baloyi Nov 21, 2013 @ 13:01

    Thanks!

  • Nooh P P Mar 11, 2014 @ 6:59

    I have installed python-dev but still..
    fatal error: Python.h: No such file or directory
    compilation terminated.

    …:(

    • Kamil Mar 30, 2014 @ 15:10

      Are you using Python3? If so, you have to install python3-dev package.

      • Kunal Feb 1, 2015 @ 10:15

        Yeah you have to use python3-dev

        sudo apt-get install python3-dev

  • naga May 9, 2014 @ 11:28

    src/main/Session.cpp:21:25: fatal error: cms/Session.h: No such file or directory
    compilation terminated.
    error: command ‘gcc’ failed with exit status 1…
    while python setup.py build

  • naga May 9, 2014 @ 11:33

    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DBOOST_PYTHON_NO_PY_SIGNATURES=1 -DPYACTIVEMQ_ENABLE_DOCSTRINGS=0 -Ipython_activemq_libs/activemq-cpp-cppVersion/include/activemq-cpp-cppVersion -I/usr/include/python2.7 -c src/main/Session.cpp -o build/temp.linux-x86_64-2.7/src/main/Session.o
    cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for Ada/C/ObjC but not for C++ [enabled by default]
    src/main/Session.cpp:21:25: fatal error: cms/Session.h: No such file or directory
    compilation terminated.
    error: command ‘gcc’ failed with exit status 1

  • sree ph Jun 12, 2014 @ 8:43

    hi i used the following code and got the result as follows
    $ gcc -c arith.c arith_wrap.c \-I/usr/local/include/python2.7
    arith_wrap.c:125:20: fatal error: Python.h: No such file or directory
    compilation terminated.
    ttl@ttl-HP-Z220-CMT-Workstation:~/Documents/first_step_2_swig$

    i did sudo apt-get to install python 2.7 and it showed my library is updated.. the error repeats.. please give some suggestion…

  • Cueto Sep 5, 2014 @ 17:27

    Gracias, me funciuono a la primera

    sudo apt-get install python2.7-dev

  • kostya Dec 5, 2014 @ 19:26

    Thenk you, second time this post help for me :)

  • dubo May 13, 2015 @ 12:32

    Thank you so much !!

  • Krishna Oct 27, 2015 @ 14:41

    I want to install paramiko on redhat machine.I ran these steps.
    1. easy_install pip
    2. pip install paramiko

    It gives the following error(s).

    fatal error: Python.h: No such file or directory
    #include “Python.h”
    ^
    I tried all the alternatives given above but it didn’t work for me . Any help is appreciated.

  • Erie Oct 30, 2015 @ 3:30

    Boom!

  • matt Mar 24, 2016 @ 14:38

    i was getting mad until i read your comment: python3-dev, of course!!!

    lifesaver!

  • Patrik Apr 30, 2016 @ 6:27

    Thanks

  • 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 :)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.