How To Install PHP Intl Extension On CentOS With Remi Repository?

Published September 7, 2024

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:

  1. Install the EPEL repository:

    sudo yum install epel-release
  2. Install the Remi repository:

    sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
  3. Install the yum-utils package:

    sudo yum install yum-utils
  4. 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:

  1. Check PHP modules:

    php -m | grep intl

    If installed, you should see "intl" in the output.

  2. 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.

  3. 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:

  1. Update your system:

    sudo yum update
  2. Install missing dependencies:

    sudo yum install php-common php-cli php-devel
  3. 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:

  1. Check your enabled repositories:

    yum repolist
  2. Disable conflicting repositories:

    sudo yum-config-manager --disable [repository_name]
  3. Enable the correct repository for your PHP version:

    sudo yum-config-manager --enable remi-php[version]
  4. Clear the yum cache:

    sudo yum clean all
  5. 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:

  1. Find the php.ini file:

    php -i | grep "Loaded Configuration File"

    This command shows where your active php.ini file is.

  2. 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.

  3. Find the Intl extension section. If it's not there, add these lines:

    extension=intl.so
    intl.default_locale = en_US
  4. Save the file and exit the editor.

  5. 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:

  1. 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";
    }
    ?>
  2. Run the script:

    php intl_test.php
  3. 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.