Wednesday, September 21, 2011

Installing GDAL in a Python Virtual Environment

Python virtual environments are great for managing package versions, but installing Python packages that have shared library dependencies can be a pain. It seems that ever since I have been using Python virtual environments, I have been trying to figure out how to install the GDAL (the geospatial workhorse) Python bindings in a virtual environment.

Well today, I finally succeeded. The following recipe seems to work for GDAL 1.8.1. It assumes that you have virtualenv and the virtualenvwrapper already installed.

(1) Build and install GDAL with Python bindings on your system

mkdir ~/src/gdal 
cd ~/src/gdal 
tar xzf gdal-$GDAL_VER.tar.gz 
cd gdal-$GDAL_VER 
./configure --prefix=$GDAL_PATH --with-geos=$GEOS_PATH/bin/geos-config --with-python >& log_configure.out 
make >& log_make.out 
make install >& log_make_install.out 
sudo sh -c "echo $GDAL_PATH'/lib' > /etc/" 
sudo ldconfig 

(2) Create a Python Virtual Environment

mkvirtualenv --no-site-packages osgeo-test 

(3) Create a symbolic link from within the virtual environment to gdal-config

This is needed because the file of the GDAL Python bindings requires gdal-config.
# create a symbolic link in the virtual environment to the specific version of GDAL that you want to use 
(osgeo-test)$ ln -s ~/src/gdal/gdal-1.8.1/apps/gdal-config ~/.virtualenvs/osgeo-test/bin/gdal-config

(4) Download the GDAL Python package using PIP

Download, but don't install, since we need to configure the build extension parameters.
(osgeo-test)$ pip install --no-install GDAL

(5) Set the build extension parameters using

(osgeo-test)$ cd ~/.virtualenvs/osgeo-test/build/GDAL 
(osgeo-test)$ python build_ext --gdal-config=/usr/local/gdal/1.8.1/bin/gdal-config --library-dirs=/usr/local/gdal/1.8.1/lib --libraries=gdal --include-dirs=/usr/local/gdal/1.8.1/include

(6) Install the configured package using PIP

(osgeo-test)$ cd 
(osgeo-test)$ pip install --no-download GDAL

(7) test it out

(osgeo-test)$ cd 
(osgeo-test)$ python 
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from osgeo import gdal 
>>> gdal.__version__ 


Thanks to Hobu for pushing out a new version of the GDAL Python bindings to PyPI that made this possible.



  1. This comment has been removed by a blog administrator.

  2. Just wanted to add that as of June 2012, pip install GDAL actually seems to work well.

  3. If I try to compile GDAL using the --with-python option, it picks up my system default python (2.4), not the virtualenv, or another version I manually installed. Is there a way to change this?

  4. If using GDAL from repo, it could be you have to use the corresponding gdal-Version. E.g. use $ pip install --no-install GDAL==1.8.1

  5. Thank you! It's very useful post.

  6. I get an error at the last step:
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

  7. Thanks for sharing amazing information about python .Gain the knowledge and hands-on experience in python Online Training