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

@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