2

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.

jake66
  • 35
  • 1
  • 5
  • I'd recommend changing the accepted answer to @GiorgosK's – jofitz Feb 19 '19 at 11:39
  • I second this recommendation. (And I'm the one who messed up the accepted answer!) @GiorgosK's answer probably works for most people encountering this issue. – Roger Gee Apr 27 '20 at 20:57
  • It is apparently important that Commerce is the latest version. BCMath was not detected and I could not get Drupal Commerce installed until I updated exactly like is instructed in the [documentation](https://docs.drupalcommerce.org/commerce2/developer-guide/install-update/updating) – ñull Apr 29 '20 at 16:58

3 Answers3

7

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

GiorgosK
  • 7,647
  • 2
  • 29
  • 26
  • 1
    This is a far quicker/simpler solution than the accepted answer. Thanks @GiorgosK. FYI In my case I only had to go as far as `sudo apt install php7.1-bcmath` – jofitz Feb 19 '19 at 11:37
  • Dispite adding the module and restarting apache even kiling running php-cgi jobs, still install.php complains that it is not there. I checked with php -i and phpinfo() and it is active. – ñull Apr 27 '20 at 19:02
0

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).

Roger Gee
  • 851
  • 5
  • 10
  • Thanks very much for your response. At the moment I'm using a rental server which uses this operating system FreeBSD 9.1-RELEASE-p24 amd64. I can not find a way to update or reinstall PHP. I also tried adding to the PHP by using sudo and pkg commands but I encountered a problem which I think might have to do with not having permission from the server. Do you know a way to work around this problem? – jake66 Jul 01 '17 at 11:06
  • If you don't have permission, you'd be hard-pressed to update the PHP installation. Since the Web server is probably configured to use the system PHP install it'd be difficult to supply an alternative unless you can modify the Web server configuration. I'd get in contact with the hosting company and ask them to install the required PHP build and any packages. By the way, your question may be better answered on serverfault.com which is better-tuned for this sort of question. This site is for computer programming questions mainly. – Roger Gee Jul 04 '17 at 16:15
  • Thanks a lot for all the information. It turns out I don't have permission to change the root directory files on the rental server I'm using. So I'm going to try a different server ! If I run into any problems I'd be very grateful for your help! – jake66 Jul 15 '17 at 10:55
  • I have all the permissions. I can see BC math extension both in php -i and phpinfo() but still install.php does not see it. Is there some caching playing up here? – ñull Apr 27 '20 at 19:00
  • @ñull Make sure the web server PHP is targeting the same configuration as your CLI PHP. See my updated answer for more. – Roger Gee Apr 27 '20 at 20:59
  • I still think this was not my fault not knowing how to install bcmath or not knowing how to test it properly. I could see it enabled with a test phpinfo() accessing the same domain. This means that also the Drupal code should have seen it on the same domain. It is impossible that two php files accessed with a browser in the same domain give different results. Then I noticed that Commerce was not the latest release version. I updated following the instructions "composer update --with-dependencies 'drupal/commerce*' " from 2.13 to 2.17. After the update Drupal recognised bcmath straight away. – ñull Apr 29 '20 at 16:50
  • @RogerGee May be that should be included in the solution, that the administrator makes sure that Commerce is the latest version. – ñull Apr 29 '20 at 16:53
0

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.