How to Install PHP-ImageMagick on Ubuntu 22.04 or 20.04


The PHP-ImageMagick extension is a powerful tool for manipulating images within PHP scripts. It allows you to perform operations such as resizing, cropping, and format conversion on images using the command-line ImageMagick library. This article will discuss installing the PHP-ImageMagick extension on Ubuntu 20.04 or 22.04 using the command-line terminal.

Step 1: Update Ubuntu

Begin by updating your system to ensure all packages are current, minimizing potential conflicts during installation.

sudo apt update

Use the command below to check for available updates for review or information.

apt list --upgradable

The command below updates all installed packages to their latest versions.

sudo apt upgrade

Step 2: Install PHP-ImageMagick

The simplest method of installing the PHP-ImageMagick extension is through the APT package manager. One can install the ImageMagick and PHP-Imagick packages by executing the command below.

sudo apt install imagemagick php-imagick

Restart any PHP-FPM services and your web server service after completing the installation to ensure optimal performance.

If you want to upgrade your PHP, check out how to upgrade PHP on Ubuntu 22.04 or 20.04 LTS.

Step 3: Enable PHP-ImageMagick for Apache or Nginx

To activate the imagick.so extension, you will have to edit the php.ini file. Before making any changes, verify that the PHP-ImagMagick extension is not already enabled by displaying the list of PHP modules.

php -m

By default, these should be loaded, but if they are not, use the following steps to enable it in your php.ini file.

Enabling PHP-ImageMagick in php.ini for Apache:

Locate your php.ini file by running the following command.

php --ini

It will give you the location of the php.ini file.

Open the php.ini file in a text editor by running the following example-only command.

sudo nano /etc/php/8.x/apache2/php.ini

Add the next line to the file.

extension=imagick.so

Save and close the file with CTRL+O and CTRL+X.

Restart the Apache service by running the following command.

sudo service apache2 restart

Enabling PHP-ImageMagick in php.ini for Nginx:

Locate your php.ini file by running the following command.

php --ini

It will give you the location of the php.ini file.

Open the php.ini file in a text editor by running the following example-only command.

sudo nano /etc/php/8.x/fpm/php.ini

Add the following line to the file.

extension=imagick.so

Save and close the file with CTRL+O and CTRL+X.

Restart the PHP-FPM service by running the following command.

sudo service php8.x-fpm restart

Restart the Nginx service by running the following command.

sudo service nginx restart

Note: Remember to replace the ‘8.x’ with your PHP version.

After completing the above steps, the PHP-ImageMagick extension should be enabled and ready to use in your PHP scripts.

php -m

It will show you the list of modules installed and check whether Imagick is on the list.

Please note that the above commands are examples and may vary depending on your specific setup, but the overall process should remain the same.

Step 4: Create a PHP Test Page

To create a PHP test page to check if the PHP-ImageMagick extension is installed correctly, you can follow these steps:

Create a new file in the web root directory of your web server, for example.

sudo nano /var/www/html/test.php

Add the following code to the file.

<?php

if (extension_loaded('imagick')) {
    echo "PHP-imagemagick is installed and enabled.";
} else {
    echo "PHP-imagemagick is not installed or not enabled.";
}

?>

Save and close the file with CTRL+O and CTRL+X.

Open a web browser and navigate to the test page.

http://localhost/test.php

## OR

http://{server-ip}/test.php

You should see the message “PHP-ImageMagick is installed and enabled” if the extension is correctly installed and enabled; otherwise, you will see the message “PHP-ImageMagick is not installed or not enabled.”

See also
How to Change Hostname on Ubuntu 22.04 or 20.04

Conclusion

In conclusion, this guide provides an overview of installing the PHP-ImageMagick extension on Ubuntu 20.04 or 22.04 using the command-line terminal and the APT package manager. It is a straightforward process that will enable you to use the powerful ImageMagick library within your PHP scripts to manipulate images efficiently.

1 thought on “How to Install PHP-ImageMagick on Ubuntu 22.04 or 20.04”

Leave a Comment

Share to...