Multiple PHP versions on Mac

Multiple PHP versions on Mac


1 min read

In one day, you want to develop this project in PHP 7.4, that project in PHP 8.1,... (who knows that you have multiple projects, they could be anything: your work project, your pet projects, your package on GitHub,...)

Would be a pain to manage under 1 machine, wouldn't it? Just like node world, they have nvm (Node Version Management), but PHP doesn't have it (might have alternative but not sure how stable it s)

So here is my kind of approach to deal with this problem.

Use Brew

Indeed, brew is a super cool tool to set things up quickly. I just simply hit some commands to install the desired versions:

brew install php@7.4
brew install php@8.0
brew install php@8.1

By default, I'll EXPORT the latest PHP version as the main php of my machine.

Setup Aliases

alias php74='/opt/homebrew/opt/php@7.4/bin/php ';
alias php80='/opt/homebrew/opt/php@8.0/bin/php ';

Then I can just simply:

php74 artisan serve
php80 artisan migrate

# default - use latest
php artisan up

Run Composer which a specific version

Simply add the php version at the top, then hit this:

php74 $(which composer) install
php80 $(which composer) update

# default - use latest
composer install

Pretty simple, doesn't it? Just like me, I'm a simple man ๐Ÿ˜‰