简体   繁体   中英

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.

Just install bcmath plugin for your php version with no need to (re)build php as mentioned on accepted answer

# get php version
php -v

# install bcmath based on your version lets assume php 7.1
# for ubuntu
sudo apt install php7.1-bcmath
# for centos 
yum install bcmath

# restart apache 
sudo systemctl restart apache2

Above problem appears when installing commerce or commerce kickstart using composer

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 (ie the one being used by your Drupal site).

Add BC MATH extension for PHP 7.2

If you are getting this ( https://prnt.sc/sehmd5 ) error then, run below command using vagrant ssh

Run these command in root of vagrant ssh

  1. sudo add-apt-repository ppa:ondrej/php
  2. sudo apt update
  3. sudo apt install php7.2-bcmath
  4. service apache2 restart
  5. Then open php.ini and search for bcmath
  6. If bcmath scale is 0, Change it to 2
  7. Restart php
  8. Restart Apache
  9. Hard Reload website
  10. Install required modules

Similarly for any version of PHP, you only need to change PHP version in 3rd command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM