[GIS] Python GDAL package missing header file when installing via pip

gdalinstallationpythonUbuntu

I asked this question on Stack Overflow before I realised this site existed. As this site it more appropriate, I'll ask it here too.

I'm trying to install gdal from pip pip install gdal inside a virtual environment (Ubuntu). It fails because it cannot find cpl_port.h

extensions/gdal_wrap.cpp:2853:22: fatal error: cpl_port.h: No such file or directory
compilation terminated

However GDAL is installed correctly and the header file is located at /usr/include/gdal/cpl_port.h. Is there some environment variable for GDAL that needs to be set in order for pip to find the header files?

Best Answer

selimnairb's answer is close but you wont have the headers unless you've installed libgdal-dev:

sudo apt-get install libgdal-dev

with that done,

export CPLUS_INCLUDE_PATH=/usr/include/gdal

export C_INCLUDE_PATH=/usr/include/gdal

and then

pip install GDAL

The compilation ran to completion and I have GDAL in my virtual env. Phew!

(edit 2018) Note: To be sure that you install the correct version and avoid any problem. retrieve the version with gdal-config --version. and then:

pip install GDAL==version