How to install PHP Imagemagick on Ubuntu 22.04

Are you a web developer looking for ways to enhance your PHP scripts with image manipulation capabilities? If so, then the Imagick PHP module is likely one of the tools you should add to your arsenal. As an open-source imaging library, it provides unprecedented support for a range of different image file types and easy integration with the PHP programming language.

But, if you’re running an Ubuntu server as many developers do, the process of installing the Imagick PHP module would not be much difficult as it seems to be. Compiling and debugging can present a challenge that not all web developers are up for, however, that is not the case with this PHP extension.

So, if you are a beginner and struggling to get this image mutilation tool on your Linux system, then here we are with the tutorial: to provide steps on how to install and use ImageMagick (IMAGICK) or php-imagick on Ubuntu 22.04 using its official repository.

What is ImageMagick (IMAGICK)?

ImageMagick, or IMAGICK as it is commonly abbreviated, is a free & open-source image manipulation software. That can be used to create, edit and manipulate bitmap images.

Furthermore, this command line tool has support for a wide range of file formats, hence which is why Imagick can be used for many applications such as file format conversion, changing image size, crop images, color quantization, transparency, the morphology of shapes, and much more.

Also, it is popular among developers because of its capabilities and a feature that allows them to use the command line interface to control certain aspects of images without GUI and compatibility to use it with popular programming languages such as C and C++.

Due to this, developers can automate tasks related to image manipulation such as batch rendering of images.

For example, for PHP language, ImageMagick is available as a module, using which a single line of code allows us to convert an entire folder full of images into different file formats or even make changes to each image individually. For its source code- visit the GitHub page.

 

Steps to Install PHP IMAGICK Extension on Ubuntu 22.04 LTS

It is not like the steps given here for the installation of the ImageMagick library and PHP’s Imagick extension are just limited to Ubuntu 22.04 LTS Jammy. We can use them for Debian, Linux Mint, POP OS, and other similar Linux distros. Alternatively one can check the already created separate tutorial to install ImageMagick & its PHP module on the Debian 11 Server or one for Ubuntu 20.04 LTS.

Requirements

To perform the commands of this tutorial you must have a working Ubuntu 22.04 Linux server, sudo or root user access, and an internet connection.

1. Start with Ubuntu 22.04 Update

Open the command terminal app whereas the server users would already be on the terminal display. So, from there just execute the system update command to ensure all the installed packages are up to date.

sudo apt update && sudo apt upgrade

 

2. Installing IMAGICK software

Well, if you are looking for just a PHP extension then you can skip this and move to the next one. Whereas developers or common users who want to try the ImageMagick command to edit the images can install its full package.

The best part is to install ImageMagick on Ubuntu 22.04 or previous versions we don’t need to add any third-party repository. Simply use the APT package manager and it will download the required packages through Ubuntu’s official default repositories.

sudo apt install imagemagick imagemagick-doc

To check whether the imagemagick is  install on your Ubuntu system or not, you can use:

convert -version

You see the version details of the program which ensures it on your system and working properly.

 

3. Installing php-imagick module on Ubuntu 22.04

Now, those who are using some PHP-based applications, for example, WordPress, and required php-imagick package or extension on their Ubuntu  22.04 server to enable image manipulation capabilities can use the given command.

sudo apt-get install php-imagick

It will install the ImageMagick extension for PHP, depending upon the PHP version available to install. For example in Ubuntu 22.04, while the time of writing this article the current version was php8.1.

To check the extension is installed successfully, use:

php -m | grep imagick

Don’t forget to reload Apache or Nginx web server after installing extensions:

sudo systemctl reload apache2

or

sudo systemctl reload nginx

 

4. Latest or old PHP IMAGICK Extension version – Ondrej

Some PHP-based applications required old versions. So, if you are interested in getting the extremely latest or older Imagick module for PHP versions such as  5.6, 7.0, 7.1, 7.2, 7.3, 7.4, or the latest 8.2 (at the time of writing this tutorial) but not available through the official base repository of Ubuntu 22.04 can use the Ondrej Sury. It is a PPA repository to install the older or latest supported versions of PHP.

To add it run:

sudo add-apt-repository ppa:ondrej/php

Now, to check what are versions of PHP available through it, we can use this syntax:

sudo apt search php

You can scroll up to find the available ones.

Now, let’s see how to install ImageMagick and PHP Imagick extension for different versions of PHP:

Note: Check which version of PHP is installed on your Ubuntu 22.04 server and then corresponding to that go for the command.

php -v

Here are the commands as per the version configured on your system, if you have multiple then install php-Imagick accordingly.

  • For PHP5.6
sudo apt install imagemagick php5.6-Imagick
  • For php7.0
sudo apt install imagemagick php7.0-Imagick
  • For php7.1
sudo apt install imagemagick php7.1-Imagick
  • For PHP7.2
sudo apt install imagemagick php7.2-Imagick
  • For PHP7.3
sudo apt install imagemagick php7.3-Imagick
  • For PHP7.4
sudo apt install imagemagick php7.4-Imagick
  • For PHP8.0
sudo apt install imagemagick php8.0-Imagick
  • For PHP8.1
sudo apt install imagemagick php8.1-Imagick
  • For PHP8.2
sudo apt install imagemagick php8.2-Imagick

 

5. Check Imagick.so extension in PHP is loaded or not

So far we have learned the ways to install Imagick and its extension for PHP now let’s see the ways to check whether it is loading and working for our PHP web applications or not.

For that use the given command that will list all the available modules including the one you have just installed:

php -m

php m command result

or to have details exactly about the extension you are looking for, we can use:

php -r 'phpinfo();' | grep imagick

Imagick PHP extension details

If this is not enough then to confirm further we can create a file to load and view the current PHP configuration using a web browser. However, make sure you have Apache (sudo apt install apache) or Nginx (sudo apt install nginx) installed on your Ubuntu 22.04 system.

Create a PHP file under the www web-root directory:

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

Add the following PHP code line and save the file by pressing Ctrl+X, typing – Y, and then hitting the Enter key.

<?php phpinfo(); ?>

After that, open your browser and access the IP-address or domain of the Ubuntu 22.04 server where you have created the file, in the following way:

http://your-server-ip-addres/info.php

Note: Replace your-server-ip-addres with your actual IP or Domain.

Soon, you will see how this tiny PHP code will create a webpage with all the information about installed PHP extensions and other configurations. Scroll down and find the Imagick section, if it is there that means the module is loaded and working fine.

Install Imagick PHP extension in Ubuntu 22.04 Server

Conclusion

Now, we are closing this tutorial, I hope with this article you will be able to install Imagick and its module for PHP on an Ubuntu 22.04 system, easily, moreover it is a fairly straightforward process, isn’t it?  We can also compile it from the source but that is not necessary when the repository is available to quickly have this tool on our system.

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.