Install Multiple PHP Versions with Homebrew on MacOS
— Apache, Homebrew, Guide, MacOS, PHP — 1 min read
Sometimes we need different PHP versions to work in older projects. In this guide we will setup or install multiple PHP with Homebrew.
Install multiple versions of PHP
Brew currently supports the following versions: 7.2, 7.3, 7.4, 8.0.
Open the terminal and we will install the php with version of 7.4 by this command:
brew install php@7.4
The output you may get if the process is done:
The next step is edit the Apache httpd.conf from:
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
to:
LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so
and then unlink the previous version of PHP then link and switch to PHP 7.4 by this command:
brew unlink php && brew link --overwrite --force php@7.4
and the last step is restart the Apache services by this command:
brew services restart httpd
Check if the PHP version is successfuly switch to PHP 7.4 by this command:
php -v
and check it via Browser by adding <?php phpinfo(); ?> to a .php at the DocumentRoot.
We are done (YaY), to go back or switch to the previous version, we need to change Apache httpd.conf to the previous before is edited, and then type this command to unlink and link:
brew unlink php@7.4 && brew link --overwrite --force php
and of course you need to restart the Apache services too.