The PHP MBString extension is used to handle multibyte strings, which is essential for supporting different character encodings in your web applications. In this guide, we will show you how to enable the PHP MBString extension on Ubuntu.
Note: These instructions are for Ubuntu 18.04 and 20.04. Some steps may differ for other Ubuntu versions.
Step 1: Update the Package List
Open a terminal window and run the following command to update the package list:
sudo apt update
This command fetches the latest package information from the configured repositories and updates the local package list.
Step 2: Install the PHP MBString Extension
After updating the package list, install the PHP MBString extension using the following command:
sudo apt install -y php-mbstring
If you have a specific PHP version installed, such as PHP 7.4, install the corresponding MBString extension:
sudo apt install -y php7.4-mbstring
root@geek:~# sudo apt install -y php7.4-mbstring Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: php7.4-mbstring 0 upgraded, 1 newly installed, 0 to remove and 39 not upgraded. Need to get 396 kB of archives. After this operation, 1,079 kB of additional disk space will be used. Get:1 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-mbstring amd64 1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1 [396 kB] Fetched 396 kB in 0s (1,193 kB/s) Selecting previously unselected package php7.4-mbstring. (Reading database ... 121676 files and directories currently installed.) Preparing to unpack .../php7.4-mbstring_1%3a7.4.33-5+ubuntu18.04.1+deb.sury.org+1_amd64.deb ... Unpacking php7.4-mbstring (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ... Setting up php7.4-mbstring (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ... Creating config file /etc/php/7.4/mods-available/mbstring.ini with new version Processing triggers for libapache2-mod-php7.4 (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ... Processing triggers for php7.4-cli (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...
Step 3: Restart the Web Server
After installing the PHP MBString extension, you need to restart your web server for the changes to take effect. If you’re using Apache, restart the service with the following command:
sudo systemctl restart apache2
If you’re using Nginx, restart the Nginx and PHP-FPM services:
sudo systemctl restart nginx sudo systemctl restart php7.4-fpm
Replace php7.4-fpm with the appropriate PHP-FPM version if you’re using a different PHP version.
Step 4: Verify the PHP MBString Extension is Enabled
To verify that the PHP MBString extension is enabled, create a PHP file called info.php in your web server’s document root (e.g., /var/www/html/) with the following content:
<?php phpinfo(); ?>
Now, visit http://your_server_ip/info.php in your web browser, and you should see the PHP information page. Look for the “mbstring” section, which confirms that the MBString extension is enabled.
After verifying the extension is enabled, don’t forget to remove the info.php file, as it can expose sensitive information about your server:
sudo rm /var/www/html/info.php
Conclusion
You have now successfully enabled the PHP MBString extension on your Ubuntu system. This extension allows your web applications to handle multibyte strings and provides better support for different character encodings.
If you have any questions, comments, or suggestions for improvement, please feel free to share your thoughts in the comments section below. Your feedback is invaluable to us, and it helps us create better and more informative content for our users.
1 Comment
# apt install php7.4-mbstring
Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Unable to locate package php7.4-mbstring
E: Couldn’t find any package by glob ‘php7.4-mbstring’
E: Couldn’t find any package by regex ‘php7.4-mbstring’