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

10 comments:

  1. This comment has been removed by the author.

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

    ReplyDelete
  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?

    ReplyDelete
  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

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

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

    ReplyDelete
  7. Thanks for every other excellent post. The place else may just anyone get that kind of info in such an ideal manner of writing? I’ve a presentation subsequent week, and I am at the look for such information. https://python.engineering/3930188-how-to-convert-nonetype-to-int-or-string/

    ReplyDelete