Running multiple versions of PHP on MacOS with Homebrew
Tags: php
Published: 2019-12-18 09:09:09

---
title: Running multiple versions of PHP on MacOS with Homebrew
published: true
description: 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.
tags: php
cover_image: https://sasablagojevic.com/img/front/baby_steps.jpg
canonical_url: https://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.

<p>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:</p>
<ul>
<li>Everybody else seems to be doing it</li>
<li>It's a new shiny technology so it means it must be the next best solution&nbsp;(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)</li>
<li>Facebook / Google / Some other big company is doing it, therefore, it must be a best practice and I should be doing it</li>
</ul>
<p><strong>We should be adding complexity only if there is an upside to that trade-off</strong>. Just because it works in the case of some big company does not mean it adds value to the&nbsp;<em>mom and pop store</em> you're working for, or the side hustle you're working on.</p>
<p>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?&nbsp;</p>
<p>Maybe Homestead / Vagrant is enough? Or maybe you don't even need that and can use your local dev machine?</p>
<p>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.</p>
<p>Thanks to Homebrew you can do it fairly easy. For newer PHP versions like 7.3 it's just running:&nbsp;</p>
<pre class="language-markup"><code>brew install php@7.3</code></pre>
<p>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 title="Homebrew Tap" href="https://docs.brew.sh/Taps" target="_blank" rel="noopener">a tap</a>:</p>
```
brew tap exolnet/homebrew-deprecated

brew install php@7.0

brew install php@5.6
```
<p>When you install PHP with homebrew the default location should be <strong>/usr/local/Cellar/php@7.3/</strong> and there should be a symbolic link to&nbsp;<strong>/usr/local/bin/opt/php@7.3/</strong>. Check out my <a title="PHP location on macOS when installed with Homebrew" href="https://stackoverflow.com/questions/48585578/where-does-homebrew-install-php-on-mac-high-sierra/48629909#48629909" target="_blank" rel="noopener">Stack Overflow response</a> for more info on this topic.</p>
<p>To change the version of the PHP used by your shell you need to add it to your path by running:</p>
```
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
```
<p><strong>Important</strong>: This won't work if you're using the new <em>zsh</em> shell which came with the Catalina update because it ignores&nbsp;<em>.bash_profile</em> and&nbsp;<em>.bashrc&nbsp;</em>configuration files. To continue using <em>bash</em> as your default shell and to upgrade it to a newer version check out this <a title="Upgrade bash on macOS" href="https://itnext.io/upgrading-bash-on-macos-7138bd1066ba" target="_blank" rel="noopener">awesome article</a>.</p>
<p>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&nbsp;<em>.bash_profile</em> to point to a different version.</p>
```
export PATH="/usr/local/opt/php@7.0/bin:$PATH"
export PATH="/usr/local/opt/php@7.0/sbin:$PATH"
```
<p>Doing that manually multiple times a day can be a bit tedious so let's automate that.&nbsp;</p>
<ol>
<li>Download <strong>phpver</strong> my small PHP binary script from <a title="phpver script" href="https://gist.github.com/sasa-b/883fd9810f773e3e200bdf8ea073f93b" target="_blank" rel="noopener">Github gist</a></li>
<li>Put it in&nbsp;<strong>/usr/local/bin</strong> or somewhere else and make a symbolic link to <strong>/usr/local/bin</strong></li>
<li>Make it executable by running <strong>chmod +x /usr/local/bin/phpver</strong></li>
</ol>

<p>Now you can change your active PHP version by running&nbsp;<strong>phpver {version_number}</strong> in your shell anywhere in the system. For example to change from version 7.3 to 7.0 you would just have to run&nbsp;<strong>phpver 7.0</strong> and restart the terminal for the changes to take effect.<br /><br />That's it, hope this comes in handy to someone because it sure did to me.<br /><br />Have fun and keep coding!&nbsp;</p>