Getting Python GeoIP working on Amazon EC2

2 Feb

Amazon EC2 doesn’t allow easy installation of a “python-geoip” for GeoIP usage with Python, so you have to do this manually.First, we need to install the GeoIP library itself – Amazon EC2 allows for this:

sudo yum -y install GeoIP

But before you can do anything, you need to install gcc and python-devel packages to compile and build this:

sudo yum -y install gcc python-devel GeoIP-devel

Then download and install the latest version of the Python code from Maxmind:

wget http://geolite.maxmind.com/download/geoip/api/python/GeoIP-Python-1.2.7.tar.gz
tar xzvf GeoIP-Python-1.2.7.tar.gz
cd GeoIP-Python-1.2.7

Then build and install the python GeoIP libraries.

python setup.py build
sudo python setup.py install

That’s it! You should have a working Python+GeoIP package in your system! You can test it out as follows:

python test.py

Now, if you want the city-level precision working, you will need to download the city data from Maxmind:

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
sudo mv GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

And then you can run the city-level geo-targeting.

python test_city.py

Note: you may need to edit the GeoIP file open line in test_city.py as follows, since that is where the GeoIPCity file is at:

gi = GeoIP.open("/usr/share/GeoIP/GeoIPCity.dat",GeoIP.GEOIP_STANDARD)

(Updated Feb 6, 2012 to reflect correct python-devel package name, and -y option to remove yum install prompt.)

Advertisements

One Response to “Getting Python GeoIP working on Amazon EC2”

  1. jr July 27, 2014 at 3:37 am #

    # pip install python-geoip
    saves you all the trouble

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: