Adding imagick to PHP 7.4 and 8.1 on MacOS 12.6

I’m currently preparing my brand new Macbook Air for WordPress plugin development. I have installed PHP 8.1 using homebrew, a well-known package manager for most software developers using Mac. After installing WordPress, I notice WordPress complained about missing imagick library. I tried to install but encountered an error.

As per my google search, most article suggested the PHP to be installed using shivammathur package (here). Hence, I use that package to allow me to install multiple PHP version on my Mac. This is the way I use to install imagick (as per this article).

$ brew install pkg-config imagemagick
$ pecl install imagick

To my surprise, it didn’t work well. I received an error stating the file already exists.

ERROR: failed to mkdir /opt/homebrew/Cellar/[email protected]/7.4.32/pecl/20190902

I open the directory /opt/homebrew/Cellar/[email protected]/7.4.32 and I rename the file with name pecl to pecl.bak (for backup purpose. just in-case)

$ cd /opt/homebrew/Cellar/[email protected]/7.4.32
$ mv pecl pecl.bak

I re-run the pecl install imagick and everything seems well. Also, I do noted the installation directory is different from what have been defined in php.ini.

Installing ‘/opt/homebrew/Cellar/[email protected]/7.4.32/pecl/20190902’ <– Take note this

Hence, it is an expected surprise when I try to run $ php -v, I received another error.

Unable to load dynamic library ‘’

Hence, I locate my php.ini file and look for extension_dir line and change it according to the location where the imagick has been installed.

I commented the old one (just in-case) and pasted new directory

Now I’m able to run $ php -v without any errors and imagick are successfully installed. However, do note, this will only work for PHP 7.4. Due to unknown reason, you need to define the full path to imagick extension in php.ini file for PHP 8.1.

I defined full path to imagick since PHP 8.1 having issue with that.

If you found a reason and/or any other solution with the need of defining full path, kindly share with me at the comment section below 🙏.

Leave a Reply

Your email address will not be published. Required fields are marked *