DEV Community

Cover image for Running multiple versions of PHP on MacOS with Homebrew
Sasha Blagojevic
Sasha Blagojevic

Posted on • Originally published at sasablagojevic.com

Running multiple versions of PHP on MacOS with Homebrew

I'm a big proponent of Baby Steps Doctrine, yep I think I just coined this term. What do I mean by that? Short version I like keeping things simple, now let's get to the long one.

I don't like adding unnecessary complexity if there is no need for that. I've noticed that people often add complexity to their systems and their workflow just because:

  • Everybody else seems to be doing it
  • It's a new shiny technology so it means it must be the next best solution (remember the SQL is dead long live NoSQL, now it's Restful is dead long live GraphQL, tomorrow it will be something else, yadda yadda yadda)
  • Facebook / Google / Some other big company is doing it, therefore, it must be a best practice and I should be doing it

We should be adding complexity only if there is an upside to that trade-off. Just because it works in the case of some big company does not mean it adds value to the mom and pop store you're working for, or the side hustle you're working on.

Does running and configuring Docker really add value if you're working in a small team 1-5 people or even worse if you're working alone and you're not running a distributed system? 

Maybe Homestead / Vagrant is enough? Or maybe you don't even need that and can use your local dev machine?

When I start out coding as my first step I like to use my local dev machine, a 2017 Macbook Pro. With time different projects for various reasons required different PHP versions so I had to run multiple PHP versions on my local machine.

Thanks to Homebrew you can do it fairly easy. For newer PHP versions like 7.3 it's just running: 

brew install php@7.3

For PHP 7.0/5.6 that are deprecated and not officially supported by the Homebrew team you have one additional step, you need to add a tap:

brew tap exolnet/homebrew-deprecated

brew install php@7.0

brew install php@5.6
Enter fullscreen mode Exit fullscreen mode

When you install PHP with homebrew the default location should be /usr/local/Cellar/php@7.3/ and there should be a symbolic link to /usr/local/bin/opt/php@7.3/. Check out my Stack Overflow response for more info on this topic.

To change the version of the PHP used by your shell you need to add it to your path by running:

echo 'export PATH="/usr/local/opt/php@7.3/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/php@7.3/sbin:$PATH"' >> ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

Important: This won't work if you're using the new zsh shell which came with the Catalina update because it ignores .bash_profile and .bashrc configuration files. To continue using bash as your default shell and to upgrade it to a newer version check out this awesome article.

Now, what if you want to use a different version of PHP that's installed on your system? Well, you'd have to manually change your .bash_profile to point to a different version.

export PATH="/usr/local/opt/php@7.0/bin:$PATH"
export PATH="/usr/local/opt/php@7.0/sbin:$PATH"
Enter fullscreen mode Exit fullscreen mode

Doing that manually multiple times a day can be a bit tedious so let's automate that. 

  1. Download phpver my small PHP binary script from Github gist
  2. Put it in /usr/local/bin or somewhere else and make a symbolic link to /usr/local/bin
  3. Make it executable by running chmod +x /usr/local/bin/phpver

Now you can change your active PHP version by running phpver {version_number} in your shell anywhere in the system. For example to change from version 7.3 to 7.0 you would just have to run phpver 7.0 and restart the terminal for the changes to take effect.

That's it, hope this comes in handy to someone because it sure did to me.

Have fun and keep coding! 

Top comments (1)

Collapse
 
leslieeeee profile image
Leslie

Have you tried using ServBay?
It is a good tool, especially for the beginners. It handles all PHP, MariaDB, PostgreSQL versions, plus Redis and Memcached. Run multiple PHP instances simultaneously and switch easily. This tool has made my PHP dev simpler. Worth a shot!