How to Update PHP Version in Mac

Subodh Poudel Feb 02, 2024
How to Update PHP Version in Mac

In this tutorial, we will introduce some methods to update to the latest stable version of PHP. As of current, the latest stable version of PHP is PHP 8.

Use Homebrew to Update to PHP 8 in Mac

We can utilize the Homebrew package manager to upgrade the current PHP version to the latest version in Mac. Homebrew is an open-source package management system that manages the installation of software in Mac and Linux. The package manager lets the user install and update the software according to the user. Installing PHP and upgrading it to the latest version is simplified by the use of Homebrew. It is the easiest way to install and upgrade PHP in Mac. We can use the following command to check the current version of PHP.

php -v

If brew is not previously installed in your system, type the code below in the macOS terminal to install brew.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

After installing Homebrew, add it to the PATH using the following command.

echo "export PATH=/opt/homebrew/bin:$PATH" >> ~/.bash_profile
source ~/.bash_profile

To install PHP, we can use the command brew install php. After installing a fresh PHP, set PHP to PATH using the following command.

echo 'export PATH="/usr/local/opt/php8/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile

To update the PHP version, update the brew using the command brew update. Then, use the command brew upgrade php. It upgrades the current version to the latest version of PHP. Then, restart the webserver to see the changes. Use the command sudo apachectl restart to restart the Apache server. If you use Nginx, use the command sudo nginx -s reload.

If we want to switch between PHP versions, we can use the following built-in command to update to PHP 8.

brew tap shivammathur/php
brew install shivammathur/php/php@8.0

It will allow us to switch back to the previous version of PHP. We can use the following command to switch the PHP versions.

brew link --overwrite --force php@8.0

The command above will switch the current PHP version to PHP 8. If we want to switch back to the previous version, we can replace the version in the above command.

Thus, we can upgrade to the latest version of PHP using Homebrew.

Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn

Related Article - PHP Update