Amazon Linux 2: Installing ImageMagick for PHP 7.4

As part of my web server update from PHP 7.2 to PHP 7.4, I had to uninstall ImageMagick. Since WordPress prefers ImageMagick for image processing, here are the steps I followed to reinstall it. From start to finish, it took me under five minutes.

Before proceeding, this article assumes you have some server knowledge, understand configuring a web server and the web server’s configuration options, have the privileges to update your server, and have a method to recover the original server state if things go sideways.

Step 1: If not already installed, install the php-devel and gcc packages.

$ sudo yum install php-devel gcc

To check if the packages are installed, use the following commands. If either command returns an empty result (or both do), you’ll need to install that package (or both).

$ sudo yum list installed | grep php-devel
$ which gcc

Step 2: Install the ImageMagick packages

$ sudo yum install ImageMagick ImageMagick-devel ImageMagick-perl

Step 3: Compile ImageMagick packages.

$ sudo pecl install imagick

Step 4: Change the permissions on the compiled module.

$ sudo chmod 755 /usr/lib64/php/modules/

Step 5: Add imagick to the libraries that are loaded when php starts.

$ cd /etc/php.d
$ sudo vim 20-imagick.ini

You can use either vim or nano to create and edit the 20-imagick.ini file. When the file opens, enter the following lines (first line is just a comment describing file purpose).

; Enable ImageMagick extension module

Step 6: Restart the php service (php-fpm in my case), and then restart the web service.

$ sudo systemctl restart php-fpm
$ sudo systemctl restart httpd

If all goes well, you should have ImageMagick installed and running on your server in 5 minutes or less.

h/t to @AwkDenver for his answer to this question on StackOverflow which was a huge help and served as the basis for these instructions.

Disclaimer: These instructions applied to my specific development environment. What I’m hinting at, in not so many words, is that these instructions may require adjustment based on the specifics of your setup. I am more than happy to entertain questions in the comments and will do my best to assist with any issues you may run into during the upgrade process, but I cannot guarantee timely and/or accurate responses.

Also, keep in mind that the instructions were created May 2021 and that certain steps may or may not be necessary as various pieces of the technology stack are updated by their respective owners.