Articles

How to Install the PHP GeoIP Extension

In the examples shown, replace "X.Y" with your app's PHP version (for example, "7.1"). To install a PECL extension for multiple PHP versions, repeat the PECL extension installation for each PHP version.

The GeoIP extension allows you to find the location of an IP address, including the city, state, country, latitude, and longitude.

If you'd like to use the new GeoIP2, MaxMind now offers a pure PHP implementation that doesn't require a C extension: https://github.com/maxmind/GeoIP2-php.

Installing GeoIP on PHP 7.X

To install this extension on PHP 7.0, 7.1, or 7.2, run the following commands as your server's root user:

sudo apt-get -y install gcc make autoconf libc-dev pkg-config
sudo apt-get -y install libgeoip-dev
sudo pecl7.X-sp install geoip-beta

Once installed, create a configuration file for the extension and restart PHP by running the following command as root:

sudo bash -c "echo extension=geoip.so > /etc/php7.X-sp/conf.d/geoip.ini"
sudo service php7.X-fpm-sp restart

Installing GeoIP on PHP 5.X

To install this extension on PHP 5.4, 5.5, or 5.6, run the following commands as your server's root user:

sudo apt-get -y install gcc make autoconf libc-dev pkg-config
sudo apt-get -y install libgeoip-dev
sudo pecl5.X-sp install geoip

Once installed, create a configuration file for the extension and restart PHP by running the following command as root:

sudo bash -c "echo extension=geoip.so > /etc/php5.X-sp/conf.d/geoip.ini"
sudo service php5.X-fpm-sp restart

Verifying GeoIP Is Installed

You can check that the extension was installed with this command:

phpX.Y-sp -i | grep geoip

The output will look like this:

$ php7.1-sp -i | grep geoip
/etc/php7.1-sp/conf.d/geoip.ini,
geoip
geoip support => enabled
geoip extension version => 1.1.1
geoip library version => 1006000
geoip.custom_directory => no value => no value

Last updated: August 31, 2017