When working with large number calculations in PHP, you might encounter:
Fatal error: Call to undefined function gmp_mod()
This occurs when the GMP (GNU Multiple Precision) extension isn't properly installed or enabled in your PHP environment.
For Ubuntu systems (including 10.04 LTS and newer versions), follow these steps:
sudo apt-get update
sudo apt-get install php-gmp # For PHP 7+
# OR for older systems:
sudo apt-get install php5-gmp
After installation, you need to enable the extension in PHP's configuration:
sudo phpenmod gmp # Modern PHP versions
# For older systems:
sudo ln -s /etc/php5/mods-available/gmp.ini /etc/php5/apache2/conf.d/20-gmp.ini
Then restart your web server:
sudo service apache2 restart
# OR for newer systems:
sudo systemctl restart apache2
Create a test PHP file with this content:
<?php
phpinfo();
?>
Search for "gmp" in the output or run:
php -i | grep gmp
You should see GMP support enabled.
Here are some common GMP function usages:
<?php
// Modular arithmetic
$mod = gmp_mod("8", "3"); // 2
// Factorial calculation
$factorial = gmp_fact(5); // 120
// Greatest common divisor
$gcd = gmp_gcd("12", "21"); // 3
// Secure random number generation
$random = gmp_random_bits(256);
?>
- Ensure you installed the package matching your PHP version
- Check for multiple PHP installations (cli vs apache)
- Verify the extension .so file exists in your PHP extension directory
- Check Apache/PHP error logs for loading issues
If package installation isn't working, you can try compiling with:
sudo apt-get install libgmp-dev
pecl install gmp
echo "extension=gmp.so" | sudo tee /etc/php5/mods-available/gmp.ini
When working on a cryptographic implementation that required arbitrary-precision arithmetic, I suddenly encountered this error:
Fatal error: Call to undefined function gmp_mod()
This was particularly frustrating because I had already installed php5-gmp through apt-get. Here's how I solved it and what you need to know about GMP in PHP.
GMP support in PHP requires three components to work properly:
sudo apt-get install libgmp-dev
sudo apt-get install php-gmp
sudo apt-get install php5-gmp # For PHP 5.x systems
After installation, you must enable the extension in php.ini. The location varies:
# For PHP CLI
sudo nano /etc/php/7.x/cli/php.ini
# For Apache module
sudo nano /etc/php/7.x/apache2/php.ini
Add or uncomment this line:
extension=gmp.so
To confirm everything is working:
php -m | grep gmp
php -i | grep gmp
Create a test script:
<?php
echo 'GMP version: ' . gmp_version() . "\n";
$result = gmp_mod("123456", "200");
echo "123456 mod 200 = $result\n";
?>
- Multiple PHP versions installed (cli vs apache2)
- Incorrect php.ini file modified
- Missing libgmp-dev dependency
- Not restarting Apache:
sudo service apache2 restart
Once working, you can leverage GMP's full power:
<?php
// Large number arithmetic
$sum = gmp_add("123456789012345", "98765432109876");
// Cryptographic operations
$prime = gmp_nextprime("100000000000");
// Bitwise operations
$shifted = gmp_div_q("256", "2", GMP_ROUND_MINUSINF);
?>
Remember that GMP functions return resources that need conversion:
$result = gmp_strval(gmp_mod("123456", "200"));