18

I am developing some code in Python, and I want to use the GDAL/OGR python bindings. What is the minimum I need to install in order to use the GDAL and OGR python bindings?

What is the easiest way to install GDAL/OGR if I only want to use it from Python?
I'm looking for an answer that applies to both mac and windows.

GDAL is listed in the Python Package Index (PyPi) but I don't understand how to install its dependencies (libgdal and the header files for both libgdal and numpy). Maybe all I need to do is install numpy and then get libgdal somewhere, and if so, where? Does a windows install need the Windows Binaries in addition to libgdal and it's header files?

How can I ensure that I get the proper python bindings for my version of Python (2.5, 2.6, 2.7, 3)?
Must changes be made to the PATH environment variable in order to use GDAL and OGR from Python, or can everything be accessed through my site-packages folder and a typical import statement?

These questions are relevant to this question but do not answer it:
installing GEOS, PROJ, GDAL/OGR into a python virtualenv on Mac OS X
How to install GDAL with Python on windows?

I should note that FWTools, OSGeo4W, and kyngchaos offer ogr and gdal with python bindings and are excellent, but FWTools and OSGeo4W ship with their own python, rather than acting as libraries for an existing python installation, and the kyng chaos frameworks appear to be for OS X only.

3
  • "kyng chaos frameworks appear to be for unix only." I think you misunderstand, they are for OS X.
    – Sean
    May 12, 2011 at 18:55
  • 2
    See gis.stackexchange.com/questions/2276/… for the Windows Install. I'm personally a big fan of lfd.uci.edu/~gohlke/pythonlibs as they are Win64 and recent
    – Mike T
    May 12, 2011 at 22:28
  • 3
    I'm revisiting this 3 years later. In short, I can see that the problem is that python library installs tend to do a poor job of dealing with dependencies written in C. KyngChaos, FWTools, Anaconda, etc. all do the work of making platform specific C binaries that can be used with the version of Python they have. Basically no one has create a cross platform way to just pip install gdal and have it automatically include and compile all the necessary files from GDAL. It's expected that you will use some other means to create the GDAL binaries. Sep 4, 2014 at 22:14

8 Answers 8

28

Only adding this because I tried using the kyng chaos tools, but on my Mac OS X machine I was able to very, very easily install this with Anaconda

conda install gdal

Posting in case anyone finds this again - I realize the original post is 3 years old.

6
  • 1
    I used Anaconda and it was simple for GDAL, but now I'd like to be able to run ogr2ogr -clipdst -105.53 39.9 -104.93 40.27 places.json ne_10m_admin_0_map_subunits.shp and it seems that I need to enable GEOS from a .configure file - any idea where Anaconda places this file? trac.osgeo.org/geos
    – blehman
    Sep 3, 2014 at 16:09
  • I appreciate the note, but this is similar to the KyngChaos binaries, in that it still doesn't allow you to use an existing python installation, and you must use the version of python shipped with Anaconda. Sep 4, 2014 at 22:11
  • I couldn't get the KyngChaos stuff to work though, hence adding the idea. But Anaconda lets you pick which version of Python to install, so...I don't know, I guess you have a reason for needing the other specific installation to be used?
    – mmallek
    Sep 5, 2014 at 16:23
  • 1
    A Christmas miracle finding this answer some 2+ hours into trying to transfer my (working) set up from PC to Mac.
    – ScottieB
    Dec 26, 2016 at 22:57
  • After having to do an environment reset, this no longer works for me. I get an error relating to from osgeo import ogr
    – ScottieB
    Feb 21, 2017 at 15:36
6

First if you are on windows,then there are straight away installers,on the link told my friends above.But if you are on Linux this is the way for installing libgdal 1.11.

go to this page

http://packages.ubuntu.com/trusty/libgdal1h

download the required .deb file choosing your system architecture.

after downloading it go to downloaded directory and run this command

$ sudo dpkg -i libgdal1h_1.10.1+dfsg-5ubuntu1_amd64.deb 

Now you will get an error telling dependencies are not installed. now type this command

$ sudo apt-get -f install

It installs all the dependencies automatically now re run the main installation command.

$ sudo dpkg -i libgdal1h_1.10.1+dfsg-5ubuntu1_amd64.deb 

It will now install happily.Now we need to install developer libraries for gdal.

$ sudo apt-get install libgdal1-dev 

export CPLUS_INCLUDE_PATH=/usr/include/gdal

export C_INCLUDE_PATH=/usr/include/gdal

Next if you have numpy,you can proceed to GDAL python library installation,else install numpy,for installing numpy and scipy on linux use

$ sudo apt-get install python-numpy python-scipy

now using pip install GDAL library

$ sudo pip install GDAL
1
  • It is thanks to this answer (reminds me the two 'export' lines that I had forgotten) that I was able to install GDAL in a virtualenv : (venv)mgc@mgc:~$ export CPLUS_INCLUDE_PATH = /usr/include/gdal (venv)mgc@mgc:~$ export C_INCLUDE_PATH = /usr/include/gdal (venv)mgc@mgc:~$ pip3 install gdal==1.11.2 (or the version number corresponding to the gdal library installed on your computer).
    – mgc
    Aug 14, 2015 at 17:05
6

I had this exact problem when installing GDAL/OGR alongside the ArcGIS python install. My solution for windows was:

  1. Download binaries of gdal and python bindings from Link. This includes versions of the python bindings for different versions of python.
  2. Update the paths in the python file manually:

os.environ['PATH'] = "C:/Program Files (x86)/GDAL/;" + os.environ['PATH']

oldpaths = sys.path

sys.path = ["C:/Program Files (x86)/GDAL/python/",]

sys.path.extend(oldpaths)

from osgeo import ogr

from osgeo import osr

from gdalconst import *

from osgeo import gdal

This isn't very pretty but allows you to mix different installations. You also know explicitly what you are loading.

Edit: fixed syntax error

0
5

There are several tutorials on this site, including a way to get your environment set up. I use pyscripter over crimson editor, but either should work.

4

I'm not aware of an easy way to install it on OS X, I think Kyng Chaos is the best thing going, as is OSGEO4W on the Windows front. The Python bindings are just wrappers to the actual C/C++ code, you can't get away without installing them either from source or through binaries.

Pip or easy_install can do the trick, depending on the Python version and system libraries present; for example here's the PyPI directory for Python 2.5. Getting GDAL installed consistently across multiple platforms and multiple versions of Python remains non-trivial, but in many ways that makes sense: its perhaps the one library which underlies most open source and many proprietary GIS software, and has a large number of data format dependencies below it.

3
  • As regards making sure things don't break, you might want to look at virtualenv and virtualenvwrapper to help keep things tidy. May 12, 2011 at 4:04
  • PyPI has directories for 2.4 (win32 only, with GDAL 1.5, 1.6), 2.5 (mac/win32 with GDAL 1.5, 1.6) and 2.6 (win32 only, with GDAL 1.6). None have GDAL 1.7. If I understand the gist of the answer correctly, you're mainly saying it's not easy and that pip might work in some cases. Should I be looking into how to build it from source? May 12, 2011 at 19:36
  • @BenjaminGolder: right, in my experience it can be tricky, especially if you're trying to support many platforms / and Python versions. Building from source takes more work up front but may be worth the time if you're trying to develop a consistent set of instructions. You may want to also ask your question on the GDAL list which is read by the developers themselves.
    – scw
    May 15, 2011 at 5:43
4

Use this if you don't care ppa addition,

$ sudo apt-add-repository ppa:ubuntugis/ubuntugis-unstable

$ sudo apt-get update

$ sudo apt-get install python-gdal


2019-04-19: Adding on a ubuntu solution that worked out for me on a cloud ubuntu server, from https://stackoverflow.com/a/41613466/4355695

sudo add-apt-repository -y ppa:ubuntugis/ppa
sudo apt update 
sudo apt install gdal-bin python-gdal python3-gdal

I'm not the original poster of this answer but this question is locked so have to edit an existing one. This source (ppa:ubuntugis/ppa) is probably less "unstable" than the previous one suggested, and worked out fine for a python2 program I was trying to get to run on my server.

1

If you are a windows user, you can find plenty usefull binaries , I am working with these and they are working very well... Good luck, cheers

1
  • 1
    Welcome to GIS@SE, it is better to provide more information with your answer, for example, specific binaries and steps for installation, this makes the information a lot more useful to the person asking the question as well as future people who view the question and answers. Aug 26, 2014 at 1:11
0

On CentOS:

yum install gdal-python

Not the answer you're looking for? Browse other questions tagged or ask your own question.