Install an Updated ImageMagick Version

ImageMagick shipped for Ubuntu 20.04 is based on version 6, the corresponding php-imagick wrapper on version 3.4 which does not have additional capabilities to render particular image types like HEIC or SVG. To install the latest version with many additional image and video capabilities for use with PHP, you must first uninstall and remove the former version of ImageMagick-6 and the old php wrapper and install ImageMagick-7 and the new php-imagick wrapper version +3.5.

Backup the ImageMagick Configuration Files

In case you have made any changes to the configuration files for ImageMagick-6, we recommend to back them up for easy transition to ImageMagick-7 with the following command:

sudo cp -rp /etc/ImageMagick-6 /etc/ImageMagick-6.backup
After installing ImageMagick-7 and if you do not define the configuration location, the default configuration files can be found at /usr/local/etc/ImageMagick-7. See also the output of the installation script used regarding the various directories set. Use the backup files as base to update them.

Remove the Old ImageMagick-6 Installation

Remove php-imagick

  1. Check if php-imagick is installed:

    dpkg -l | grep php | awk '{print $2}' | tr "\n" " " | grep php-imagick

    You will see the name printed if it is installed.

  2. Check if the imagick.so library is installed:

    ls `php -i | grep "^extension_dir" | sed -e 's/.*=> //'` | sort | grep imagick
  3. Check the installed php-imagick version:

    php --ri imagick | grep -i "module version"

    If both the php-imagick library and the imagick.so binary is installed, proceed with the next steps.

  4. Disable php-imagick:

    sudo phpdismod imagick
  5. Remove php-imagick:

    sudo apt remove php-imagick
  6. Depending on the installation, restart Apache or php-fpm:

    sudo service apache2 restart

    or

    sudo service php7.4-fpm restart

Remove ImageMagick-6

  1. Check which ImageMagick version is installed. The version may differ from our example output.

    convert -version  | grep -i version
    
    Version: ImageMagick 6.9.7-4
  2. Remove the old imagemagick-6 version:

    sudo apt remove imagemagick-6-common

Install ImageMagick 7

Install the Latest ImageMagick-7 Binary

To install ImageMagick-7, a script is used. Alternatively, you can copy&paste all installation commands step by step from IMEI - ImageMagick Easy Install. See the README description for more information on options and parameters. IMEI uses Checkinstall for ease of removing/uninstalling ImageMagick 7 and its components.

  1. Change to the /tmp directory:

    cd /tmp
  2. Download and check the signature of the installation script which is done in four steps:

    1. Download the IMEI script

    2. Download signature file

    3. Download public key

    4. Verify the installer

    Run this example to do all steps in one chained command:

    wget https://dist.1-2.dev/imei.sh && \
    wget https://dist.1-2.dev/imei.sh.sig && \
    wget https://dist.1-2.dev/imei.sh.pem && \
    openssl dgst -sha512 -verify imei.sh.pem -signature imei.sh.sig imei.sh
  3. If you get a Verified OK message, make the script executable:

    sudo chmod +x imei.sh
  4. Install the latest ImageMagick-7 release:

    For Ubuntu, ImageMagick uses /etc as base for the config directory, see the backup information above. This installation example uses the same base set by an option. Change it according your needs.
    Depending on your environment, this may take a while (+25min).
    sudo ./imei.sh --config-dir "/etc"
  5. Check if ImageMagic-7 and its libraries have been properly installed

    dpkg -l | grep imei
  6. Remove the downloaded script and verification files:

    rm imei.*

Check the Installed ImageMagick-7 Version

Check the version installed. The version printed may be different than in the example output.

convert -version | grep -i version
Version: ImageMagick 7.1.0-2 ...

Get a List of Supported Formats

Type the following commands to get a list of supported formats:

convert identify -list format
   Format  Module    Mode  Description
----------------------------------------------------
      3FR  DNG       r--   Hasselblad CFV/H3D39II
      3G2  VIDEO     r--   Media Container
      3GP  VIDEO     r--   Media Container
      AAI* AAI       rw+   AAI Dune image
...

Reuse Changed Configuration Settings

If you have changed configuration settings, you can reuse them for ImageMagick 7. Copy either the changed contend of the files in question or the complete files from /etc/ImageMagick-6.backup to /etc/ImageMagick-7. You may want to keep a backup of the original configuration files.

Secure ImageMagick

Make sure to disable ImageMagick’s scripting language in
/etc/ImageMagick-7/policy.xml
by adding or uncommenting this policy:

<policy domain="coder" rights="none" pattern="MSL" />

See ImageTragick for more information and context.

Install the New ImageMagick PHP Wrapper

The new php-imagick wrapper is installed via PECL and uses the recently installed ImageMagick-7 version as base.

If you have installed the php-wrapper via PECL before and want to reinstall it, you will get a warning that it is already installed. You must remove it first with sudo pecl uninstall imagick.
  1. Install php-imagick

    The printf command auto-accepts the question for using defaults.

    sudo pecl channel-update pecl.php.net
    printf "\n" | sudo pecl install imagick
  2. Check if file imagick.ini is present in mods-available.

    Use your php version in the path of the example command below:

    ll /etc/php/7.4/mods-available/imagick.ini

    If the file is not present, create one:

    sudo nano /etc/php/7.4/mods-available/imagick.ini

    with following content:

    ; configuration for php imagick module
    extension=imagick.so

Enable the php-imagick wrapper

  1. After ImageMagick-7 and the php wrapper have been installed, enable the php wrapper:

    sudo phpenmod imagick
  2. Depending on the installation, restart Apache or php-fpm:

    sudo service apache2 restart
    or
    sudo service php7.4-fpm restart
  3. Print supported php-imagick formats:

    php -r 'phpinfo();' | grep -i "ImageMagick supported formats"

TIPS

Reinstall or upgrade ImageMagick-7 and the php wrapper.

To reinstall or upgrade ImageMagick-7, follow the principle steps described above by disabling and removing the php wrapper first, rerun the imei.sh installation script with the options of choice. The script checks if components need an upgrade and, if that’s the case, installs them. Then reinstall the php wrapper, enable it and restart your web server or php-fpm.

Uninstall ImageMagic-7 or components

If you want to uninstall ImageMagick-7 only, run:

sudo apt remove imei-imagemagick

If you want to completely remove ImageMagic-7 and all of its installed components, run:

sudo apt remove imei-imagemagick,imei-libaom,imei-libheif,imei-libjxl

Change Configuration Settings of ImageMagick-7

You can change configuration settings of ImageMagick-7 at any time according to your needs. In case you do so, restart your web server of the php-fpm service post changing the settings so they can take effect for web services.

Previews Configuration Rules

If you need to configure or enable previews for formats not enabled by default, see Notes for PDF Preview Generation which can serve as a template.