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:

tar xzvf GeoIP-Python-1.2.7.tar.gz
cd GeoIP-Python-1.2.7

Then build and install the python GeoIP libraries.

python build
sudo python install

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


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

gunzip GeoLiteCity.dat.gz
sudo mv GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

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


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

gi ="/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.)


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: Logo

You are commenting using your 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: