User Agents

Web browsers and everyday web tech, explained

Install GMP Math Library & PHP GMP Extension

· 4 min read · Extensions

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through installing the GMP (GNU Multiple Precision) math library and the PHP GMP extension, which enables arbitrary-precision arithmetic in PHP. You’ll need this for handling very large integers or fractions beyond PHP’s native float or integer limits—common in cryptography, number theory, and certain scientific calculations.

What You'll Need

  • A Linux server or local machine (Ubuntu/Debian or RHEL/CentOS/Fedora) with sudo/root access.
  • PHP installed (any version 7.x or 8.x).
  • Basic comfort with the command line.

Step-by-Step

  1. Update your package lists (Debian/Ubuntu):
    sudo apt update
    For RHEL/CentOS/Fedora, use sudo yum update or sudo dnf update.
  2. Install the GMP library (development files included):
    sudo apt install libgmp-dev
    On RHEL/CentOS/Fedora: sudo yum install gmp-devel or sudo dnf install gmp-devel. This provides the core library and headers needed to compile the PHP extension.
  3. Install the PHP GMP extension using your package manager (recommended):
    sudo apt install php-gmp
    On RHEL/CentOS/Fedora: sudo yum install php-gmp or sudo dnf install php-gmp. This automatically installs the compiled extension and enables it.
  4. If you are compiling PHP from source, run the configure script with the --with-gmp flag before building. For example:
    ./configure --with-gmp
    Then run make and make install as usual.
  5. Restart your web server to load the new extension:
    sudo systemctl restart apache2
    Or for Nginx with PHP-FPM: sudo systemctl restart php-fpm. For CLI usage, the extension loads immediately without restart.
  6. Verify the extension is active by running:
    php -m | grep gmp
    You should see gmp in the output. Alternatively, create a PHP file with <?php phpinfo(); ?> and check for the GMP section.

Common Mistakes

  • Installing the library but not the PHP extension: The GMP library alone doesn’t add GMP functions to PHP. You must install php-gmp (or compile with --with-gmp).
  • Forgetting to restart the web server: PHP extensions won’t load until the server or PHP-FPM is restarted.
  • Using the wrong package name: On Debian/Ubuntu, it’s libgmp-dev for the library and php-gmp for the extension. On RHEL/CentOS, it’s gmp-devel and php-gmp.
  • Not matching PHP version: If you have multiple PHP versions, ensure the extension matches your active version. Use php -v to check.

Tips

  • If you’re on a shared host without sudo, contact your hosting provider—they may already have GMP enabled.
  • For Docker users, add RUN docker-php-ext-install gmp to your Dockerfile after installing libgmp-dev.
  • Check your PHP configuration file (php.ini) to see if the extension is explicitly enabled. On Debian/Ubuntu, the package usually adds a line like extension=gmp in /etc/php/<version>/mods-available/gmp.ini.

Troubleshooting

GMP functions are undefined: First, verify the extension is loaded with php -m | grep gmp. If not, try reinstalling the extension. If you compiled PHP yourself, ensure --with-gmp was used and that the GMP dev files were present during compilation.

Installation fails with “Unable to locate package php-gmp”: This usually means your package lists are outdated. Run sudo apt update first. On some minimal systems, you may need to enable the universe repository (Ubuntu) or EPEL (RHEL/CentOS).

Web server doesn’t show GMP after restart: Check that the web server user has permission to read the extension file. Also, confirm that the correct php.ini is being used—run php --ini to see the loaded configuration file. Sometimes multiple PHP versions have separate ini files.

Compilation errors when building PHP from source: Ensure libgmp-dev (or gmp-devel) is installed before running ./configure. If the configure script can’t find GMP, you may need to specify the path with --with-gmp=/usr (adjust to your installation prefix).

Frequently Asked Questions

What is GMP used for in PHP?

GMP provides functions for arbitrary-precision arithmetic, allowing you to work with integers and rational numbers larger than PHP’s native limits. It’s often used in cryptography, mathematical computations, and scientific applications.

Is GMP available on Windows?

Yes, but the installation differs. On Windows, you typically enable the php_gmp.dll extension in php.ini and ensure the GMP DLL is in your PATH. However, the process is more manual than on Linux.

Can I use GMP without installing the library?

No, the PHP extension requires the underlying GMP library to be installed. The library provides the actual arithmetic functions.

Does GMP slow down PHP?

GMP is optimized for large numbers, but it is slower than native integer operations for small numbers. Only use it when you truly need arbitrary precision.

How do I test if GMP works?

Run php -r "echo gmp_strval(gmp_add('12345678901234567890', '1'));" and you should see the correct sum. If it outputs a number, GMP is working.

What’s the difference between GMP and BC Math?

Both provide arbitrary-precision arithmetic. GMP is generally faster and uses more memory, while BC Math is simpler and available by default on many systems. Choose based on your performance needs and availability.