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

GDAL_VER=1.8.1 
GDAL_PATH=/usr/local/gdal/$GDAL_VER 
GEOS_VER=3.2.2 
GEOS_PATH=/usr/local/geos/$GEOS_VER 
mkdir ~/src/gdal 
cd ~/src/gdal 
wget http://download.osgeo.org/gdal/gdal-$GDAL_VER.tar.gz 
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/ld.so.conf.d/gdal.conf" 
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 setup.py 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 setup.py

(osgeo-test)$ cd ~/.virtualenvs/osgeo-test/build/GDAL 
(osgeo-test)$ python setup.py 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__ 
'1.8.1' 
>>>

Thanks

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

References

http://openblockproject.org/docs/install/common_install_problems.html