Install GMP Math Library & PHP GMP Extension
In this article
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
- Update your package lists (Debian/Ubuntu):
sudo apt update
For RHEL/CentOS/Fedora, usesudo yum updateorsudo dnf update. - Install the GMP library (development files included):
sudo apt install libgmp-dev
On RHEL/CentOS/Fedora:sudo yum install gmp-develorsudo dnf install gmp-devel. This provides the core library and headers needed to compile the PHP extension. - Install the PHP GMP extension using your package manager (recommended):
sudo apt install php-gmp
On RHEL/CentOS/Fedora:sudo yum install php-gmporsudo dnf install php-gmp. This automatically installs the compiled extension and enables it. - If you are compiling PHP from source, run the configure script with the
--with-gmpflag before building. For example:./configure --with-gmp
Then runmakeandmake installas usual. - 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. - Verify the extension is active by running:
php -m | grep gmp
You should seegmpin 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-devfor the library andphp-gmpfor the extension. On RHEL/CentOS, it’sgmp-develandphp-gmp. - Not matching PHP version: If you have multiple PHP versions, ensure the extension matches your active version. Use
php -vto 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 gmpto your Dockerfile after installinglibgmp-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 likeextension=gmpin/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.