How to Install PHP-IMAGICK on Debian Linux

PHP-Imagick is an extension for PHP that provides image manipulation capabilities through the ImageMagick library. With this extension, you can resize images, add watermarks, convert between formats (PNG, JPEG, WebP, GIF), and apply filters directly from within your PHP applications. Common use cases include generating thumbnails for WordPress uploads, creating image galleries, and processing user-uploaded photos.

This guide demonstrates how to install PHP-Imagick on Debian 13, 12, or 11 using the command-line terminal. You’ll learn two installation methods: using Debian’s default repository for a straightforward setup, or using the Ondřej Surý repository for access to the latest PHP versions. By the end, you’ll have PHP-Imagick loaded and ready for use in your web applications.

Choose Your PHP-Imagick Installation Method

Debian offers PHP-Imagick through its default repositories, while the Ondřej Surý repository provides newer PHP versions with the latest features and security patches.

MethodChannelPHP VersionsUpdatesBest For
Debian RepositoryDebian PackagesDistro defaultAutomatic via apt upgradeMost users who want stability
Ondřej Surý Repositorypackages.sury.orgPHP 5.6–8.5Automatic via apt upgradeUsers needing specific PHP versions

Most users should choose the Debian repository method because it requires no third-party repositories and receives security updates through standard system upgrades. Use the Ondřej Surý repository only if you specifically need a PHP version newer than what Debian provides.

PHP versions vary by Debian release: Debian 11 ships PHP 7.4, Debian 12 ships PHP 8.2, and Debian 13 ships PHP 8.4. The php-imagick package automatically installs the extension for your system’s default PHP version.

Method 1: Install PHP-Imagick from Debian Repository

Update System Packages

Before installing, update your package lists and upgrade existing packages:

sudo apt update && sudo apt upgrade

Install PHP-Imagick

Next, install the PHP-Imagick extension from Debian’s repository:

sudo apt install php-imagick

During installation, APT automatically installs the ImageMagick library as a dependency along with all required packages.

Verify PHP-Imagick Installation

Finally, confirm the extension is loaded by checking the list of PHP modules:

php -m | grep imagick

Expected output:

imagick

If you see imagick in the output, PHP has loaded the extension for CLI. For web server integration, see the PHP-FPM restart instructions in the troubleshooting section below.

Method 2: Install PHP-Imagick from Ondřej Surý Repository

The Ondřej Surý repository provides newer PHP versions than Debian’s default packages. Use this method if you need PHP 8.3, 8.4, or want access to the latest PHP releases.

Install Required Packages

First, install the packages needed to add the third-party repository:

sudo apt install lsb-release ca-certificates curl -y

Add the Ondřej Surý Repository

Next, download and install the repository keyring package:

curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb
sudo dpkg -i /tmp/debsuryorg-archive-keyring.deb

Then, add the PHP repository using the modern DEB822 format:

cat <<EOF | sudo tee /etc/apt/sources.list.d/sury-php.sources
Types: deb
URIs: https://packages.sury.org/php/
Suites: $(lsb_release -sc)
Components: main
Signed-By: /usr/share/keyrings/deb.sury.org-php.gpg
EOF

Update Package Lists

After adding the repository, refresh your package lists to include the new packages:

sudo apt update

To confirm the repository is active, check the package availability:

apt-cache policy php8.4-imagick

You should see output showing the package is available from packages.sury.org:

php8.4-imagick:
  Installed: (none)
  Candidate: 3.8.0-4+0~20251124.53+debian13~1.gbp46bd33
  Version table:
     3.8.0-4+0~20251124.53+debian13~1.gbp46bd33 500
        500 https://packages.sury.org/php trixie/main amd64 Packages

Install Your Chosen PHP Version First

Before installing PHP-Imagick, install the PHP version you want to use. The Sury repository’s php-imagick metapackage pulls the latest PHP (currently 8.5), which may not match your existing setup:

sudo apt install php8.4-cli php8.4-fpm php8.4-common

Replace 8.4 with your desired version (8.2, 8.3, etc.). This ensures you control which PHP version is active before adding extensions.

Install PHP-Imagick

Install the version-specific PHP-Imagick extension that matches your PHP installation:

sudo apt install php8.4-imagick

Adjust the version number to match your PHP version:

sudo apt install php8.2-imagick
sudo apt install php8.3-imagick
sudo apt install php8.4-imagick

Always use the version-specific package (e.g., php8.4-imagick) rather than the generic php-imagick metapackage. The metapackage pulls the repository’s default PHP version, which may upgrade your entire PHP stack unexpectedly. Check your current PHP version with php -v. For a complete PHP installation guide, see our dedicated article.

Verify the Installation

Once installed, confirm PHP-Imagick is loaded correctly:

php -m | grep imagick

Expected output:

imagick

Troubleshooting PHP-Imagick

Extension Not Loading in Web Server

If PHP-Imagick works in CLI but not in your web application, you need to restart PHP-FPM. First, identify your PHP-FPM service:

systemctl list-units --type=service | grep php

For example, on Debian 12 you might see:

php8.2-fpm.service   loaded active running   The PHP 8.2 FastCGI Process Manager

Then, restart the service matching your PHP version. For example, with PHP 8.4:

sudo systemctl restart php8.4-fpm

However, for Apache with mod_php, restart Apache instead:

sudo systemctl restart apache2

Class ‘Imagick’ Not Found Error

If your PHP application displays this error:

PHP Fatal error:  Uncaught Error: Class 'Imagick' not found

This means PHP cannot find the extension for your active version. First, verify the extension exists for your active PHP version:

php -m | grep imagick

If this returns nothing, install the version-specific package (e.g., sudo apt install php8.4-imagick). If you’re using PHP-FPM, restart the service after installation.

Check Imagick Configuration

Additionally, to view detailed information about your Imagick installation, run:

php -i | grep -A 20 "^imagick$"

When PHP loads the extension correctly, you’ll see:

imagick

imagick module => enabled
imagick module version => 3.8.0
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
Imagick compiled with ImageMagick version => ImageMagick 7.1.1-47 Q16 x86_64
Imagick using ImageMagick library version => ImageMagick 7.1.1-43 Q16 x86_64

This output confirms the Imagick version, available classes, and the linked ImageMagick library version.

Remove PHP-Imagick

If you no longer need the extension, remove PHP-Imagick from your system:

sudo apt remove php-imagick
sudo apt autoremove

If you used a version-specific package, remove that instead:

sudo apt remove php8.2-imagick
sudo apt autoremove

Remove Ondřej Surý Repository (Optional)

If you no longer need the Ondřej Surý repository, remove the repository file and keyring:

sudo rm /etc/apt/sources.list.d/sury-php.sources
sudo apt remove debsuryorg-archive-keyring
sudo apt update

Finally, verify the repository is removed:

apt-cache policy php-imagick

After this, the output should show only Debian repository sources:

php-imagick:
  Installed: 3.8.0-2
  Candidate: 3.8.0-2
  Version table:
 *** 3.8.0-2 500
        500 http://deb.debian.org/debian trixie/main amd64 Packages
        100 /var/lib/dpkg/status

Conclusion

You’ve successfully installed PHP-Imagick on Debian using either the default repository or the Ondřej Surý repository. As a result, the extension is now available for image resizing, format conversion, and watermarking in your PHP applications. For projects like WordPress or custom galleries, PHP-Imagick handles thumbnail generation and image processing automatically. To learn more, explore the PHP Imagick documentation for the complete method reference. Additionally, consider setting up Nginx or Apache with PHP-FPM for production deployments.

Leave a Comment