Install and Switch between Multiple PHP Versions with Homebrew
Homebrew proves to be a convenient method for installing and switching between various PHP versions on MacOS.
![Install and Switch between Multiple PHP Versions with Homebrew](/content/images/size/w1200/2024/04/Switch-PHP-Versions.png)
Install multiple versions
To install deprecated php versions "tap" a repository for deprecated packages
brew tap shivammathur/php
Please note shivammathur/php is a third-party tap that is not affiliated with Homebrew, please trust the contents of that tap before installing it.
Then proceed to install
brew install php@<version-1> php@<version-2>
Switch between versions
- Get the current version
php -v
- Unlink the current version
brew unlink php@<current-version>
- Link the new version
brew link php@<new-version>
If errors exist when running the above command try to overwrite
brew link --overwrite php@<new-version> --dry-run
The argument --dry-run
show the files that will be deleted double-check and remove the argument to proceed.