Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Setup multiple versions of PHP on OSX using homebrew
#!/usr/bin/env bash
# Use this script to install or re-install multiple versions of PHP
# You should be able to re-run the script multiple times; it will update/reinstall what it needs.
# Check OS
if [ "${OSTYPE//[0-9.]/}" != "darwin" ]; then
echo "This script is intended for OSX users."
# Install homebrew
if [ -z "$(which brew)" ]; then
/usr/bin/ruby -e "$(curl -fsSL"
# Intall taps
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php
# Update/upgrade everything
brew update
brew upgrade --all
# Install multiple versions of php
brew install php55
brew link php55
echo "date.timezone = UTC" > /usr/local/etc/php/5.5/conf.d/date.ini
brew install php55-memcache
brew install php55-xdebug
brew install php55-xhprof
brew unlink php55
brew install php56
brew link php56
echo "date.timezone = UTC" > /usr/local/etc/php/5.6/conf.d/date.ini
brew install php56-memcache
brew install php56-xdebug
brew install php56-xhprof
brew unlink php56
brew install php70
brew link php70
brew install --HEAD homebrew/php/php70-memcached
brew install php70-xdebug
brew unlink php70
# Setup phpenv
brew install phpenv
if [ ! -d "${HOME}/.phpenv" ]; then
if [ ! -d "${HOME}/.phpenv/versions" ]; then
mkdir -p "${HOME}/.phpenv/versions"
find "${HOME}/.phpenv/versions" -maxdepth 1 -mindepth 1 -type l -delete
for version in $(find $(find /usr/local/Cellar -maxdepth 1 -mindepth 1 -name 'php??' ) -maxdepth 1 -mindepth 1 -type d 2>/dev/null); do
ln -s ${version} ${HOME}/.phpenv/versions/ 2>/dev/null
ln -s ${version} ${HOME}/.phpenv/versions/$(echo "${version}" | perl -p -e "s/.*(\d+\.\d+)\.(\d+)$/\1/") 2>/dev/null
phpenv rehash

This comment has been minimized.

Copy link

@eporama eporama commented Jul 31, 2016

The perl RE

perl -p -e "s/.*(\d+\.\d+)\.(\d+)$/\1/"

only handles numeric minors, but 7.1.0-beta.1 and 7.1.0-beta.1_2 make this fail. Seems like we could probably go ahead and just use .+ for the third+

perl -p -e "s/.*(\d+\.\d+)\.(.+)$/\1/"

This comment has been minimized.

Copy link

@eporama eporama commented Oct 1, 2016

Just found that -name 'php??' will pick up phpmd which is installed by brew now, so

find /usr/local/Cellar -maxdepth 1 -mindepth 1 -name 'php[0-9][0-9]'

Works very well


This comment has been minimized.

Copy link

@christopher-hopper christopher-hopper commented Sep 6, 2017

@eporama, @webbj

Loved this little installer. I created a fork of a fork of this to:

  1. Clean-up and fix for latest homebrew and MacOS.
  2. Simplify symlinking of versions using bash globs.
  3. Make homebrew PHP install consistent for each version.
  4. Other minor bug fixes and refactoring.


You may want to fetch and merge some of these changes yourself.

# Clone this Gist.
git clone phpenv-osx && cd $_
# Fetch my fork.
git remote add fork
git fetch --all --prune
# Merge latest fixes.
git merge fork/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.