How to Install PHP ImageMagick (IMAGICK) on Debian 11 Bullseye

Imagick is a PHP extension to create and modify images using the ImageMagick library. Those unfamiliar with the ImageMagick software are open-source, free software that can convert, read, write, and process raster images. Currently, the PHP extension only supports PHP 5.4, 5.5, 5.6, 7.0, 7.1, 7.2, 7.3, and 7.4. Currently, no PHP 8.0 or 8.1 support exists at this current time.

In the following tutorial, you will learn how to install PHP ImageMagick (IMAGICK) on Debian 11 Bullseye.

Note, visit our guide on How to Install ImageMagick on Debian 11 Bullseye if you want to install ImageMagick software.

Prerequisites

  • Recommended OS: Debian 11 Bullseye
  • User account: A user account with sudo privilages or root access (su command).
  • Required Packages: wget

Updating Operating System

Update your Debian 11 operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt upgrade

Root or Sudo Access

By default, when you create your account at startup with Debian compared to other distributions, it does not automatically receive sudoers status. You must either have access to the root password to use the su command or visit our tutorial on How to Add a User to Sudoers on Debian.

Install PHP ImageMagick

You have two choices for installation. One is the default Debian repository or installed from the PPA by Ondřej Surý. The tutorial will cover both options, and you can best decide which one to choose.

Install PHP-IMAGICK from Debian 11 Bullseye Repository

The first option is to use the official Debian 11 default repository, and this is one of the most preferred options and arguably the most stable. The only drawback will be outdated for new builds and improvements compared to the other two options.

Install php-imagick with the following command:

sudo apt install php-imagick

Alternatively, you can use the preferred version such as 7.:

sudo apt install php7.4-imagick

Next, verify the installation:

php -m | grep imagick

Example output:

imagick

Install PHP-IMAGICK from PHP Repository by Ondřej Surý

The second installation choice and is recommended installing the PHP Repository by Ondřej Surý. For those unfamiliar, Ondřej Surý is the maintainer for PHP at Debian and always updates this PPA with the latest stable versions of PHP released.

The first step is to import and install the GPG key before adding the repository. To do this, use the following terminal (CTRL+ALT+T) command:

sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

Note, you may need to install these dependencies if you have trouble:

sudo apt install apt-transport-https lsb-release ca-certificates

With the GPG key sorted, it is time to add the Ondřej Surý repository as follows:

sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'

Before you continue, refresh your repository list to make the new repository is in sync.

sudo apt update

Now, y ou can install php-imagick from the new repository:

sudo apt install php-imagick

Alternatively, you can use the preferred version such as 7.4:

sudo apt install php7.4-imagick

You can also install PHP 8.0 or any other specific version this way, another example:

sudo apt install php8.0-imagick

Next, verify the installation:

php -m | grep imagick

Example output:

imagick

Enabling PHP-IMAGICK .SO in PHP.INI file

To enable imagick.so you will need to edit your php.ini file. A quick way to do this is to create a .PHP file with your PHP information.

Apache

The php.ini for Apache webservers is located at /etc/php/7.4/apache2/php.ini. Replace the version build (7.4) if you are using, for example, 8.0, 7.3, 7.2.

First, use any text editor and open the php.ini as follows:

sudo nano /etc/php/7.4/apache2/php.ini 

Next, add the following line under the [PHP], which is located on line 2:

extension=imagick

Now, save the file CTRL+O and exit after saving, CTRL+X.

To finish off, restart Apache:

sudo systemctl restart apache2

Nginx

By default, this should be automatically enabled. If missing following the instructions below.

The php.ini for Nginx webservers is located at /etc/php/7.4/fpm/php.ini. Replace the version build (7.4) if you are using, for example, 7.3, 7.2.

First, use any text editor and open the php.ini as follows:

sudo nano /etc/php/7.4/fpm/php.ini.

Next, add the following line under the [PHP], which is located on line 2:

extension=imagick

Now, save the file CTRL+O and exit after saving, CTRL+X.

To finish off, restart PHP-FPM:

sudo systemctl restart php7.4-fpm

Note, replace the 7.4 with your PHP version.

Create PHP Info Page to Verify Installation

The last part is to create a PHP test page to confirm your PHP information. Ideally, you want to do this in a locked-off or hidden area in your web directory if it’s accessible and public.

First, create in your web directory a test page:

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

Next, copy and paste the sample PHP code below.

<?php
phpinfo();
?>

Save the file (CTRL+O), then exit (CTRL+X).

In your Internet Browser, type in the HTTP:// or HTTPS:// server IP or domain name.

Examples:

https://www.linuxcapable.com/phpinfo.php

http://192.168.51.55/phpinfo.php

You should see imagick.ini on the PHP information page:

Apache Example:

How to Install PHP ImageMagick (IMAGICK) on Debian 11 Bullseye

Nginx Example:

How to Install PHP ImageMagick (IMAGICK) on Debian 11 Bullseye

Scrolling down the page, you can also find the version build and information on your version php-imagick installation.

Example only:

How to Install PHP ImageMagick (IMAGICK) on Debian 11 Bullseye

Comments and Conclusion

You have learned how to install php-imagick either using the Debian default repository or the one by Ondřej Surý. Overall, out of the many options for image conversion that PHP can use, this is the most highly used and recommended choice compared to other options such as GD, especially for WordPress web servers, as it produces sharper, higher-quality compressed images.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!