Install Imagick extension with webp support on ubuntu 16.04/18.04/20.04 with php-fpm

, , Leave a comment

In this post we will be going to see how to install Imagick extension with webp support on ubuntu 16.04/18.04/20.04 with php-fpm. Direct installation of sudo apt-get install php-imagick and php-imagick module do not come with webp support. Inorder to install imagick extension with webp support it needs to be compiled and installed from source.

The installation requires below steps to be performed.

  1. Compile libwebp from source.
  2. Compile imagemagick from source.
  3. Compile php-imagick from source and then phpize it.

 

1. Compile libwebp from source:

sudo apt-get install libjpeg-dev libpng-dev
sudo wget https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-1.1.0.tar.gz
sudo tar xvzf libwebp-1.1.0.tar.gz
cd libwebp-1.1.0
./configure
make
sudo make install

2. Compile imagick from source, configured with webp

sudo apt-get update
sudo apt build-dep imagemagick
sudo wget https://imagemagick.org/download/ImageMagick.tar.gz
sudo tar xvzf ImageMagick.tar.gz
cd ImageMagick-v.x.x #Replace ImageMagick-v.x.x with the corrent directory name after extracting tar
./configure --with-webp=yes
sudo make
sudo make install
sudo ldconfig /usr/local/lib
sudo identify -version # to check if installed correctly
sudo make check # optional run in-depth check

3(A) Install extension with pecl

First find the php version and the configuration ini file location of it. Update the php version in the statement with the php version installed on your server.


sudo apt-get update
sudo apt-get install imagemagick gcc libmagickwand-dev php-pear php7.4-dev
sudo pecl install imagick
sudo echo "extension=imagick.so" >> /etc/php/7.4/fpm/php.ini
sudo echo "extension=imagick.so" >> /etc/php/7.4/cli/php.ini
sudo service php7.4-fpm restart

3(B) Compile php imagick from source

Either you install through step 3(A), incase if that doesn’t work then follow this step. First find the php version and the configuration ini file location of it. Update the php version in the statement with the php version installed on your server.


sudo wget https://pecl.php.net/get/imagick-3.4.3.tgz
sudo tar xvzf imagick-3.4.3.tgz
cd imagick-3.4.3
sudo /usr/bin/phpize7.2      # note: find you version of phpize with locate phpize
sudo ./configure
sudo make
sudo make install
sudo echo "extension=imagick.so" >> /etc/php/7.4/fpm/php.ini
sudo echo "extension=imagick.so" >> /etc/php/7.4/fpm/php.ini
sudo service php7.4-fpm restart

To confirm installation is successful and webp is showing in supported formats, check with
below commands. If webp format lists there along with other formats that means installation is successful.


php -r "print_r(Imagick::queryFormats());"
php -i | grep ImageMagick