Register now and start sharing your code snippets.
-->

Geolocation with MaxMind's GeoIP and the geoip-city RubyGem

Ruby posted 8 months ago by christian

Install GeoIP library

   1  cd /usr/local/src/
   2  curl -O http://www.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
   3  tar zxvf GeoIP.tar.gz 
   4  cd GeoIP-1.4.4
   5  
   6  ./configure
   7  make
   8  make check
   9  make install

Install the geoip-city gem

   1  git clone git://github.com/ry/geoip-city.git
   2  
   3  sudo gem install geoip_city -- --with-geoip-dir=/usr/local/src/GeoIP-1.4.4

Test the bindings

   1  curl -O http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
   2  gunzip GeoLiteCity.dat.gz

Fire up IRB and try the following code:

   1  require 'rubygems'
   2  require 'geoip_city'
   3  db = GeoIPCity::Database.new('GeoLiteCity.dat')
   4  result = db.look_up('192.143.34.23')
   5  p result

Another option is to use hostip.info’s database, as described in this article.

Tagged geoip, geolocation, maxmind