Fixing PHP Composer error: ‘mbstring’ is missing

Updated: January 13, 2024 By: Guest Contributor Post a comment

The Problem

If you’re working with PHP and managing your project dependencies with Composer, you may encounter the ‘mbstring is missing’ error at some point. This error is an indication that the ‘mbstring’ extension, which stands for multi-byte string, is not enabled or installed on your PHP environment. This extension is necessary for handling non-ASCII strings, such as UTF-8 characters, which are commonly used in applications that support multiple languages.

In this tutorial, we’ll walk you through the steps to resolve this error by installing and enabling the ‘mbstring’ PHP extension on various operating systems and configurations.

Understanding the ‘mbstring’ Extension

The ‘mbstring’ PHP extension provides functions that help manage multi-byte encoded strings. PHP has native string functions; however, they do not reliably handle characters outside the ASCII range. The ‘mbstring’ extension overcomes this limitation and is essential for processing strings in character encodings such as UTF-8, EUC-JP, and Big5.

Let’s Fix It

Step 1: Verify PHP Installation

First, verify that PHP is installed by running the following command in your terminal:

php -v

If PHP is installed, you should see version information printed out. If not, you’ll need to install PHP first.

Step 2: Check if ‘mbstring’ is Installed

To check if ‘mbstring’ is already installed, run:

php -m

Look for ‘mbstring’ in the list of modules. If it’s not listed, it is not installed or enabled.

Step 3: Install ‘mbstring’ on Various Systems

Ubuntu/Debian

Update your package manager and install the extension with:

sudo apt-get update
sudo apt-get install php-mbstring

CentOS/Fedora/RHEL

Use YUM or DNF to install ‘mbstring’:

sudo yum install php-mbstring # For CentOS/RHEL 7
sudo dnf install php-mbstring # For Fedora or CentOS/RHEL 8

Windows

Uncomment the following line in your ‘php.ini’ file:

extension=php_mbstring.dll

Don’t forget to restart your web server after making any changes to the ‘php.ini’ file.

Step 4: Enable ‘mbstring’ Extension

Sometimes ‘mbstring’ is installed but not enabled. To enable it:

Ubuntu/Debian

sudo phpenmod mbstring

Windows

Make sure the line in ‘php.ini’ is not commented out (as mentioned in step 3).

Step 5: Verify ‘mbstring’ Installation

After installation and enabling, verify that ‘mbstring’ is loaded by running:

php -m | grep mbstring

You should now see ‘mbstring’ in the output.

Step 6: Update Composer

Once ‘mbstring’ is enabled, you should also update Composer:

composer self-update

Then, retry whatever Composer command you were running when you encountered the error.

Final Words

If after these steps Composer still gives errors, here are a few troubleshooting tips:

  • Make sure your PHP and Composer versions are compatible.
  • Confirm that PHP and Composer are using the same PHP configuration.
  • Check if you have multiple PHP installations and align Composer to use the correct one.
  • Analyze the ‘composer.json’ file for any inconsistencies.

Dealing with PHP extensions can sometimes be a hassle, but hopefully, this walkthrough has demystified the process and helped you fix the ‘mbstring is missing’ Composer error. With the ‘mbstring’ extension now properly installed and enabled, you can go back to building your PHP application with all the international string manipulation powers at your disposal.