Skip to content

Instantly share code, notes, and snippets.

@cspanring
Last active August 24, 2024 12:07
Show Gist options
  • Save cspanring/5680334 to your computer and use it in GitHub Desktop.
Save cspanring/5680334 to your computer and use it in GitHub Desktop.
Installing GDAL in a Python virtual environment

Installing GDAL in a Python virtual environment

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.


Source: http://gis.stackexchange.com/questions/28966/python-gdal-package-missing-header-file-when-installing-via-pip

@keithfma
Copy link

keithfma commented Oct 6, 2018

@basaks solution worked for me on Ubuntu 18.04 -- thank you for curing this headache!

@v67bruno
Copy link

v67bruno commented Oct 24, 2018

For the error bellow, i solved adding GDAL lib (gdal-config --libs) and include (gdal-config --cflags) files path: --global-option="-I/usr/include/gdal -L/usr/local/lib -lgdal"

sudo add-apt-repository -y ppa:ubuntugis/ubuntugis-unstable
sudo apt update
sudo apt upgrade
sudo apt install gdal-bin libgdal-dev libgdal1i
python -m pip install GDAL==$(gdal-config --version) --global-option=build_ext --global-option="-I/usr/include/gdal -L/usr/local/lib -lgdal"
extensions/gdal_wrap.cpp: In function ‘retStringAndCPLFree* wrapper_VSIGetSignedURL(const char*, char**)’:
    extensions/gdal_wrap.cpp:4067:48: error: ‘VSIGetSignedURL’ was not declared in this scope
         return VSIGetSignedURL( utf8_path, options );
                                                    ^
    extensions/gdal_wrap.cpp: In function ‘void GDALDatasetShadow_ResetReading(GDALDatasetShadow*)’:
    extensions/gdal_wrap.cpp:4895:35: error: ‘GDALDatasetResetReading’ was not declared in this scope
         GDALDatasetResetReading( self );
                                       ^
    extensions/gdal_wrap.cpp: In function ‘OGRFeatureShadow* GDALDatasetShadow_GetNextFeature(GDALDatasetShadow*, bool, bool, OGRLayerShadow**, double*, GDALProgressFunc, void*)’:
    extensions/gdal_wrap.cpp:4913:63: error: ‘GDALDatasetGetNextFeature’ was not declared in this scope
                                           callback, callback_data );
                                                                   ^
    extensions/gdal_wrap.cpp: In function ‘void GDALRasterBandShadow_GetActualBlockSize(GDALRasterBandShadow*, int, int, int*, int*, int*)’:
    extensions/gdal_wrap.cpp:5140:89: error: ‘GDALGetActualBlockSize’ was not declared in this scope
         *pisvalid = (GDALGetActualBlockSize(self, nXBlockOff, nYBlockOff, pnxvalid, pnyvalid) == CE_None);
                                                                                             ^
    extensions/gdal_wrap.cpp: In function ‘int GDALRasterBandShadow_GetDataCoverageStatus(GDALRasterBandShadow*, int, int, int, int, int, double*)’:
    extensions/gdal_wrap.cpp:5370:52: error: ‘GDALGetDataCoverageStatus’ was not declared in this scope
                                              pdfDataPct);
                                                        ^
    extensions/gdal_wrap.cpp: In function ‘GDALDatasetShadow* ApplyVerticalShiftGrid(GDALDatasetShadow*, GDALDatasetShadow*, bool, double, double, char**)’:
    extensions/gdal_wrap.cpp:6058:63: error: ‘GDALApplyVerticalShiftGrid’ was not declared in this scope
                                                           options );
                                                                   ^
    extensions/gdal_wrap.cpp: In function ‘GDALDriverShadow* IdentifyDriverEx(const char*, unsigned int, char**, char**)’:
    extensions/gdal_wrap.cpp:6268:63: error: ‘GDALIdentifyDriverEx’ was not declared in this scope
                                                     sibling_files );
                                                                   ^
    extensions/gdal_wrap.cpp: In function ‘PyObject* _wrap_GetErrorCounter(PyObject*, PyObject*)’:
    extensions/gdal_wrap.cpp:7465:33: error: ‘CPLGetErrorCounter’ was not declared in this scope
         result = CPLGetErrorCounter();
                                     ^
    extensions/gdal_wrap.cpp: In function ‘PyObject* _wrap_MkdirRecursive(PyObject*, PyObject*)’:
    extensions/gdal_wrap.cpp:8553:57: error: ‘VSIMkdirRecursive’ was not declared in this scope
           result = VSIMkdirRecursive((char const *)arg1,arg2);
                                                             ^
    extensions/gdal_wrap.cpp: In function ‘PyObject* _wrap_RmdirRecursive(PyObject*, PyObject*)’:
    extensions/gdal_wrap.cpp:8623:52: error: ‘VSIRmdirRecursive’ was not declared in this scope
           result = VSIRmdirRecursive((char const *)arg1);
                                                        ^
    extensions/gdal_wrap.cpp: In function ‘PyObject* _wrap_GetActualURL(PyObject*, PyObject*)’:
    extensions/gdal_wrap.cpp:8788:58: error: ‘VSIGetActualURL’ was not declared in this scope
           result = (char *)VSIGetActualURL((char const *)arg1);
                                                              ^
    extensions/gdal_wrap.cpp: In function ‘PyObject* _wrap_GetFileSystemsPrefixes(PyObject*, PyObject*)’:
    extensions/gdal_wrap.cpp:8962:51: error: ‘VSIGetFileSystemsPrefixes’ was not declared in this scope
           result = (char **)VSIGetFileSystemsPrefixes();
                                                       ^
    extensions/gdal_wrap.cpp: In function ‘PyObject* _wrap_GetFileSystemOptions(PyObject*, PyObject*)’:
    extensions/gdal_wrap.cpp:9026:66: error: ‘VSIGetFileSystemOptions’ was not declared in this scope
           result = (char *)VSIGetFileSystemOptions((char const *)arg1);
                                                                      ^
    extensions/gdal_wrap.cpp: In function ‘PyObject* _wrap_VSISupportsSparseFiles(PyObject*, PyObject*)’:
    extensions/gdal_wrap.cpp:9833:62: error: ‘VSISupportsSparseFiles’ was not declared in this scope
           result = (int)VSISupportsSparseFiles((char const *)arg1);
                                                                  ^
    extensions/gdal_wrap.cpp: In function ‘PyObject* _wrap_VSIFGetRangeStatusL(PyObject*, PyObject*)’:
    extensions/gdal_wrap.cpp:9937:55: error: ‘VSIFGetRangeStatusL’ was not declared in this scope
           result = (int)VSIFGetRangeStatusL(arg1,arg2,arg3);
                                                           ^
    extensions/gdal_wrap.cpp: In function ‘PyObject* _wrap_VSICurlClearCache(PyObject*, PyObject*)’:
    extensions/gdal_wrap.cpp:10092:25: error: ‘VSICurlClearCache’ was not declared in this scope
           VSICurlClearCache();
                             ^
    error: command 'gcc' failed with exit status 1

@pyeprog
Copy link

pyeprog commented Feb 18, 2019

@basaks made my day too!! thanks a lot basaks

@jacksonjos
Copy link

@basaks I really appreciate your comments.
Helped me a lot! =)

@zackespry
Copy link

zackespry commented Apr 25, 2019

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.

@mssobhan
Copy link

mssobhan commented Sep 4, 2019

Earlier I was getting the following error.

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/osgeo/_gdal.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib
  Referenced from: /usr/local/opt/gdal/lib/libgdal.20.dylib
  Reason: image not found

for the following import

import gdal

Just create the directory in the desired location

My installation was here...

/usr/local/Cellar/openssl/1.0.2s/

I created a directory at their desired location. openssl directory was not there, I mkdir ed it.

/usr/local/opt/openssl/

Then copied the folder as required. Now

import gdal

works.

@snowman907
Copy link

What is the equivalent procedure to install GDAL API on a virtualenv in Windows?
thanks

@samkellerhals
Copy link

samkellerhals commented Apr 12, 2020

pip install GDAL==$(gdal-config --version) --global-option=build_ext --global-option="-I/usr/include/gdal"

This also just worked for me on Pop OS 19.10.

Thanks @basaks !

@SuperElectron
Copy link

This works:
https://gis.stackexchange.com/questions/28966/python-gdal-package-missing-header-file-when-installing-via-pip/74060#74060?newreg=6b1e677d9dfe4652a3512380e6e73819

sudo apt-get install libgdal-dev

export CPLUS_INCLUDE_PATH=/usr/include/gdal
export C_INCLUDE_PATH=/usr/include/gdal

pip install GDAL==2.4.2
  • use a version with GDAL installation, otherwise you gcc errors.

@raeesaroj
Copy link

raeesaroj commented Jun 1, 2020

I am able to run with this one
pip install --global-option=build_ext --global-option="-I/usr/include/gdal" GDAL==gdal-config --version

@gt-novelt
Copy link

I am able to run with this one
pip install --global-option=build_ext --global-option="-I/usr/include/gdal" GDAL==gdal-config --version

In case the backquote is not obvious:

pip install --global-option=build_ext --global-option="-I/usr/include/gdal" GDAL==`gdal-config --version`

@rafatieppo
Copy link

Thanks @basaks, I just used pip3 instead:

pip3 install GDAL==$(gdal-config --version) --global-option=build_ext --global-option="-I/usr/include/gdal"

@D8989
Copy link

D8989 commented Jun 20, 2021

i use python 3.8 and install gdal 3.0.4 with @rafatieppo solution (but with pip)
pip install GDAL==$(gdal-config --version) --global-option=build_ext --global-option="-I/usr/include/gdal"

PS: Before run pip, i install libgdal-dev with sudo apt install

@ausnet-bwang-zz
Copy link

I had to do

apt-get update
apt-get install libgdal-dev python3.8-dev -y
pip install GDAL==$(gdal-config --version) --global-option=build_ext --global-option="-I/usr/include/gdal" 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment