January 17th, 2018

Installing Imagick Extension for PHP 7 on Ubuntu 16.04

Imagick is a PHP extension that allows you to manipulate images programmatically. Some libraries or packages require that Imagick be installed on your server. However, because Imagick is optional, it's not installed with PHP by default. Here's how to install and enable Imagick on your Ubuntu server.

Installation

First thing, download and install Imagick. In Terminal, run the following commands on your server. This will install Imagick for PHP 7:

sudo apt-get update
sudo apt-get install php-imagick

Verify that Imagick is now installed. This command should output the word imagick if it was installed successfully:

php -m | grep imagick

imagick.so

Find out the directory where your PHP extensions are installed. This command should output the path to that directory (example: /usr/lib/php/20170718, your path may be different):

php-config --extension-dir

Verify that the file imagick.so exists in that directory:

ll /usr/lib/php/20170718

Edit php.ini

Now you need to edit the php.ini configuration file. To find out where this file is located, you need to look at the current php configuration. To do that, create a php info file that you can access from your browser. You may call it info.php:

// info.php
<?php phpinfo(); ?>

Load that file in your browser and look for the Loaded Configuration File setting. It should have the path to the php.ini file you need to edit (the path to your php.ini file may be different than what's shown in this example):

PHP info screenshot

Open that php.ini file:

sudo pico /etc/php/7.2/fpm/php.ini

Add the following line (you may add it at the end of the file) and save it:

extension=imagick

Restart

If you're doing this in your local development environment, restart your computer or virtual machine. If you're doing this on an actual server, restart it. You may want to try restarting your web server only (apache, nginx, etc), but this may not work. It's more effective to just do a restart on the machine so that all the changes take effect.

After restarting, verify that Imagick is available by loading your info.php file in your browser again, and this time you should see in it many references to the Imagick extension.

Clean up

Once you're done, remember to delete the info.php file you created. This file reveals a lot about your system, so you don't want it to remain there, accessible to the public. Simply delete it.