If you do development work on a Mac, chances are you’re using Homebrew to install and maintain the various tools, languages, and packages that make up your local development environment. One of the most common things folks use Brew to install is PHP. Homebrew makes it very easy to install any PHP versions you’re interested in along with any extensions your environment may require. But here’s where one of the biggest Brew/PHP pain points appears: there’s no simple out-of-the-box method of managing multiple PHP versions and switching between them in Brew. We’ve all got those legacy projects that won’t work on newer versions of PHP, but at the same time we want to (or have to) use a newer version for any newer development projects! Because of this, people generally end up turning to other options (like MAMP) that provide better options for switching between the currently installed PHP versions.
This pain-point is why I built my newest side-project,
switch-php; a simple command-line script that we can use to switch PHP with one small command.
There are currently several methods of installation:
- You can install with NPM:
$ npm install --global switch-php
- You can install with Yarn:
$ yarn global add switch-php
- You can manually download the
switch-php.shfile from the
switch-phprepo and move it into your home directory. Then you’ll need to add this line in your
Once you install it via one of the above methods, you will be able to access
Here’s an bare-minimum example of how to use it:
$ switch-php 72
Here’s what we’re doing:
switch-php→ The main command. (Required)
72→ Specify the version of PHP you want to switch too, in this case we’re asking for
However, you can also pass some optional parameters that provide some additional functionality. For example:
$ switch-php 71 -v -m 512M
-v→ Request verbose output (Optional)
-m 512M→ Request a custom PHP memory setting. If you don't pass an additional argument, the memory will be reset to the Valet default. (Optional)
switch-php, give it a spin, and let me know your thoughts! Any bugs or feature requests can be added to the Github repo. If you found
switch-php useful in your workflow, please share it with your friends and help them enjoy Brewing PHP again! Thanks!