Skip to content

Instantly share code, notes, and snippets.

@webbj74
Last active September 6, 2017 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save webbj74/4151020ef4f642fa80a6 to your computer and use it in GitHub Desktop.
Save webbj74/4151020ef4f642fa80a6 to your computer and use it in GitHub Desktop.
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."
exit
fi
# Install homebrew
if [ -z "$(which brew)" ]; then
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
# 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
phpenv-install.sh
fi
if [ ! -d "${HOME}/.phpenv/versions" ]; then
mkdir -p "${HOME}/.phpenv/versions"
fi
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
done
phpenv rehash
@eporama
Copy link

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/"

@eporama
Copy link

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

@christopher-hopper
Copy link

@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.

See https://gist.github.com/christopher-hopper/e8cfb878db9c1aaa8a21175f68607fdb

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

# Clone this Gist.
git clone https://gist.github.com/4151020ef4f642fa80a6.git phpenv-osx && cd $_
# Fetch my fork.
git remote add fork https://gist.github.com/e8cfb878db9c1aaa8a21175f68607fdb.git
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