Skip to content

Instantly share code, notes, and snippets.

@elistone
Last active August 6, 2023 12:12
Show Gist options
  • Save elistone/474b5448304f05596556477d81344372 to your computer and use it in GitHub Desktop.
Save elistone/474b5448304f05596556477d81344372 to your computer and use it in GitHub Desktop.
MacOS Multiple PHP versions with switching.

MacOS Multiple PHP versions with switching.

PLEASE NOTE THE ORIGNAL INFORMATION FOR THIS DOCUMENT AS BEEN TAKEN FROM: https://getgrav.org/

Getting ready for install

  1. We want to make sure that we upgrade everything ready to setup
brew update
brew upgrade
brew cleanup
  1. We want to see what php verisons are currently installed
brew list --formula | grep php

This should return something like:

$ brew list --formula | grep php
php
php@7.3
  1. Lets remove these to clean everything up
brew uninstall --force php56 php56-apcu php56-opcache php56-xdebug php56-yaml
brew uninstall --force php70 php70-apcu php70-opcache php70-xdebug php70-yaml
brew uninstall --force php71 php71-apcu php71-opcache php71-xdebug php71-yaml
brew uninstall --force php72 php72-apcu php72-opcache php72-xdebug php72-yaml
brew uninstall --force php73 php73-apcu php73-opcache php73-xdebug php73-yaml
brew uninstall --force php74 php73-apcu php74-opcache php74-xdebug php74-yaml
brew uninstall --force php php-apcu php-opcache php-xdebug php-yaml
brew cleanup

You might also need to run it like this:

brew uninstall --force php@7.3 php7.3-apcu php7.3-opcache php7.3-xdebug php7.3-yaml
brew cleanup

You can run brew list --formula | grep php again to make sure everything has been removed.

  1. Finally remove old config
rm -Rf /usr/local/etc/php/*

Installing php versions

  1. Enable access to php version 8.0
brew tap shivammathur/php
  1. Install the versions of php you want
brew install shivammathur/php/php@5.6
brew install shivammathur/php/php@7.0
brew install shivammathur/php/php@7.1
brew install shivammathur/php/php@7.2
brew install shivammathur/php/php@7.3
brew install shivammathur/php/php@7.4
brew install shivammathur/php/php@8.0

You don't need to install all of the above.

  1. Your php.ini files are now located here
/usr/local/etc/php/5.6/php.ini
/usr/local/etc/php/7.0/php.ini
/usr/local/etc/php/7.1/php.ini
/usr/local/etc/php/7.2/php.ini
/usr/local/etc/php/7.3/php.ini
/usr/local/etc/php/7.4/php.ini
/usr/local/etc/php/8.0/php.ini

Quick switching

You can you can switching without any additional software using brew directly:

brew unlink php && brew link --overwrite --force php@5.6

Switch to php5.6 can be confirmed with php -v (assuming this version was installed.

brew unlink php && brew link --overwrite --force php@7.4

Switch to php7.4 can be confirmed with php -v (assuming this version was installed.

But to make switching a little easier to remember we can use this switching script

  1. Install the script
curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw/0c36a5067fbd63e6a36700a6aaa119df0836bdfc/sphp.sh > /usr/local/bin/sphp
chmod +x /usr/local/bin/sphp
  1. Make sure it can be found via your path, check path using echo $PATH to see if it contains: /usr/local/bin and /usr/local/sbin if not you can add it too your path.

  2. You can now switch php version using

sphp 7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment