When developing with PHP on macOS, it’s often necessary to switch between different versions. Here’s a comprehensive guide on how to install, list, and switch PHP versions using Homebrew.
Installing the Latest PHP Version
To install the latest version of PHP, you can use Homebrew. Open your terminal and run the following command:
Check installed version:
result:
To check the installation path, use:
This should return something like:
Installing a Specific PHP Version
If you need a specific version of PHP, you can specify it during installation. For example, to install PHP 8.0, use:
Listing Installed PHP Versions
To list all the PHP versions installed via Homebrew, you can use:
Switching Between PHP Versions
Switching between different PHP versions can be done using a Bash script. Below is a script to switch PHP versions.
Create a file named php_versions.sh and add the following content:
Make the script executable:
To switch to PHP 8.0, run:
Creating Aliases for Convenience
You can create aliases to simplify the usage of the script and listing installed PHP versions. Add the following lines to your shell configuration file (e.g., .bashrc or .zshrc):
Apply the Zsh configuration with:
Now, you can switch PHP versions and list them more easily: