(Re)installing PHP on Mac OS X
So you develop PHP on a Mac? Mebbe you, like me, are a fan of Symfony2? Then you might have noticed a few deficiencies in the pre-loaded version of PHP 5.3 on your computer. Maybe you, like me, prefer to use PHP 5.4? Here’s how to …
Fix PHP on OS X
First you’ll need Homebrew1. If you’re not already using it, what are you waiting for?
tl;dr (just give me something to copy and paste)
If you’re impatient, copy and paste this gist into a Terminal.
Otherwise, you’ll want to use one of the following options to install the Homebrew formula for PHP 5.4
Most of you will want MySQL and Apache support
brew tap josegonzalez/homebrew-php
brew install php54 --with-mysql
By default this formula comes with readline
Because honestly, who wants to run a php -a interactive shell without real interaction? If you’re masochistic, you can disable readline by adding the --without-readline flag.
But wait, there’s more!
This Homebrew formula can do PHP FPM, MSSQL and Postgres as well. All of these options are available:
--32-bit Build 32-bit only.
--homebrew-apxs Build against apxs in Homebrew prefix
--with-cgi Enable building of the CGI executable (implies --without-apache)
--with-debug Compile with debugging symbols
--with-fpm Enable building of the fpm SAPI executable (implies --without-apache)
--with-gmp Build with gmp support
--with-homebrew-openssl Include OpenSSL support via Homebrew
--with-imap Include IMAP extension
--with-intl Include internationalization support
--with-libmysql Include (old-style) libmysql support
--with-mssql Include MSSQL-DB support
--with-pgsql Include PostgreSQL support
--with-thread-safety Build with thread safety
--with-tidy Include Tidy support
--with-unixodbc Build with unixodbc support
--without-apache Build without shared Apache 2.0 Handler module
--without-bz2 Build without bz2 support
--without-mysql Remove MySQL/MariaDB support
--without-pear Build without PEAR
Using your fresh copy of PHP 5.4
You might have missed it, but Homebrew spit out a couple of caveats at the end of the install. The interesting bits probably looked something like this:
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php5_module /usr/local/opt/php54/libexec/apache2/libphp5.so
The php.ini file can be found in:
/usr/local/etc/php/5.4/php.ini
✩✩✩✩ PEAR ✩✩✩✩
If PEAR complains about permissions, 'fix' the default PEAR permissions and config:
chmod -R ug+w /usr/local/Cellar/php54/5.4.12/lib/php
pear config-set php_ini /usr/local/etc/php/5.4/php.ini
✩✩✩✩ Extensions ✩✩✩✩
If you are having issues with custom extension compiling, ensure that this php is
in your PATH:
PATH="$(brew --prefix josegonzalez/php/php54)/bin:$PATH"
PHP54 Extensions will always be compiled against this PHP. Please install them
using --without-homebrew-php to enable compiling against system PHP.
If you need to see the caveats again, run brew info php54
If you haven’t already, follow that advice
If you have any custom settings in your system-wide php.ini, copy them to the new one in /usr/local/etc/php/5.4.
Fix the PEAR config and upgrade
chmod -R ug+w `brew --prefix php54`/lib/php
pear config-set php_ini /usr/local/etc/php/5.4/php.ini
pear config-set auto_discover 1
pear update-channels
pear upgrade
You’ll need to install some PHP extensions now
Here’s a “Choose Your Own Adventure” moment. You can install packages via PECL, and have to reinstall them every time you update to a new minor PHP version, or you can install them via Homebrew.
I vote Homebrew.
Your needs will vary, but since this is my article I’ll just leave these here so I can copypaste them later:
brew install php54-intl php54-apc php54-mongo php54-memcache
Note that if you’re using Symfony2 (or many of its friends) you’ll need that php54-intl extension.
Need gearman? How ‘bout that Twig extension? You can see a list of all (50 or so) available extensions by running
brew search php54-
If you didn’t go the brew install route for your extensions, or if you use extensions which aren’t available in Homebrew yet…
You’ll need to reinstall any additional PECL packages now (and whenever you update PHP). Like all good Homebrew setups, you won’t need sudo for PEAR anymore.
Because PEAR is now installed in the Homebrew prefix, you’ll need to reinstall all of your PEAR libraries as well
Here’s what I use:
pear install pear.phpunit.de/PHPUnit PHP_CodeSniffer
After installing PEAR libraries with executables (like phpunit) you’ll need to get them into your path. Either add the PHP bin directory to your path (e.g. in ~/.bashrc):
export PATH="$(brew --prefix php54)/bin:$PATH"
… or symlink PHP binaries after running pear install ...:
brew unlink php54; brew link php54
Of course, there might be some gotchas
Offhand, I can think of one — In order to work with things like the Symfony2 console and PHPUnit, your new PHP should come before the pre-installed PHP in your $PATH. Check it like this:
which php
In my case, I see this:
/usr/local/bin/php
If my $PATH were incorrect, I’d see this instead:
/usr/bin/php
To fix it, you can add something like this to your .bashrc:
export PATH="$(brew --prefix)/bin:$PATH"