Tutorial – Installing ImageMagick on Amazon Linux 2 (From Source)

Prerequisites:

  • Docker (see: Docker installation).
  • Copy-and-paste skills (minimal thinking required).
  • Cup of coffee (while the commands run).

Step 1: Create an Installation Folder

Create a folder on your development machine for the ImageMagick binaries:

mkdir imagemagick
cd imagemagick

(Don't forget to cd into this directory.)

Step 2: Run an Amazon Linux 2 Docker Container

Run a docker container to build ImageMagick with:

docker run \
  --rm -it \
  --platform linux/amd64 \
  -v $(pwd):/root/result \
  amazonlinux:2

Notes:

  • Use --platform linux/amd64 to compile for x86.
  • Use --platform linux/arm64/v8 to compile for ARM64 (Amazon's new Graviton processors). This option is slow (from non-ARM build machines).

All remaining commands in this tutorial must be run inside the container...


Step 3: Install OS Packages

Install the following OS packages (required by ImageMagick's build process):

yum install -y git gcc gcc-c++ cpp cpio make cmake automake autoconf chkconfig clang clang-libs dos2unix zlib zlib-devel zip unzip tar perl libxml2 bzip2 bzip2-libs xz xz-libs pkgconfig libtool

Step 4: Install ImageMagick's Dependencies

Skip any modules you don't need to support:

Optional: libjpeg

If you require .jpeg support:

cd /root
curl https://github.com/winlibs/libjpeg/archive/refs/tags/libjpeg-9c.tar.gz -L -o tmp-libjpeg.tar.gz
tar xf tmp-libjpeg.tar.gz
cd libjpeg*

dos2unix *
dos2unix -f configure
chmod +x configure

PKG_CONFIG_PATH=/root/build/cache/lib/pkgconfig \
  ./configure \
    CPPFLAGS=-I/root/build/cache/include \
    LDFLAGS=-L/root/build/cache/lib \
    --disable-dependency-tracking \
    --disable-shared \
    --enable-static \
    --prefix=/root/build/cache

dos2unix -f libtool

make
make install

Optional: libpng

If you require .png support:

cd /root
curl http://prdownloads.sourceforge.net/libpng/libpng-1.6.37.tar.xz -L -o tmp-libpng.tar.xz
tar xf tmp-libpng.tar.xz
cd libpng*

PKG_CONFIG_PATH=/root/build/cache/lib/pkgconfig \
  ./configure \
    CPPFLAGS=-I/root/build/cache/include \
    LDFLAGS=-L/root/build/cache/lib \
    --disable-dependency-tracking \
    --disable-shared \
    --enable-static \
    --prefix=/root/build/cache

make
make install

Optional: libwebp

If you require .webp support:

cd /root
curl https://github.com/webmproject/libwebp/archive/v1.2.1.tar.gz -L -o tmp-libwebp.tar.gz
tar xf tmp-libwebp.tar.gz
cd libwebp*

sh autogen.sh

PKG_CONFIG_PATH=/root/build/cache/lib/pkgconfig \
  ./configure \
    CPPFLAGS=-I/root/build/cache/include \
    LDFLAGS=-L/root/build/cache/lib \
    --disable-dependency-tracking \
    --disable-shared \
    --enable-static \
    --prefix=/root/build/cache

make
make install

Optional: libde265 (HEIC support 1 of 2)

If you require .heic support, you'll need both libde265 and libheif.

The following installs libde265:

cd /root
curl https://github.com/strukturag/libde265/releases/download/v1.0.8/libde265-1.0.8.tar.gz -L -o tmp-libde265
tar xf tmp-libde265
cd libde265*

sh autogen.sh

PKG_CONFIG_PATH=/root/build/cache/lib/pkgconfig \
  ./configure \
    CPPFLAGS=-I/root/build/cache/include \
    LDFLAGS=-L/root/build/cache/lib \
    --disable-dependency-tracking \
    --disable-shared \
    --enable-static \
    --prefix=/root/build/cache

make
make install

Optional: libheif (HEIC support 2 of 2)

The following installs libheif :

cd /root
curl https://github.com/strukturag/libheif/releases/download/v1.12.0/libheif-1.12.0.tar.gz -L -o tmp-libheif.tar.gz
tar xf tmp-libheif.tar.gz
cd libheif*

sh autogen.sh

PKG_CONFIG_PATH=/root/build/cache/lib/pkgconfig \
  ./configure \
    CPPFLAGS=-I/root/build/cache/include \
    LDFLAGS=-L/root/build/cache/lib \
    --disable-dependency-tracking \
    --disable-shared \
    --enable-static \
    --prefix=/root/build/cache

make
make install

Optional: libopenjp2

If you require .jp2 / JPEG 2000 support:

cd /root
curl https://github.com/uclouvain/openjpeg/archive/v2.4.0.tar.gz -L -o tmp-libopenjp2.tar.gz
tar xf tmp-libopenjp2.tar.gz
cd openjpeg*

mkdir -p build
cd build

PKG_CONFIG_PATH=/root/build/cache/lib/pkgconfig \
  cmake .. \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/root/build/cache \
    -DBUILD_SHARED_LIBS:bool=off \
    -DBUILD_CODEC:bool=off

make clean
make install

Optional: libtiff

If you require .tiff support:

cd /root
curl http://download.osgeo.org/libtiff/tiff-4.3.0.tar.gz -L -o tmp-libtiff.tar.gz
tar xf tmp-libtiff.tar.gz
cd tiff*

PKG_CONFIG_PATH=/root/build/cache/lib/pkgconfig \
  ./configure \
    CPPFLAGS=-I/root/build/cache/include \
    LDFLAGS=-L/root/build/cache/lib \
    --disable-dependency-tracking \
    --disable-shared \
    --enable-static \
    --prefix=/root/build/cache

make
make install

Optional: libbz2

BZIP is required by some formats (e.g. TIFF) for image compression.

cd /root
curl http://prdownloads.sourceforge.net/bzip2/bzip2-1.0.6.tar.gz -L -o tmp-bzip2.tar.gz
tar xf tmp-bzip2.tar.gz
cd bzip2*

make libbz2.a
make install PREFIX=/root/build/cache

Optional: lcms

If you need to work with ICC color profiles:

cd /root
curl https://github.com/mm2/Little-CMS/releases/download/lcms2.12/lcms2-2.12.tar.gz -L -o tmp-lcms2.tar.gz
tar xf tmp-lcms2.tar.gz
cd lcms2*

sh autogen.sh

PKG_CONFIG_PATH=/root/build/cache/lib/pkgconfig \
  ./configure \
    CPPFLAGS=-I/root/build/cache/include \
    LDFLAGS=-L/root/build/cache/lib \
    --disable-dependency-tracking \
    --disable-shared \
    --enable-static \
    --prefix=/root/build/cache

make
make install

Step 5: Build ImageMagick on Amazon Linux 2

With the above dependencies installed, we're ready to compile ImageMagick:

cd /root
curl https://github.com/ImageMagick/ImageMagick/archive/7.0.8-45.tar.gz -L -o tmp-imagemagick.tar.gz
tar xf tmp-imagemagick.tar.gz
cd ImageMagick*

PKG_CONFIG_PATH=/root/build/cache/lib/pkgconfig \
  ./configure \
    CPPFLAGS=-I/root/build/cache/include \
    LDFLAGS="-L/root/build/cache/lib -lstdc++" \
    --disable-dependency-tracking \
    --disable-shared \
    --enable-static \
    --prefix=/root/result \
    --enable-delegate-build \
    --disable-installed \
    --without-modules \
    --disable-docs \
    --without-magick-plus-plus \
    --without-perl \
    --without-x \
    --disable-openmp

make clean
make all
make install

The Result

Exit the docker container and run ls -la to see the compiled ImageMagick binaries:

bash-4.2# ls -la
total 28
drwxr-xr-x 7 root root 4096 May  2 11:04 .
dr-xr-x--- 1 root root 4096 May  2 11:04 ..
drwxr-xr-x 2 root root 4096 May  2 11:04 bin
drwxr-xr-x 3 root root 4096 May  2 11:04 etc
drwxr-xr-x 3 root root 4096 May  2 11:04 include
drwxr-xr-x 4 root root 4096 May  2 11:04 lib
drwxr-xr-x 3 root root 4096 May  2 11:04 share

This entire folder can be copied to any Amazon Linux 2 EC2 instance (or AWS Lambda function).

You won't need to install any other dependencies — making it perfect for AWS Lambda functions! Just call ./bin/magick and enjoy...

bash-4.2# ./bin/magick -version
Version: ImageMagick 7.0.8-45 Q16 x86_64 2022-05-02 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI
Delegates (built-in): bzlib heic jng jp2 jpeg lcms png tiff webp zlib

Final Thoughts

Here's a working Dockerfile that codifies the above steps:

https://github.com/upload-io/aws-image-magick-example

Remember: when executing docker run you must ensure the --platform flag matches the processor architecture of the AWS EC2 instance or AWS Lambda function you'll be running the binary on.