How to Install PHP Extension ImageMagick (IMAGICK) on Ubuntu 22.04 LTS
You’ll get Imagick up and running so your PHP scripts can resize, watermark, or convert images without pulling in a heavyweight GUI library. The guide assumes you have root or sudo access and that PHP is already installed.
1. Install the ImageMagick binaries and development files
sudo apt update
sudo apt install imagemagick libmagickwand-dev
imagemagick gives you the command‑line tools (convert, identify, …) while libmagickwand-dev provides the header files needed to compile the PHP extension. I’ve run into broken builds when people skip the -dev package; the compiler will complain about missing MagickWand.h.
2. Pull in the PECL source for imagick
sudo apt install php-pear php-dev build-essential
pecl download imagick
tar xf imagick-*.tgz
cd imagick-*
php-pear and php-dev bring in phpize, the script that prepares a module for compilation. Skipping them usually ends with an “phpize not found” error, which wastes time.
3. Compile and install the extension
phpize
./configure
make
sudo make install
During ./configure you’ll see it probing for ImageMagick libraries; if it can’t locate them, double‑check that libmagickwand-dev is installed. I once saw this happen after a kernel upgrade that silently removed some shared objects—reinstalling the dev package fixed it.
4. Enable imagick in PHP
echo "extension=imagick.so" | sudo tee /etc/php/$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')/cli/conf.d/20-imagick.ini
sudo systemctl restart php$(php -r 'echo PHP_MAJOR_VERSION."".PHP_MINOR_VERSION;')-fpm.service # if you use FPM
The php -r snippet grabs your exact version so the ini file lands in the right directory. If you’re only using the CLI, skip the systemd restart.
5. Verify the installation
php -m | grep imagick
You should see “imagick” listed. A quick test script also helps:
<?php
$im = new Imagick();
echo $im->getVersion()['versionString'];
?>
Run it with php test.php; you’ll get something like “ImageMagick 7.1.0‑something”. If it throws a class‑not‑found error, double‑check the ini file path and that PHP is actually loading it.
Optional: Keep imagick in sync after system upgrades
Ubuntu’s package manager won’t touch PECL extensions, so when you run apt upgrade and ImageMagick itself jumps to a newer major version, you may need to rebuild imagick. A habit of running pecl update-channels && pecl install imagick after a big OS upgrade saves headaches later.