How to Install the PHP GeoIP Extension
In this article
The PHP GeoIP extension lets you map an IP address to a geographic location (country, city, etc.) directly in your PHP code. It uses the GeoIP database from MaxMind (now part of the GeoIP2 legacy format). This guide covers installing the legacy geoip extension for PHP 7.x and earlier, as well as the newer geoip2 via Composer for PHP 7.4+ and PHP 8.x. You'll need a Unix-like environment (Linux, macOS) or Windows with the necessary build tools, and you'll need to download the GeoIP database file.
What You'll Need
- A running PHP installation (PHP 5.3+ for the legacy extension, PHP 7.4+ for GeoIP2)
- For the legacy extension:
pecl(PECL) available, and build tools (gcc,make,autoconf) - For GeoIP2: Composer installed
- GeoIP database files (e.g.,
GeoIP.datfor legacy, or the free GeoLite2 databases for GeoIP2)
Step-by-Step
- Check your PHP version and environment – Run
php -vto see your PHP version. If you're on PHP 7.4 or higher, you'll likely use the GeoIP2 library instead of the legacy extension. Also check ifpeclis available by runningpecl version. - Install the legacy PECL extension (for PHP 7.x and earlier) – Run
pecl install geoip. If prompted, provide the path to the GeoIP library (usually/usr/local/libor/usr/lib). After installation, addextension=geoip.soto yourphp.inifile (for Windows, it'sextension=php_geoip.dll). Restart your web server or PHP-FPM. - Download the GeoIP database – For the legacy extension, download the free GeoLite2 Country or City database in the legacy binary format (e.g.,
GeoIP.dat). Place it in a directory like/usr/share/GeoIP/. For GeoIP2, download the GeoLite2-Country.mmdb or GeoLite2-City.mmdb from MaxMind (requires free account). - Configure the legacy extension (optional) – In
php.ini, you can setgeoip.custom_directoryto point to the folder containing the .dat file, e.g.,geoip.custom_directory = /usr/share/GeoIP. - Verify the legacy extension – Create a PHP file with
<?php echo geoip_country_code_by_name('8.8.8.8'); ?>and run it via CLI or browser. You should seeUS. - Install GeoIP2 via Composer (for PHP 7.4+ and PHP 8.x) – Run
composer require geoip2/geoip2in your project directory. This does not require a compiled extension. - Use GeoIP2 in your code – Example:
<?php require 'vendor/autoload.php'; $reader = new GeoIp2\Database\Reader('/path/to/GeoLite2-City.mmdb'); $record = $reader->city('8.8.8.8'); echo $record->country->isoCode; ?>. - Update the database periodically – For both methods, you'll need to download updated database files regularly (e.g., monthly) to keep accuracy.
Common Mistakes
- Using the legacy extension with PHP 8 – The legacy PECL geoip is not compatible with PHP 8; use GeoIP2 instead.
- Forgetting to set the
geoip.custom_directory– The extension may not find the .dat file if it's not in the default path. - Not restarting the web server after adding the extension to php.ini – Changes won't take effect until restart.
- Downloading the wrong database format – The legacy extension needs the binary .dat format, not the .mmdb format.
- Using the free GeoLite2 databases without a license key – MaxMind requires a free account to download them.
Tips
- For production, consider using the GeoIP2 API (via Composer) because it's actively maintained and supports the newer .mmdb format.
- Cache the results of geoip lookups in your application to avoid repeated disk reads.
- If you only need country-level data, use the Country database – it's smaller and faster.
- Test with a known IP like 8.8.8.8 (Google DNS) to verify your setup.
Troubleshooting
- Extension not loading – Check that the extension line in php.ini is correct and that the .so or .dll file exists. Run
php -mto see if geoip is listed. If not, check the error log. - Fatal error: Call to undefined function geoip_country_code_by_name() – This means the extension isn't loaded. Confirm you added the extension line and restarted the server.
- Cannot find GeoIP database – Ensure the .dat file is in the default directory or set
geoip.custom_directorycorrectly. - PECL install fails with 'phpize not found' – Install the PHP development package (e.g.,
php-devon Debian/Ubuntu). - Composer install fails due to missing extensions – GeoIP2 requires the
curlandjsonextensions, which are usually enabled by default.
Frequently Asked Questions
Is the legacy geoip extension still supported?
No, the legacy PECL geoip extension is deprecated and not compatible with PHP 8. Use the GeoIP2 library instead.
Do I need a paid license to use GeoIP databases?
MaxMind offers free GeoLite2 databases (Country and City) that require a free account. The commercial GeoIP2 databases are paid and more accurate.
Can I use the GeoIP extension on Windows?
Yes, the legacy extension has a Windows DLL, but it's outdated. For modern PHP on Windows, use the GeoIP2 Composer package.
How often should I update the GeoIP database?
MaxMind updates the GeoLite2 databases monthly. For accuracy, update at least monthly.
Does GeoIP work with IPv6 addresses?
The legacy extension supports IPv6 if the database includes IPv6 data. GeoIP2 supports both IPv4 and IPv6 natively.
Can I use GeoIP to get the user's city?
Yes, if you use the City database (GeoLite2-City.mmdb) and the GeoIP2 library, you can retrieve city, region, and postal code.