Setup multiple PHP Versions on macOS
Introduction:
Running different PHP versions is often necessary for developers working on various projects or dealing with older scripts. In this guide, we’ll walk you through the steps to install and manage multiple PHP versions on macOS using Homebrew, a powerful package manager.
XCode Command Line Tools:
If you haven’t installed XCode, you’ll need it for Homebrew. Open your Terminal and run the following command:
xcode-select --install
Install Homebrew:
Homebrew is a package manager for macOS. Install it by running the following command in your Terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Verify the installation with:
brew --version
Output:
Homebrew 4.1.21
Ensure everything is configured correctly by running:
brew doctor
Install Multiple PHP Versions:
Homebrew supports various PHP versions. To install older versions, tap into the shivammathur/php repository
brew tap shivammathur/php
Install the desired PHP versions, for example:
brew install shivammathur/php/php@5.6
brew install shivammathur/php/php@7.0
brew install shivammathur/php/php@7.1
brew install shivammathur/php/php@7.2
brew install shivammathur/php/php@7.3
brew install shivammathur/php/php@7.4
brew install shivammathur/php/php@8.0
Adjust PHP Configuration:
Customize PHP settings as needed. Configuration files are located in the following directories:
/usr/local/etc/php/5.6/php.ini
/usr/local/etc/php/7.0/php.ini
/usr/local/etc/php/7.1/php.ini
/usr/local/etc/php/7.2/php.ini
/usr/local/etc/php/7.3/php.ini
/usr/local/etc/php/7.4/php.ini
/usr/local/etc/php/8.0/php.ini
Switch Between PHP Versions:
Use the following command to switch between PHP versions (e.g., switch from PHP 7.4 to PHP 5.6):
brew unlink php@7.4 && brew link --force --overwrite php@5.6
Check Active PHP Version:
Verify the currently enabled PHP version:
php -v
Conclusion: Effectively managing multiple PHP versions on macOS is crucial for developers working on diverse projects. Homebrew simplifies the process, allowing seamless installation, configuration, and switching between PHP versions. By following these steps, you can ensure a smooth development experience tailored to the requirements of your projects.