If you tried to install any PHP extensions recently via our favourite (only) OS X package manager Homebrew – you might be used to seeing this:
➜ ~ brew install php71-redis Error: No available formula with the name "php71-redis" ==> Searching for a previously deleted formula (in the last month)... Error: No previously deleted formula found. ==> Searching for similarly named formulae... ==> Searching local taps... Error: No similarly named formulae found. ==> Searching taps... ==> Searching taps on GitHub... Error: No formulae found in taps.
Whatever PHP extension you may want to install, brew will not be able to find the correct formula. This is not a temporary problem. The maintainers of the brew project decided to remove all PHP extensions from the packet manager. I cannot judge if this was a good choice or not – I am just a user of Homebrew without any insight into the actual effort of maintaining the project. However, this did cause me to lose 2 hours of my productive flow trying to do the simplest possible action – install a PHP extension.
This highlights a deeper problem in the PHP ecosystem – the poor infrastructure for managing and installing extensions. Brew used to solve this problem by abstracting away all the detail and letting me do my job. However, looks like we will have to abandon the horse and say hello to the donkey. This donkey is called PECL and has been around for ages. In this post I will take you through the setup so you don’t waste as much time as I did.
To dig the rabbit-hole of unproductivity even deeper, PECL is also not available on brew. Which means we have to get our hands dirty.
- Run the installer
curl -O http://pear.php.net/go-pear.phar sudo php -d detect_unicode=0 go-pear.phar
- Change the Installation Base
- Press 1
- Enter “/usr/local/pear”
- Change the Binaries Directory
- Press 4
- Enter “/usr/local/bin”
Yaay, that’s it. We can now finally install our extension.
Installing the Extension
You can browse all PECL extensions in their package repository. However, you probably already know the name. If you tried to do
brew install php71-redis, your package is probably called – you guessed it “redis”.
To install it via PECL you have to:
sudo pecl install <EXTENSION_NAME>
Note: If you get an error that your system is missing “php.h” – you probably need to run
- After it compiles it will tell you which extension file you have to set in php.ini (You should add “extension=redis.so” to php.ini)
- Find your php.ini file – you can use
php -i | grep php.iniif you are not sure
- Add the extension definition to your .ini file
- Restart the PHP server that requires the extension