drupal-8php-extensiondrupal-commercecommercebcmath

Drupal 8 - Commerce Module - BC math PHP extension not found


I'm trying to install the commerce module in Drupal 8 however I get the error 'BC math PHP extension not found'.

I've searched for this problem and tried different things such as editing the PHP.ini by adding 'bcmath.scale=2' however I still get the error message.

Any help would be appreciated, thanks.


Solution

  • Update 2020

    Please refer to @GiorgosK's answer for installing bcmath via a package manager if you are using a distribution that provides a bcmath package for PHP. I will ask the OP in comments to update the recommended answer, since that solution is probably what most people need.

    Three years ago when I answered this question, I suggested that you have to rebuild PHP to get bcmath. That was incorrect. I was using an older distribution of Debian/Ubuntu that provided bcmath as a statically linked extension in the core php package. I determined at the time (incorrectly) that bcmath was a core extension that had to be enabled at build-time (like SPL and PCRE).

    For those trying to troubleshoot a missing bcmath extension (such as those building/installing PHP themselves or nevertheless encountering issues), I've corrected and updated my original answer below. It explains in detail how to troubleshoot a missing PHP extension.

    Original Answer (Corrected)

    The error message indicates that PHP wasn't built with bcmath support or can't find the installed extension. PHP extensions are either built into PHP directly or they are loaded from an external dynamic library file at runtime.

    Since PHP obviously doesn't have the extension built-in, it can't find the external library file that provides bcmath. This file on POSIX platforms will be called bcmath.so and php_bcmath.dll on Windows.

    Extension files are installed under a directory indicated by the extension_dir property in php.ini. To determine the value of this property, run the following command:

    php -r 'echo ini_get("extension_dir").PHP_EOL;'
    

    The default value for this property is configured when PHP is built and may vary from distribution-to-distribution.

    Once you verify the extension file is installed in this location, you can then check to see if the extension is enabled in php.ini. You should see a line that enables the extension like so:

    # POSIX platforms
    extension=bcmath.so
    
    # Windows
    extension=php_bcmath.dll
    

    For Linux distributions like Ubuntu/Debian that install extensions via the package manager, the format is somewhat different since Debian employs a distributed configuration. Typically the package manager installs everything correctly, but you can check to see if an ini file exists for bcmath under the corresponding conf.d directory. These small ini files are snippets imported into the larger php.ini file, and they are typically symlinked to /etc/phpX/mods-available, allowing modules to be initially enabled for all PHP SAPIs such as CLI, CGI, Apache Mod PHP, ETC. Make sure a symlink exists for the PHP SAPI you need to use.

    To ensure your PHP is loading the extension, run phpinfo(); in a test page and search for bcmath. You can also more easily do this with the CLI using a command like:

    $ php -i | grep -i bcmath
    # Success output: BCMath support => enabled
    
    # (Another command that works well for checking extensions)
    $ php -m | grep -i bcmath
    # Success output: bcmath
    

    In order for the CLI to show accurate results, it must target the same php.ini file. If it doesn't, then use the -c option to temporarily point the CLI at the correct php.ini (i.e. the one being used by your Drupal site).