George MacKerron: code blog

GIS, software development, and other snippets

How to compile PostGIS 2.1 on Ubuntu Server 12.04+

PostGIS 2 has some exciting new goodies — including raster support — that I’m keen to use in the analysis of Mappiness data.

But the PostGIS package provided by Ubuntu 12.04 is still only at version 1.5, and the GEOS and GDAL packages are also too old to support the new version.

So — this is how I compiled PostGIS 2.0.1 2.0.3 2.1.0rc2 2.1.0 and its dependencies on my GIS server.

I used checkinstall so as to be able easily to remove these libraries in future. And I installed GEOS and GDAL to /opt to keep them out of the way (I didn’t do the same for PostGIS because of a bug).

cd ~
mkdir -p src
 
 
# First install PostgreSQL 9.2, plus contributed packages and any missing prerequisites
# ===
 
# add the Postgres PPA
echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' \
  | sudo tee /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc \
  | sudo apt-key add -
sudo aptitude update
 
# the following is necessary on 13.04 (and possibly 12.10?)
sudo aptitude install postgresql-common --target-release raring
 
# then
sudo aptitude install postgresql-9.2 postgresql-server-dev-9.2 postgresql-contrib-9.2 \
  build-essential checkinstall libjson0-dev libxml2-dev libproj-dev python2.7-dev swig
 
 
# Now for GEOS, GDAL and PostGIS
# ===
 
# download and compile geos in /opt/geos
cd ~/src/
wget http://download.osgeo.org/geos/geos-3.4.1.tar.bz2
tar xvjf geos-3.4.1.tar.bz2
cd geos-3.4.1/
./configure --prefix=/opt/geos --enable-python
make -j2
sudo checkinstall  # uninstall with: sudo dpkg -r geos
 
# download and compile gdal in /opt/gdal
cd ~/src/
wget http://download.osgeo.org/gdal/1.10.0/gdal-1.10.0.tar.gz
tar xvzf gdal-1.10.0.tar.gz
cd gdal-1.10.0/
./configure --prefix=/opt/gdal --with-geos=/opt/geos/bin/geos-config \
  --with-pg=/usr/lib/postgresql/9.2/bin/pg_config --with-python
make -j2
sudo checkinstall  # uninstall with: sudo dpkg -r gdal
 
# download and compile postgis in default location
cd ~/src/
wget http://download.osgeo.org/postgis/source/postgis-2.1.0.tar.gz
tar xvzf postgis-2.1.0.tar.gz
cd postgis-2.1.0/
./configure --with-geosconfig=/opt/geos/bin/geos-config \
  --with-gdalconfig=/opt/gdal/bin/gdal-config
make -j2
sudo checkinstall  # uninstall with: sudo dpkg -r postgis
 
# for command-line tools:
echo 'export PATH="$PATH:/opt/geos/bin:/opt/gdal/bin:/usr/lib/postgresql/9.2/bin"' >> .bashrc
 
# so libraries are found:
echo '/opt/geos/lib
/opt/gdal/lib' | sudo tee /etc/ld.so.conf.d/geolibs.conf
 
sudo ldconfig
 
# restart postgres
sudo /etc/init.d/postgresql restart
 
# restore a pg_dump -Fc backup from an earlier PostGIS version
echo 'create database mydb;' | sudo -u postgres psql
echo 'create extension postgis; create extension postgis_topology;' \
  | sudo -u postgres psql -d mydb
/usr/share/postgresql/9.2/contrib/postgis-2.1/postgis_restore.pl \
  /path/to/mydb.dump \
  | sudo -u postgres psql -d mydb

Share

Written by George

June 1st, 2012 at 6:30 pm

  • Anon

    very nice post

  • George, you are my hero! This was invaluable for me and my company!!

  • Hmmm… Even after rebuilding the GDAL library and everything as you stated above, when I try the command “ogrinfo –formats”, it still does not have the PostGIS driver installed. Any idea how to install that?? It is crucial that OGR can talk to my PG database!

  • jawj

    Sorry, not sure why this might be.

  • I solved this and detailed the process over in Google Groups. We gave some great credit to this blog. Thanks again. https://groups.google.com/forum/?fromgroups=#!topic/tilestache/1XUlLzNxwcs

  • Marcin Gontarek

    For my instalation of Ubuntu (12.04)
    echo ‘export PATH=/opt/geos/bin:$PATH
    export PATH=/opt/gdal/bin:$PATH
    export PATH=/usr/lib/postgresql/9.2/bin:$PATH’ >> ~/.bashrc

  • Lorenzo Muttoni

    Great post! thanks a lot.