How to Install PHP Zip Extension in CentOS/RHEL?

Published July 4, 2024

Problem: Installing PHP Zip Extension on CentOS/RHEL

The PHP Zip extension allows you to work with ZIP archives in PHP applications. It's not included by default in CentOS and RHEL distributions. Users often have trouble installing this extension, as the process can be complex compared to other PHP modules.

Steps to Install PHP Zip Extension

Check PHP Version

To start, you need to know your PHP version. Open a terminal and run:

php -v

This shows your current PHP version. Your PHP version determines which package to install.

Install PHP Zip Extension on CentOS 7

On CentOS 7, use YUM to install the PHP Zip extension:

  1. Open a terminal.
  2. Run:
sudo yum install php-zip
  1. Enter your password if asked.

Install PHP Zip Extension on CentOS 8

CentOS 8 uses DNF. To install the PHP Zip extension:

  1. Open a terminal.
  2. Run:
sudo dnf install php-zip
  1. Enter your password if asked.

Check the Installation

After installation, check if the Zip extension is active:

  1. Run in the terminal:
php -m | grep zip
  1. If you see "zip" in the output, the extension is active.

You can also create a PHP file with this content:

<?php
if (extension_loaded('zip')) {
    echo "ZIP extension is installed.";
} else {
    echo "ZIP extension is not installed.";
}
?>

Save this as "check_zip.php" and run it in your browser or command line to confirm the installation.

Alternative Methods for Installing PHP Zip Extension

Using PECL (PHP Extension Community Library)

PECL offers another way to install PHP extensions. To use PECL for installing the ZIP extension:

  1. Install PECL:

    sudo yum install php-pear
  2. Install the ZIP extension through PECL:

    sudo pecl install zip
  3. Add the extension to your PHP configuration:

    echo "extension=zip.so" | sudo tee -a /etc/php.ini
  4. Restart your web server to apply the changes.

Compiling from Source

You can compile the ZIP extension from source:

  1. Download the source code:

    wget https://pecl.php.net/get/zip-1.19.3.tgz
  2. Extract the archive:

    tar -xzvf zip-1.19.3.tgz
  3. Go to the extracted directory:

    cd zip-1.19.3
  4. Prepare the build environment:

    phpize
  5. Configure the build:

    ./configure
  6. Compile and install:

    make
    sudo make install
  7. Add the extension to your PHP configuration:

    echo "extension=zip.so" | sudo tee -a /etc/php.ini
  8. Restart your web server to apply the changes.

Replace "1.19.3" with the version you want to install.

Troubleshooting Common Issues

Repository Problems

You might face issues with package repositories when trying to install the PHP Zip extension. To fix these:

  1. Update your repository lists:

    sudo yum clean all
    sudo yum update
  2. If the system can't find the PHP Zip package, add the EPEL repository:

    sudo yum install epel-release
    sudo yum update

After adding EPEL, try installing the PHP Zip extension again.

Dependency Conflicts

You might encounter dependency conflicts when installing the PHP Zip extension. To fix these:

  1. Check for conflicting or missing dependencies:

    sudo yum deplist php-zip
  2. If there are missing dependencies, install them:

    sudo yum install [package-name]
  3. Update your system packages:

    sudo yum update
  4. If you still have issues, try installing the PHP Zip extension with the "--skip-broken" option:

    sudo yum install php-zip --skip-broken

This option allows yum to skip packages that may cause dependency problems.