24

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?

Kevin
  • 1,113
  • 3
  • 12
  • 26

7 Answers7

25

As suggested in the other thread, exporting some shell variables before running pip worked flawlessly. A path for *_INCLUDE_PATH can be found with gdal-config --cflags.

# GDAL library must have been installed
sudo apt-get install libgdal-dev

# Set up pip and/or virtualenv stuff
...

# Now install Python binding for GDAL
export CPLUS_INCLUDE_PATH=/usr/include/gdal
export C_INCLUDE_PATH=/usr/include/gdal
pip install GDAL
Community
  • 1
  • 1
tomyun
  • 388
  • 7
  • 6
  • 1
    This fixed it for me, but also see @Neil Smith's answer: in Ubuntu 14.04, you need to match the version of libgdal installed on the system. – Gabriel Jan 31 '15 at 09:37
  • Still think this is bug. gdal_wrap.cpp should #include "gdal/cpl_port.h" instead of just "cpl_port.h" – Jaap Versteegh Nov 01 '16 at 09:28
  • Definitely did NOT work for me. Still same cpl_port.h error. Using OpenSUSE – DanCat Nov 14 '16 at 16:42
  • 3
    pip3 install GDAL==$(gdal-config --version) Explicit version number should match your existing libgdal version. – Ivan Kovtun Apr 29 '19 at 19:29
8

Tomyun's answer worked for me, with the proviso that you have to ensure that the version of GDAL-dev installed via apt-get matches the version being installed by pip.

For Ubuntu 14.04, the commands are:

# GDAL library must have been installed
sudo apt-get install libgdal-dev

# Set up pip and/or virtualenv stuff
...

# Now install Python binding for GDAL
export CPLUS_INCLUDE_PATH=/usr/include/gdal
export C_INCLUDE_PATH=/usr/include/gdal
pip3 install GDAL=1.10.0
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Neil Smith
  • 313
  • 3
  • 10
8

Taken from this comment, it solved my issue directly

pip3 install GDAL==$(gdal-config --version) 

Explicit version number should match your existing libgdal version. –

Rhibi Hamza
  • 136
  • 1
  • 4
5

Using PIP :

pip install --no-install GDAL

Then cd into ENV/build/GDAL

python setup.py build_ext --include-dirs=/usr/include/gdal
pip install --no-download GDAL

(Source: http://ubuntuforums.org/showthread.php?t=1769445)

Using Buildout :

[gdal-bindings]
recipe = zc.recipe.egg:custom
egg = GDAL==1.9.1
include-dirs = /usr/include/gdal
library-dirs = /usr/lib
leplatrem
  • 1,005
  • 13
  • 25
  • This could also be `include-dirs=/Library/Frameworks/GDAL.framework/Versions/2.1/Headers/` on OSX if you installed w/ Kyngchaos... – Owen Dec 05 '16 at 14:23
1

I was also getting this error when test installing in a virtual environment a package of mine that depends on GDAL. In this case the solution is to change the dependecy from GDAL to pygdal in the install_requires parameter in setup.py. Like so:

install_requires=['pygdal'],

Luís de Sousa
  • 5,765
  • 11
  • 49
  • 86
1

This is what worked for me:

I had to get the latest hearder versions for installing gdal 2.2.4 through pip:

sudo apt-add-repository ppa:ubuntugis/ubuntugis-unstable
sudo apt update
sudo apt install libgdal-dev

Before that, I was getting extensions/gdal_wrap.cpp:3172:27: fatal error: cpl_vsi_error.h: No such file or directory, even when including the correct "include" path to pip.

The the pip installation (in a virtualenv):

 pip install --global-option=build_ext --global-option="-I/usr/include/gdal" gdal
Scott Staniewicz
  • 712
  • 6
  • 14
-3

try to do: brew install gdal

after that try again.

Yariv Katz
  • 1,353
  • 1
  • 17
  • 24
  • This answer would be much more helpfull if you explained OP what is wrong in his case and in what way your snippet solves the problem, and what are the prerequisites for brew to work. – jb. Nov 05 '14 at 20:29
  • `brew` is Mac-only whilst the OP uses Ubuntu – AlessioX Jul 21 '17 at 13:23