(Re)installing PHP on Mac OS X
So you develop PHP on a Mac? Mebbe you, like me, you might have noticed a few deficiencies in the pre-loaded version of PHP on your computer. Maybe you, like me, prefer to use a newer version of PHP? 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.6
The default options work pretty great
brew tap josegonzalez/homebrew-php brew install php56
By default this formula comes with MySQL and Apache
If you don’t want those, you can disable them with
--without-apache. Because Homebrew’s flexible like that.
But wait, there’s more!
This Homebrew formula can do PHP FPM, MSSQL and Postgres as well. As of the last time I checked, all of these options are available:
--disable-opcache Build without Opcache extension --disable-zend-multibyte Disable auto-detection of Unicode encoded scripts (PHP 5.2 and 5.3 only) --homebrew-apxs Build against apxs in Homebrew prefix --with-apache Enable building of shared Apache 2.0 Handler module, overriding any options which disable apache --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-curl Include Curl support via Homebrew --with-homebrew-libxslt Include LibXSLT support via Homebrew --with-homebrew-openssl Include OpenSSL support via Homebrew --with-imap Include IMAP extension --with-libmysql Include (old-style) libmysql support instead of mysqlnd --with-mssql Include MSSQL-DB support --with-pdo-oci Include Oracle databases (requries ORACLE_HOME be set) --with-pgsql Include PostgreSQL support --with-phpdbg Enable building of the phpdbg SAPI executable (PHP 5.4 and above) --with-thread-safety Build with thread safety --with-tidy Include Tidy support --without-bz2 Build without bz2 support --without-mysql Remove MySQL/MariaDB support --without-pcntl Build without Process Control support --without-pear Build without PEAR --HEAD install HEAD version
Using your fresh copy of PHP 5.6
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/php56/libexec/apache2/libphp5.so The php.ini file can be found in: /usr/local/etc/php/5.6/php.ini ✩✩✩✩ PEAR ✩✩✩✩ If PEAR complains about permissions, 'fix' the default PEAR permissions and config: chmod -R ug+w /usr/local/Cellar/php56/5.6.0/lib/php pear config-set php_ini /usr/local/etc/php/5.6/php.ini ✩✩✩✩ Extensions ✩✩✩✩ If you are having issues with custom extension compiling, ensure that you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH: PATH="/usr/local/bin:$PATH" PHP56 Extensions will always be compiled against this PHP. Please install them using --without-homebrew-php to enable compiling against system PHP. ✩✩✩✩ PHP CLI ✩✩✩✩ If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc, ~/.zshrc, ~/.profile or your shell's equivalent configuration file: export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH" To have launchd start php56 at login: ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents Then to load php56 now: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
If you need to see the caveats again, run
brew info php56
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
Fix the PEAR config and upgrade
chmod -R ug+w `brew --prefix php56`/lib/php pear config-set php_ini /usr/local/etc/php/5.6/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 php56-mongo php56-memcache
gearman? How ‘bout that Twig extension? You can see a list of all (50 or so) available extensions by running
brew search php56-
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
Before Composer supported global installs, this was a bit of a pain. Now, I don’t worry about it so much anymore.
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
export PATH="$(brew --prefix php56)/bin:$PATH"
… or symlink PHP binaries after running
pear install ...:
brew unlink php56; brew link php56
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:
In my case, I see this:
$PATH were incorrect, I’d see this instead:
To fix it, you can add something like this to your
export PATH="$(brew --prefix)/bin:$PATH"