Resolving the Missing php7.1-bcmath Package on Ubuntu
When deploying a PHP 7.1 stack on Ubuntu 16, you might encounter an immediate roadblock: the package manager refuses to install the specific extension you need. You attempt to run apt install php7.1-bcmath, only to be met with errors stating that the package cannot be located via glob or regex. Simultaneously, installing the generic php-bcmath pulls in php7.0-bcmath. This mismatch occurs because Ubuntu's default repositories often lag behind the latest PHP minor versions, bundling packages into broader version-neutral names rather than strict minor-version prefixes like you might expect from upstream Debian testing branches.
The core issue is that php-bcmath is a meta-package designed to install the extension for whatever is currently set as your primary PHP 7.x stream. On Ubuntu 16 with PHP 7.1, the package manager defaults to installing dependencies for PHP 7.0 if the specific 7.1 variant isn't explicitly available or prioritized in the main repository. To get bcadd and other arbitrary-precision math functions working on your PHP 7.1 server, you must force the installation of the correct component without relying on the generic meta-package name.
The most reliable solution is to bypass the generic php-bcmath package entirely and target the specific versioned extension file directly. You should execute the following command:
sudo apt-get install php7.1-bcmath
If this returns the same "Unable to locate package" error, it indicates that your system's APT repositories are not configured to include the PHP 7.1 specific packages for Ubuntu 16.04 (Xenial Xerus). In many cases, these specific packages are only available in universe or require enabling a specific PPA (Personal Package Archive) that hosts backported PHP versions.
To resolve this without downgrading to PHP 7.0 or upgrading the entire OS immediately, you can try adding the OndÅej Surý PHP repository, which is widely used for keeping PHP versions current on Ubuntu. Add the GPG key and add the repository:
wget -q https://packages.sury.org/php/apt.gpg -O - | sudo apt-key add -
echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu xenial main" | sudo tee /etc/apt/sources.list.d/php-xenial.list
sudo apt-get update
Once the repository is added and your package list is refreshed, run the installation command again:
sudo apt-get install php7.1-bcmath
This ensures you are installing the extension compiled specifically for PHP 7.1. After installation, do not forget to enable the module within your web server configuration. For Apache users, this usually means ensuring the .ini file is included:
sudo phpenmod bcmath
sudo systemctl restart apache2
For Nginx, you typically need to ensure the php-fpm service picks up the new binary.
There is a critical trade-off to consider here. Relying on default Ubuntu repositories for PHP 7.1 can lead to version fragmentation where extensions are mismatched with the core interpreter. This often results in runtime errors or missing functions like bcadd. By switching to a maintained PPA, you align your system closer to how modern Laravel applications expect the environment to be configured. If you are building a new application on this stack, using the PPA ensures that when you later need to install other extensions like php7.1-imagick or php7.1-intl, they will match the major and minor version of your PHP core without conflict.
Finally, verify that the function is actually available by creating a simple script or checking the output of php -m. If you see bcmath listed in the module list, your application can now safely utilize high-precision arithmetic functions.
