Skip to content
The Ruby gem for querying's GeoIP database, which returns the geographic location of a server given its IP address
Ruby JavaScript Python CSS HTML
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin Test program was broken on country dbs, Sep 17, 2014
config Eric Wong's patch to use io-extra to handle forking Nov 12, 2009
lib Load the data in memory rather than use a StringIO Apr 28, 2019
script Updated license text in accordance with Maxmind's 1.4.2 release Aug 4, 2013
test Fixed false returns from unassigned regions in ASN and some other dat… May 1, 2015
website (never say final) website tweak to update license text in accordance … Aug 4, 2013
.gitignore Now version 1.5.0, Fixes encoding of city names Mar 10, 2015
History.rdoc Merged cityv6 API call into city Oct 16, 2012
LICENSE Added LICENSE file as copy of original MaxMind v1.4.8 COPYING file, r… Sep 13, 2013
Rakefile GitHub is HTTPS by default Feb 2, 2018
geoip.gemspec Forgot to push new gemspec May 15, 2018




GeoIP searches a GeoIP database for a given host or IP address, and returns information about the country where the IP address is allocated, and the city, ISP and other information, if you have that database version.


Does not support the new Maxmind v2 database formats.

Includes support for ASN data files, thanks to Roland Matiz. This release adds support for timezone names, thanks to Tonni Aagesen.

If you have required 'io/extra' and have IO#pread, cross-process file-descriptor sharing is enabled. Each GeoIP instance keeps the file descriptor open, with a Mutex for thread-safety. You should consider this if your process will fork without exec, as modrails does


require 'geoip'

# Use the country database:
c ='GeoIP.dat').country('')
=> ["", "", 69, "FI", "FIN", "Finland", "EU"]
=> "FIN"
=> {:country_code3=>"FIN", :country_name=>"Finland", :continent_code=>"EU", :request=>"", :country_code=>69, :country_code2=>"FI", :ip=>""}

Returned values are the requested hostname, the IP address as a dotted quad, Maxmind's country code, the ISO3166-1 alpha-2 country code, the ISO3166-2 alpha-3 country code, the ISO3166 country name, and the continent code.

# Use the city database:
c ='GeoLiteCity.dat').city('')
=> ["", "", "US", "USA", "United States", "NA", "CA", "San Francisco", "94110", 37.7484, -122.4156, 807, 415, "America/Los_Angeles"]
>> c.longitude
=> -122.4156
>> c.timezone
=> "America/Los_Angeles"'GeoCity.dat').city('')
=> ["", "", "US", "USA", "United States", "NA", "CA", "San Francisco", "94110", 37.7484, -122.4156, 807, 415, "America/Los_Angeles"]

# Use the city ipv6 database:'GeoLiteCityv6.dat').city('::')
=> ["::", "::", "IT", "ITA", "Italy", "EU", "05", "Piacenza", "", 45.016699999999986, 9.666699999999992, nil, nil, "Europe/Rome"]

Returned values are the requested hostname, the IP address as a dotted quad, the ISO3166-1 alpha-2 country code, the ISO3166-2 alpha-3 country code, the ISO3166 country name, the continent code, the region (state or territory) name, city name, postal_code/zipcode, latitude, longitude, USA DMA code, USA area code, timezone name.

Result arrays from both city and country have mixed-in accessor methods as appropriate: request, ip, country_code, country_code2, country_code3, country_name, continent_code, region_name, city_name, postal_code, latitude, longitude, dma_code, area_code, timezone'GeoIPASNum.dat').asn("")
=> ["AS8342", "RTComm.RU Autonomous System"]

For testing or other purposes, you might wish to treat localhost IP addresses as some other address - an external one. You can do this by assigning the desired external IP address to the attribute local_ip_alias:

g ='GeoIP.dat')"")
=> nil
g.local_ip_alias = """") 
#<struct GeoIP::City request="", ip="", country_code2="US", country_code3="USA", country_name="United States", continent_code="NA", region_name="CA", city_name="Mountain View", postal_code="94043", latitude=37.41919999999999, longitude=-122.0574, dma_code=807, area_code=650, timezone="America/Los_Angeles", real_region_name="California">


You need one of the free GeoLite country, city or ASN binary databases, or a subscription database version. Maxmind have unfortunately stopped updating databases in the original GeoLite format - only v2 is currently available, and the CSV form. There is no available compiler from CSV to the legacy database format, so this gem has started to go stale and needs a path forwards. Any volunteers? The download location for the GeoLite2 databases is <>.

This API requires a gzipped binary file to be decompressed for searching. Other versions of this database are available for purchase which contain more detailed information, but not all this information is returned by this implementation. See for more information.


sudo gem install geoip


This version Copyright (C) 2005 Clifford Heath Derived from the C version, Copyright (C) 2003 MaxMind LLC

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This follows the license applied by Maxmind to their C library, for example in the version here: <>.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

You can’t perform that action at this time.