Install MaxMind GeoIP C Extension for PHP (10–50x Faster)

If you’re using MaxMind for IP geolocation in PHP, there’s a simple way to make it dramatically faster.

By installing the official MaxMind C extension, you can speed up lookups by 10 to 50 times compared to the default PHP reader — with just a few extra steps.

All the self-hosted AltumCode PHP scripts use MaxMind GeoIP databases for tracking, so it only made sense to make it as fast as possible.

In this guide, I’ll walk you through the step-by-step installation and activation of the MaxMind C extension on an Ubuntu server — specifically tested on Ubuntu 24.04.

So if you use any AltumCode product or integrate the MaxMind library into your own PHP projects, this performance boost will benefit both you and your users.

Why use the MaxMind C extension? 🤔

The drop-in default PHP reader provided by MaxMind works perfectly fine and will do a wonderful job for the many use cases.

However, if you use it for analytics tracking and/or you have many visitors that will constantly trigger IP lookups – then it would be wise to integrate the C extension.

If you are using the MaxMind\Db\Reader in PHP loaded via composer – then you do not need to rewrite any of your code.

The C extension is a drop-in replacement – just install it and load it as an extension in PHP and you’re automatically benefiting from the speed improvements.

Prerequisites ⚙️

Before the installation of the MaxMind C extension, let’s make sure you meet the requirements.

✅ Ubuntu 20.04 or newer (This guide was tested on Ubuntu 24.04).

✅ You need root & terminal access to your server.

✅ PHP 7.4 or newer with access to the PHP.ini file

✅ maxmind-db/reader loaded via composer

Installation ⚡️

Those are the exact steps I followed to install and enable the MaxMind C extension on my Ubuntu 24.04 server.

It should work similarly or the same on other recent Ubuntu versions as well.

Required build tools installation

These are the needed dependencies that will help with the building and compilation of the C extension.

sudo apt update
sudo apt install -y \
    git \
    build-essential \
    cmake \
    autoconf \
    automake \
    libtool \
    pkg-config \
    php-dev \
    php-pear

Build & install the MaxMind C library

Now we need to clone the their provided library – libmaxminddb and compile it.

cd ~
git clone https://github.com/maxmind/libmaxminddb.git
cd libmaxminddb

./bootstrap
./configure --disable-tests
make
sudo make install
sudo ldconfig

Build & install the PHP extension

It’s now time to install the generate the PHP extension from maxmind/MaxMind-DB-Reader-php

cd ~
git clone https://github.com/maxmind/MaxMind-DB-Reader-php.git
cd MaxMind-DB-Reader-php/ext

phpize
./buildconf
./configure
make
sudo make install

This will create the maxminddb.so extension which will be loaded & used by PHP.

Enable the PHP extension

You can now simply enable it by loading it via your php.ini file.

Add the following line at the end of your php.ini file:

extension=maxminddb.so

If you want to do it automatically, you can run these commands in the terminal for PHP 8.4:

echo "extension=maxminddb.so" | sudo tee -a /etc/php/8.4/cli/php.ini
echo "extension=maxminddb.so" | sudo tee -a /etc/php/8.4/fpm/php.ini
echo "extension=maxminddb.so" | sudo tee -a /etc/php/8.4/apache2/php.ini

Simply replace 8.4 with your PHP version if you use any other version.

Restart your server

To apply all the new changes, simply restart your apache or nginx server.

Apache server

sudo systemctl restart apache2

Nginx server

sudo systemctl restart nginx

Verify maxminddb extension

The only thing left to do is to check if all the things we did above have worked.

You can do it via the terminal – run the following command and if it shows maxminddb then it is ready to go.

php -m | grep maxminddb

If you do not see anything after running this command, it is possible that you did not add the extension=maxminddb.so in the right php.ini file.

🧪 PHP Benchmark Script

Here are the before and after results based on the following PHP script, which I ran before the MaxMind C extension loaded into PHP and after.

This is the code I ran on the same machine:

<?php

/* No timeout */
set_time_limit(0);

/* Require Composer autoload */
require 'vendor/autoload.php';

use MaxMind\Db\Reader;

/* Set path to your .mmdb file */
$maxmind_database_path = '/path/to/GeoLite2-City.mmdb';
$sample_ip_address = '8.8.8.8';

/* Load the MaxMind database reader */
$maxmind_reader = new Reader($maxmind_database_path);

$lookup_count = 100000;
$start_time = microtime(true);

for ($i = 0; $i < $lookup_count; $i++) {
    $maxmind_reader->get($sample_ip_address);
}

$end_time = microtime(true);
$total_duration = $end_time - $start_time;

echo "Total time for {$lookup_count} lookups: {$total_duration} seconds" . PHP_EOL;
echo "Average per lookup: " . ($total_duration / $lookup_count * 1000) . " ms" . PHP_EOL;

?>

As you can see from the code above, I am using the City database – so if you are using the Country only database, results may vary.

Of course, the results will also depend on the performance of the server.

📊 Before & after performance

In my specific case, it shows an average of over 80x speed improvement ✅.

StateTotal time for 100K lookupsAvg. time per lookup
Before (PHP reader)45.02 seconds0.4502 ms
After (C extension)0.54 seconds0.0054 ms

Wrapping up 🫡

Congratulations! You just implemented the GeoIP C Extension library in your PHP extensions – one of the fastest and easiest performance gains you can implement.

This change will significantly improve your IP lookups on your PHP scripts that implement MaxMind GeoIP library – or any AltumCode product you may use.

Once installed, you do not need to do anything else – no code changes or hassle.

If you have any questions, feel free to comment or get in touch 👋.

Leave a Comment