How To Build The Imagick Php Extension From Source
This article describes the process you can follow to build the Imagick PHP extension from source, which can be handy if the version you are after has not been released via PECL.
After I followed Freek's instructions, I noticed that the extention was missing a package version number that is ordinarily populated automatically when the extension is built via the PECL installer. As it turns out, one of my sites also relies on Laravel DOMPDF which requires the Imagick version number to be present for it to work correctly.
Let's go over the steps described by Freek with some additional context and code snippets.
- Clone the repository to your local development directory:
git clone https://github.com/Imagick/imagick.git
- Open the
php_imagick.hlocated in the root of the
imagickdirectory. Locate the following line:
#define PHP_IMAGICK_VERSION "@PACKAGE_VERSION@"
"@PACKAGE_VERSION@" with the latest released version. In my case this was
3.4.4, so I modified the line to this:
#define PHP_IMAGICK_VERSION "3.4.4"
- Follow the php.net guide to compile the extension.
# Navigate to the cloned directory cd /path/to/imagick # Prepare the build environment for a PHP extension. phpize # Run a script to make sure your system has the proper dependencies to compile from source. ./configure # Compile the extension. make
- Locate and copy the newly created
imagick.soextension which can be found in the
modulesdirectory inside the cloned
- Use the following command to find the directory in which your PHP version expects to find extensions:
php -i | grep extension_dir
php -i prints all php information. This output is piped (sent) to the grep command which searches the output for the given keyword -
extension_dir in this case.
This command should return something like this:
extension_dir => /usr/local/lib/php/pecl/20200930 => /usr/local/lib/php/pecl/20200930
imagick.so file to this directory.
- Use the
php --inicommand to find the loaded configuration file for your php installation. In my case, this was:
Open this file in your preferred code editor and add the following line anywhere in the file:
This will instruct php to load the imagick extension.
All being well, the Imagick extension should now be installed. You might need to restart the PHP service for this change to be registered. In my case, as a Laravel Valet user, I restarted Valet with the following command:
Finally, you can use the following command to make sure the extension is loaded correctly:
php -i | grep imagick
This should result in the following output:
imagick module => enabled
I hope this works for you as it did for me. Once again, my thanks go to Freek for most of these instructions and to Danack, the maintaner of the PHP Imagick extension. Please consider sponsoring him if your work depends on the Imagick extension.