If you're working with Drupal on an Ubuntu 24.04 server, it's common to use the command line interface for various tasks, including clearing the cache with drush cr. However, you might encounter a warning like this
PHP Warning: Module "mbstring" is already loaded in Unknown on line 0
This warning indicates that the mbstring PHP extension, which is essential for multibyte string handling, is being loaded more than once in your PHP configuration. While this warning might not prevent your commands from completing successfully, it’s something you’ll want to address to avoid potential issues down the line.
In this article, we'll walk you through understanding and resolving this warning.
Understanding the issue
What is mbstring?
The mbstring extension in PHP provides multibyte-specific string functions that help with processing non-ASCII characters. It's crucial for ensuring that your web applications handle UTF-8 and other multibyte encodings correctly.
Why the warning appears
The warning you're seeing occurs because the mbstring module is being loaded multiple times, usually due to configuration files that overlap. PHP loads its configuration from a main php.ini file and additional .ini files, often stored in a conf.d directory. If mbstring is specified in more than one of these files, PHP will throw this warning.
Step-by-step solution
1. Locate the PHP configuration files
First, you need to identify where PHP is loading its configuration files from. PHP might have separate configurations for command-line usage (CLI) and the web server.
For CLI PHP Configuration
To find out which configuration file is being used by PHP in the CLI, run:
php --ini
The output will show you:
- The directory where PHP looks for the php.ini file;
- The specific php.ini file being used; and
- Any additional configuration files being parsed.
Such as the environment we were working in a truncated output is below:
Configuration File (php.ini) Path: /etc/php/8.3/cli
Loaded Configuration File: /etc/php/8.3/cli/php.ini
Scan for additional .ini files in: /etc/php/8.3/cli/conf.d
Additional .ini files parsed: /etc/php/8.3/cli/conf.d/10-mysqlnd.ini,
/etc/php/8.3/cli/conf.d/10-opcache.ini,
/etc/php/8.3/cli/conf.d/10-pdo.ini,
/etc/php/8.3/cli/conf.d/15-xml.ini,
/../
/etc/php/8.3/cli/conf.d/20-gd.ini,
/etc/php/8.3/cli/conf.d/20-gettext.ini,
/etc/php/8.3/cli/conf.d/20-iconv.ini,
/etc/php/8.3/cli/conf.d/20-mbstring.ini,
/etc/php/8.3/cli/conf.d/20-mysqli.ini,
/../
/etc/php/8.3/cli/conf.d/20-xsl.ini
For Web Server PHP Configuration
If you also want to check the PHP configuration used by your web server (e.g., Apache or Nginx), you can create a PHP file in your web root directory (e.g., /var/www/html/) with the following content:
<?php phpinfo(); ?>
Save this file as phpinfo.php, then access it via your browser (e.g., http://yourdomain.com/phpinfo.php). Look for the line that reads Loaded Configuration File, which will indicate the path to the php.ini file used by your web server.
2. Identify duplicate mbstring entries
Once you have identified the relevant configuration files, you need to check for any duplicate entries that might be loading the mbstring extension more than once.
Open the php.ini file and look for the line
extension=mbstring
Next, check the additional .ini files in the conf.d directory (or equivalent) for similar lines. For example, in our case, the mbstring extension is being loaded by /etc/php/8.3/cli/conf.d/20-mbstring.ini
. If this file is loading mbstring, you should comment out or remove the extension=mbstring line in the main php.ini file to avoid loading the extension multiple times.
3. Remove or comment out the duplicate entry
If you find multiple entries, choose one place to load the mbstring extension and remove or comment out the others. For instance, if you decide to keep the entry in the php.ini file, you can comment out the line in the conf.d file:
;extension=mbstring
4. Restart your web server
After making these changes, restart your web server to apply the new configuration:
sudo systemctl restart apache2
Or, if you’re using Nginx:
sudo systemctl restart nginx
5. Verify the warning is resolved
Run the drush cr command again, or any other PHP command, and confirm that the warning no longer appears.
The wrap
The "Module 'mbstring' is already loaded" warning is a common issue when working with PHP, especially in environments where multiple configuration files are in play. By carefully checking and adjusting your PHP configuration, you can eliminate this warning and ensure that your server is configured correctly.