Problem: Installing PHP Intl Extension on CentOS
The PHP Intl extension provides functionality for handling internationalization in PHP applications. Installing this extension on CentOS systems can be difficult without the right repository and configuration.
Preparing Your CentOS System
Checking Current PHP Version
To check your PHP version, open a terminal and run this command:
php -v
This command shows the installed PHP version. Knowing your PHP version helps with Intl extension compatibility. Different PHP versions may need specific Intl extension versions.
Tip: Version Compatibility Check
Before installing the Intl extension, make sure to check its compatibility with your PHP version. You can visit the official PHP documentation or the extension's repository to confirm which versions are supported for your specific PHP installation.
Enabling Remi Repository
The Remi Repository is a third-party repository that provides updated versions of PHP and its extensions for CentOS. It helps install the latest PHP packages and extensions.
To enable the Remi Repository on CentOS:
-
Install the EPEL repository:
sudo yum install epel-release
-
Install the Remi repository:
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
-
Install the yum-utils package:
sudo yum install yum-utils
-
Enable the Remi repository for your PHP version. For example, to enable PHP 7.4:
sudo yum-config-manager --enable remi-php74
Replace "74" with your desired PHP version number.
After enabling the Remi Repository, you can install the PHP Intl extension.
Installing PHP Intl Extension
Using Yum Package Manager
To install the PHP Intl Extension using Yum, run this command:
sudo yum install php-intl
If you have dependency issues, try this command:
sudo yum --enablerepo=remi install php-intl
This tells Yum to use the Remi repository for the installation, which often fixes dependency problems.
If you still have issues, try updating your system first:
sudo yum update
Then try the installation again.
Tip: Troubleshooting Installation Issues
If you encounter errors during installation, check your PHP version compatibility. Some versions of PHP-Intl require specific PHP versions. Use php -v
to verify your PHP version and ensure it matches the Intl extension requirements.
Verifying the Installation
To check if the PHP Intl Extension is installed, you can use these methods:
-
Check PHP modules:
php -m | grep intl
If installed, you should see "intl" in the output.
-
Use PHP info function: Create a PHP file (e.g., phpinfo.php) with this content:
<?php phpinfo(); ?>
Access this file through your web server and look for "intl" on the page.
-
Check PHP configuration:
php -i | grep intl
This shows details about the Intl extension if it's installed.
If you don't see the Intl extension in these checks, restart your web server:
sudo systemctl restart httpd
or
sudo systemctl restart php-fpm
depending on your setup.
Example: Testing Intl Extension Functionality
After installation, you can test if the Intl extension is working correctly by running a simple PHP script:
<?php
if (extension_loaded('intl')) {
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
echo $formatter->formatCurrency(1234.56, 'USD');
} else {
echo "Intl extension is not loaded.";
}
?>
This script should output "$1,234.56" if the Intl extension is working properly.
Troubleshooting Common Installation Issues
Dependency Conflicts
When installing the PHP Intl extension, you might face dependency errors. These can happen when packages are not available or when versions don't match.
Common dependency errors include:
- Missing packages
- PHP and Intl extension version mismatch
- Conflicts with other packages
To fix these conflicts:
-
Update your system:
sudo yum update
-
Install missing dependencies:
sudo yum install php-common php-cli php-devel
-
If problems persist, install the Intl extension version that matches your PHP version:
sudo yum install php-intl-[your_php_version]
Tip: Check PHP Version
Before installing the Intl extension, check your PHP version using the command:
php -v
This helps you select the correct Intl extension version compatible with your PHP installation.
Repository Mismatch
Repository mismatches occur when packages come from different sources, causing compatibility issues.
Signs of repository problems:
- Error messages about different repositories
- Version conflicts between packages
To align repositories for better installation:
-
Check your enabled repositories:
yum repolist
-
Disable conflicting repositories:
sudo yum-config-manager --disable [repository_name]
-
Enable the correct repository for your PHP version:
sudo yum-config-manager --enable remi-php[version]
-
Clear the yum cache:
sudo yum clean all
-
Try the installation again:
sudo yum install php-intl
By addressing these issues, you can often solve problems with installing the PHP Intl extension on CentOS.
Configuring PHP Intl Extension
Modifying PHP Configuration
To configure the PHP Intl extension, you need to change the PHP configuration file (php.ini). Follow these steps:
-
Find the php.ini file:
php -i | grep "Loaded Configuration File"
This command shows where your active php.ini file is.
-
Open the php.ini file with a text editor:
sudo nano /path/to/php.ini
Replace "/path/to/" with the actual path from step 1.
-
Find the Intl extension section. If it's not there, add these lines:
extension=intl.so intl.default_locale = en_US
-
Save the file and exit the editor.
-
Restart your web server to apply the changes:
sudo systemctl restart httpd
or
sudo systemctl restart php-fpm
depending on your setup.
Tip: Verifying Intl Extension Status
After restarting your web server, you can quickly check if the Intl extension is enabled by running the following command:
php -m | grep intl
If the Intl extension is properly loaded, you should see "intl" in the output.
Testing Intl Functionality
After setting up the Intl extension, test it to make sure it works. Here's a simple PHP script to test the Intl extension:
-
Create a new PHP file named "intl_test.php" with this content:
<?php if (extension_loaded('intl')) { echo "Intl extension is loaded.\n"; $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY); echo $formatter->formatCurrency(1234.56, 'USD') . "\n"; } else { echo "Intl extension is not loaded.\n"; } ?>
-
Run the script:
php intl_test.php
-
Check the results:
- If you see "Intl extension is loaded." and "$1,234.56", the extension is working.
- If you see "Intl extension is not loaded.", review your installation and configuration steps.
If the test fails, check your installation steps and php.ini configuration. Make sure you've restarted your web server after making changes.