Installing PHP Extensions On Mac After Homebrew’s April 2018 Update

John Danek
2 min readApr 18, 2018

--

Apparently the Homebrew Package Manager decided to nuke support for PHP extensions. Regardless of my opinions or feelings about this decision, it’s necessary to figure out a way to get PHP and its extensions to work again WITHOUT USING MACPORTS. I have some standards.

The good news is that you can still install a base PHP 7.1 via Homebrew:brew install php@7.1

#Uninstall the old Homebrew PHP 7.1
brew uninstall php71
brew install php@7.1

Next, you’ll want to make sure your Mac is using this php homebrew version:

echo 'export PATH="/usr/local/opt/php@7.1/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.1/sbin:$PATH"' >> ~/.zshrc
source ~/.zshrc

Make sure to use the appropriate .rc file: zshrc for z-shell, bashrc for bash, etc. I am obviously using z-shell.

Now if you run which php you should see:

/usr/local/opt/php@7.1/bin/php

Good. Now we have a shiny new PHP 7.1 with pecl available.

Next is to start installing extensions. I need to install the PHP memcache extension. Whereas I used to do brew install php71-memcache, we need to now use pecl instead:

pecl install memcached

Cool! I should be able to start up my PHP server and have the memcache extension available, right? It might work for you, which is awesome. Sadly, I was met with:

[exec] Warning: PHP Startup: Unable to load dynamic library '/usr/local/Cellar/php@7.1/7.1.16_1/lib/php/20160303/memcached.so' - dlopen(/usr/local/Cellar/php@7.1/7.1.16_1/lib/php/20160303/memcached.so, 9): image not found in Unknown on line 0

Balls. What is happening?

After some digging, I learned that even though I uninstalled homebrew’s old php71 and replaced it with php@7.1 (the post-nuke version), that didn’t remove the old /usr/local/etc/php/7.1 dir. So, old extension .ini files could be found in conf.d. My server was still trying to use them and thus raising errors. So, I had to do:

brew uninstall php@7.1
rm -rf /usr/local/etc/php/7.1
brew install php@7.1
echo 'export PATH="/usr/local/opt/php@7.1/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.1/sbin:$PATH"' >> ~/.zshrc
pecl install memcached
#"yes" to libmemcached because why not? I have no idea what it is!

And there we go. It finally works again on my machine.

So if your approach to PHP 7.1 on Mac like “Homebrew for base PHP, pecl for all my extensions” you should be good to go, as long as you remove remnants of the previous Homebrew PHP 7.1.

This is probably the way it always should have worked. And the Homebrew team is right. But doesn’t it feel so much better to whine and kick and scream about a change, even if it’s for the better?

DISCLAIMER: I have no idea if this is the best way to do this. But it works on my machine. And that’s all I really care about.

--

--