[GIS] Install GDAL Python binding on mac

gdal

When I try to install gdal (on OSX 10.11.6) with pip install gdal (or easy_install) the installation fails with following error

extensions/gdal_wrap.cpp:3085:10: fatal error: 'cpl_port.h' file not found
#include "cpl_port.h"

According to this thread https://stackoverflow.com/questions/37700484/python-gdal-does-not-install-on-mac-osx-el-capitan I added the location of the header file to my path variable.
My .bash_profile now looks like this

export PATH=$PATH:/Library/Frameworks/GDAL.framework/Programs
export PATH=$PATH:/Library/Frameworks/GDAL.framework/Headers

The error remains the same.

Then I tried following this solution: Python GDAL package missing header file when installing via pip although I do not want to install it in a virtualenv.

After setting CPLUS_INCLUDE_PATH and C_INCLUDE_PATH I ran into following error:

    clang++ -bundle -undefined dynamic_lookup -Wno-error=unused-command-line-argument-hard-error-in-future build/temp.macosx-10.11-x86_64-2.7/extensions/gdal_wrap.o -L/usr/local/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/sqlite/lib -L/Library/Frameworks/GDAL.framework/Versions/2.1/lib -lgdal -o build/lib.macosx-10.11-x86_64-2.7/osgeo/_gdal.so
ld: warning: directory not found for option '-L/Library/Frameworks/GDAL.framework/Versions/2.1/lib'
ld: library not found for -lgdal
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'clang++' failed with exit status 1

Any idea how to fix the errors and get a working installation?

Best Answer

You received the error because the header file was not included in your GDAL build. In order to get all of the required headers, you need to use the latest development branch of GDAL, as it added another header ~3 weeks ago. Set your CC and CXX environmental variables to clang and clang++, then install using Homebrew:

brew install gdal --HEAD

If you try to build GDAL with gcc-8 instead, it will throw syntax errors. As of June 2018 this provides the development branch of GDAL 2.3.0. To install gdal for your existing Python (e.g., Anaconda Python 3.6) and link to GDAL, follow the method of @Holt, but with the below modifications.

$ cd /private/tmp 
$ pip download GDAL==2.3
$ tar xzf GDAL-2.3.0.tar.gz
$ cd GDAL-2.3.0

Now, set CC and CXX to gcc-8 and g++-8. Next, install gcc-8 with Homebrew if not currently installed: brew install gcc. Then, make the following change to the setup.cfg file:

gdal_config=/usr/local/bin/gdal-config

gdal-config contains all of the information needed to link to include and library files. Next, make the following modifications to the setup.py file:

cxx11_flag='-std=c++11 -stdlib=libc++'
os.environ['ARCHFLAGS'] = ''
name='gdal'

This sets ARCHFLAGS to blank rather than '-Wunused-command-line-argument-hard-error-in-future', as the latter is incompatible with gcc. Setting stdlib should help clang find the correct C++11 libraries, if used. Last, changing the name from GDAL to gdal lists the package name correctly under conda list. Finally, install the Python package correctly linked to GDAL 2.3.0:

python setup.py install

As @Holt noted, be sure to load and test the install in another folder.

Rant: It's crazy that this much effort is needed to install gdal for Anaconda Python 3.6 linked to a centralized GDAL 2.3.0 installation, but that is because the conda-forge gdal folks intentionally broke the connection to Homebrew GDAL ~2 years ago in an effort to equally [not] support MacPorts. This is nuts in my opinion, as others also build from source and managing multiple GDAL installations is a pain. The GDAL package for R, rgdal, correctly links to Homebrew GDAL during installation without any hassle. The GDAL library for Anaconda Python should also do this!