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/imagick.so

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
extension=imagick

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.

8 thoughts on “Amazon Linux 2: Installing ImageMagick for PHP 7.4

  1. Pingback: Amazon Linux 2: Upgrading from PHP 7.2 to PHP 7.4 - Gregg Borodaty

  2. Rex Raymond

    Thanks for this!

    I’ve followed your guide, but the latest WordPress on AL2 still complains that imagick is not loaded or disabled.

    I ran php -s -i | grep imagick , and the results show:
    /etc/php.d/20-imagick.ini,
    imagick
    imagick module => enabled
    imagick module version => 3.7.0
    imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
    imagick.allow_zero_dimension_images => 0 => 0
    imagick.locale_fix => 0 => 0
    imagick.progress_monitor => 0 => 0
    imagick.set_single_thread => 1 => 1
    imagick.shutdown_sleep_count => 10 => 10
    imagick.skip_version_check => 0 => 0

    I’ve spent hours trying to find a solution, but no luck. Any help would be so appreciated!

    Reply
    1. Gregg Borodaty Post author

      Outside of a doing a bunch of google searches, I would set up a page to run phpinfo(). Review the output of the page to see if you see the information for the imagick module. If it is not there, make sure that the permissions are set properly on the module, that it is enabled, and that the correct php service is restarted (steps 4-6). In my case, it’s php-fpm, but it could be something different in your case depending on how you have the server setup to process php files.

      Sorry I can’t offer more specific help, but it’s difficult without knowing a lot more detail about your server setup.

      Reply
  3. NEIL A CUTCLIFFE

    Good instructions, worked for me.
    I found it was necessary to install php-pear, before Step 3. $ sudo pecl install imagick
    $ sudo yum install php-pear

    Reply
  4. Chirag N

    Thank you so much for all the steps; I can install it successfully.

    Any idea how to update maria’s DB from the previous version to the latest? I am running a WordPress blog.

    Cheers!!!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *