Learn how to install the bcmath extension in your Ubuntu server for a specific PHP distribution.

How to solve "The requested PHP extension bcmath is missing from your system" when installing a library via composer in Ubuntu 16.04

A lot of packagist libraries that can be installed with composer require the famous bcmath extension. Thanks to the computers, you can easily run calculations on huge numbers with a high accuracy, however in some situations where the required amount of digits in the number exceeds the available memory of the computer, this high accuracy may be lower as expected nor may not work. That's why this extension in PHP exists, to solve this problem. BCMath is set of PHP functions that allow to you to use Arbitrary-precision arithmetic. This package is basically going to be a wrapper for these functions so that they can be used in an object oriented way.

In some Ubuntu servers that aren't being correctly configured, this extension may be missing, but don't worry as you can easily install via the CLI running a simple command. Follow these steps to get the bcmath extension running on your PHP distribution and finally install the PHP libraries that rely on this extension as moontoast/math.

1. Verify which PHP version are you using

As first step, you need to know which PHP version are you using as you can't simply install any version of the extension with any PHP version. To be sure of the version of PHP that you're using, you can run this command from the CLI:

php -v

This will generate an output similar to:

PHP 7.0 Ubuntu php -v

Where you can easily identify the PHP version that you're using. In our case, we are using PHP 7.0.

2. Install bcmath extension

Now that you know which PHP version are you using, you can simply install the respective version of bcmath using the following command:

# As we have php 7.0
sudo apt install php7.0-bcmath

# According to the version of PHP that you use
# the package name changes as:
# php7.1-bcmath

After finishing the installation you will be able to use the bcmath extension and you won't have the mentioned warning anymore.

Happy coding !

Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Become a more social person