Get gdal development libraries:
$ sudo apt-add-repository ppa:ubuntugis/ubuntugis-unstable
$ sudo apt-get update
$ sudo apt-get install libgdal-dev
Create and activate a virtual environment:
$ virtualenv gdalenv
$ source gdal/bin/activate
Download GDAL:
(gdalenv) $ pip install --no-install GDAL
Specify where the headers are:
(gdalenv) $ cd /path/to/gdalenv/build/GDAL
(gdalenv) $ python setup.py build_ext --include-dirs=/usr/include/gdal/
Install GDAL:
(gdalenv) $ pip install --no-download GDAL
Done.
I ran into this issue on OSX High Sierra, trying to install the headers for GDAL 2.2.3 on Python 3.6.5. To resolve it I had to do the following:
$ gdal-config --cflags
which outputted:
-I/Library/Frameworks/GDAL.framework/Versions/2.2/Headers
I then had to use this to set the CFLAG env variable:
$ export CFLAGS='-I/Library/Frameworks/GDAL.framework/Versions/2.2/Headers'
Then installed via pip with flags from the above answers
$ pip install GDAL==$(gdal-config --version) --global-option=build_ext --global-option="-L/Library/Frameworks/GDAL.framework/Versions/2.2/GDAL -I/Library/Frameworks/GDAL.framework/Versions/2.2/GDAL -lgdal"
and it finally installed.