Enabling Imagick extension with Laravel Valet

Edit 4 February 2021: I was just revisiting this and realized that an important bit of the mkdir command below was partially missing. 😅 Apologies to anyone who ran in to confusion because of that!

After setting up Laravel Valet and MySQL with Homebrew a while ago, local development has been pretty smooth sailing. Today though, I ran in to some trouble getting the Imagick extension up and running. After some searching online, this discussion thread got me going in the right direction.

I had Homebrew and pkg-config installed already, so the first thing I did was install ImageMagick with Homebrew by running brew install imagick. Next, I installed the Imagick extension with PECL by running pecl install imagick. It’s worth keeping an eye on the output related to this installation. At the very end of the output, I got this error:

ERROR: failed to mkdir /usr/local/Cellar/php@7.2/7.2.19_1/pecl/20170718

Someone else in the discussion thread ran in to a similar problem, so I roughly followed their directions.

I ran pecl config-get ext_dir to get the extensions directory that the PECL config expects, then I copied that output and ran mkdir -p copied_directory (of course please replace copied_directory width your output) to create the necessary directory myself. I then ran pecl install imagick again, and this time there were no errors, likely since it no longer had to create the directory. Note that the output from this successful installation ended in Extension imagick enabled in php.ini.

To wrap it all up, I ran valet restart and then ran php -i | grep Imagick to check that Imagick Imagick in the PHP configuration. It returned a few lines in relation to classes and the ImageMagick version indicating that everything was set up as necessary.

Note that this only applies to the PHP version that is currently in use by Valet. I use a few different versions depending on the project, so I’ve repeated the pecl install imagick step for each of those versions as well.