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.
Installing PECL
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 https://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:
- Run
sudo pecl install <EXTENSION_NAME>
Note: If you get an error that your system is missing “php.h” – you probably need to runxcode-select --install
- 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.ini
if you are not sure - Add the extension definition to your .ini file
- Restart the PHP server that requires the extension
Doesn’t seem to work. After running this I got:
sudo php -d detect_unicode=0 go-pear.phar
404 Not Found
Not Found
The requested URL /go-pear.phar was not found on this server.
Apache/2.4.25 (Debian) Server at pear.php.net Port 80
Got an error when applying it:
Build process completed successfully
Installing ‘/usr/lib/php/extensions/no-debug-non-zts-20160303/redis.so’
ERROR: failed to write /usr/lib/php/extensions/no-debug-non-zts-20160303/redis.so (copy(/usr/lib/php/extensions/no-debug-non-zts-20160303/redis.so): failed to open stream: Operation not permitted)
when i execute sudo php -d detect_unicode=0 go-pear.phar in mac mojave, i get html error
301 Moved Permanently
Moved Permanently
The document has moved here.
For people having issues when executing `sudo php -d detect_unicode=0 go-pear.phar`, you need to change the previous command to `curl -O https://pear.php.net/go-pear.phar`, this will correctly download the phar file and not the HTML redirect page.