How to upgrade PHP 7 to PHP 8 version in MacOS?

I recently bought a new MacBook Air. PHP 7 is the default version installed in my MacBook Air. But I need to work on Laravel 9 and other updated PHP CMS is required the PHP 8 version.

So I faced this issue and search about it and fixed this problem.

Now I am sharing these steps in this article. So you can also fix this issue and keep the focus on your coding.

Updating PHP in a macOS machine can be difficult, you cannot just install Xampp, MAMP, or WAMP, run it, and then you have PHP 8 in your machine, you have to go through some steps.

1. Install the HomeBrew package if you haven’t this on your MacOs. Click Here Copy the installation command and paste it into your terminal window.

2. Install PHP 8 using HomeBrew Command

// Install PHP 8
// This will install the latest PHP version
brew install php
// To install a specific version, run this command:
brew install [email protected]

3. After installation command execution, Link the new PHP version first and restart the services.

brew link --force [email protected]

brew services start [email protected]

4. Check the active PHP version. It will still same version.

php -v or php --version

5. You need to run a few more commands to make sure PHP 8 starts working as the default PHP version in your macOS.

echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/[email protected]/sbin:$PATH"' >> ~/.zshr

export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib"
export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"

Setup MacOs PHP version Commands

6. After execution of the above four commands. You can open a new terminal window and check the PHP version again. It will work for you. Something like the below screenshot.

Mac Os new PHP verison