How To Build The Imagick Php Extension From Source
Update: This instructions in this article should no longer be necessary as you can once again install imagick via pecl:
pecl install imagick
If you are still having issues, I would recommend this excellent article by Andrea Olivato.
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.
Thank you for reading this article.If you've made it this far, you might like to connect with me on 𝕏 where I post similar content and interact with like-minded people. If this article was helpful to you I'd really appreciate it if you would consider buying me a coffee.
Reset Your Laravel App In Seconds
In his recent talk about Laravel Nova at Laracon US, Taylor Otwell used a nice little shortcut to reset his demo app during the presentation. To reset an app in our local environment, we need to do three things:
- Drop the database.
- Migrate the database.
- Seed the database.
As of Laravel 5.5, we've been able to to use the following command to perform all three actions at once:
php artisan migrate:fresh --seed
That's still quite a lot to type if you are using this command over and over, so let's add a quick alias to our
alias mfs="php artisan migrate:fresh --seed"
Now you can simply type
mfs to quickly refresh your application and reseed it with fresh data.
While I've got your attention, Taylor's talk is well worth watching. I always learn something new from his presentations. I love the way that he can abstract a concept to the point where thousands of developers can use it in their own projects.