How to Install PHP ImageMagick (IMAGICK) on Ubuntu 20.04

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, 7.4, 8.0 and 8.1.

In the following tutorial, you will learn how to install PHP ImageMagick (IMAGICK) on Ubuntu 20.04.

Note, visit the tutorial How to Install ImageMagick on Ubuntu 20.04 for installing package ImageMagick.

Advertisement

Prerequisites

  • Recommended OS: Ubuntu 20.04.
  • User account: A user account with sudo or root access.

Update Operating System

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

sudo apt update && sudo apt upgrade -y

The tutorial will be using the sudo command and assuming you have sudo status.

To verify sudo status on your account:

sudo whoami

Example output showing sudo status:

[joshua@ubuntu ~]$ sudo whoami
root

To set up an existing or new sudo account, visit our tutorial on How to Add a User to Sudoers on Ubuntu.

To use the root account, use the following command with the root password to log in.

su

The tutorial will utilize the terminal interface, which can be found in the show applications menu.

Example:

How to Install Linux Kernel 5.16 on Ubuntu 20.04 LTS

Install PHP ImageMagick on Ubuntu

You have two choices for installation. One is the default Ubuntu 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 Ubuntu Repository

The first option is to use the official 20.04 Ubuntu 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, specific versions.

Install 7.4 PHP-IMAGICK:

sudo apt install php7.4-imagick

Install 8.0 PHP-IMAGICK:

sudo apt install php8.0-imagick

Next, verify the installation:

php -m | grep imagick

Example output:

imagick

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

The second installation choice is recommended over the default repository by installing the PPA 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.

First, install the PPA as follows:

sudo apt install software-properties-common && sudo add-apt-repository ppa:ondrej/php -y

Now install php-imagick:

sudo apt install php-imagick

Alternatively, specific versions.

Install 7.4 PHP-IMAGICK:

sudo apt install php7.4-imagick

Install 8.0 PHP-IMAGICK:

sudo apt install php8.0-imagick

Install 8.1 PHP-IMAGICK:

sudo apt install php8.0-imagick

Next, verify the installation:

php -m | grep imagick

Example output:

imagick
Advertisement

Enabling PHP-IMAGICK .SO in PHP.INI file

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

Apache

The php.ini for Apache webservers is located at /etc/php/8.0/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/8.0/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 the Apache service:

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/8.0/fpm/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/8.0/fpm/php.ini.

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

extension=imagick

To finish off, restart PHP-FPM:

sudo systemctl restart php8.0-fpm

Note, replace the 8.0 with your PHP version.

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

Advertisement

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).

Type in the HTTP:// or HTTPS://or server IP or domain name in your Internet Browser.

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 Ubuntu 20.04

Nginx Example:

How to Install PHP ImageMagick (IMAGICK) on Ubuntu 20.04

Note, your php-imagick. The path will be different if you use Apache since the test server uses Nginx.

Example:

How to Install PHP ImageMagick (IMAGICK) on Ubuntu 20.04

Comments and Conclusion

You have learned how to install php-imagick either using the Ubuntu 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.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
adplus-dvertising
0
Would love your thoughts, please comment.x
()
x