How do i change php version in terminal mac?

Homebrew, the popular package manager for macOS, allows you to install more than one version of PHP.

Now, let us assume you have installed PHP 7.4 and PHP 8.1 with HomeBrew. If you want to switch from PHP 7.4 to PHP 8.1, you need to run the following commands:

brew unlink 

and

brew link  --force --overwrite

How about this instead?

8.1

The following shell script (added to ~/.bashrc or ~/.zshrc, whichever shell you prefer) creates aliases for every single PHP version you have installed with Homebrew:

# determine versions of PHP installed with HomeBrew
installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))

# create alias for every version of PHP installed with HomeBrew
for phpVersion in ${installedPhpVersions[*]}; do
    value="{"

    for otherPhpVersion in ${installedPhpVersions[*]}; do
        if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
            continue
        fi

        # unlink other PHP version
        value="${value} brew unlink php@${otherPhpVersion};"
    done

    # link desired PHP version
    value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"

    alias "${phpVersion}"="${value}"
done

💡 macOS ships with grep, but that version does not support Perl-compatible regular expressions (PCREs) as used above.

Run

brew install grep

to install the latest version of grep. Homebrew installs grep wit the prefix g, hence ggrep.

With this script in place, when I open a new terminal and run

alias | grep php

I can see that the script created aliases as expected.

7.2='{ brew unlink ; brew unlink ; brew unlink ; brew unlink ; brew link  --force --overwrite; } &> /dev/null && php -v'
7.3='{ brew unlink ; brew unlink ; brew unlink ; brew unlink ; brew link  --force --overwrite; } &> /dev/null && php -v'
7.4='{ brew unlink ; brew unlink ; brew unlink ; brew unlink ; brew link  --force --overwrite; } &> /dev/null && php -v'
8.0='{ brew unlink ; brew unlink ; brew unlink ; brew unlink ; brew link  --force --overwrite; } &> /dev/null && php -v'
8.1='{ brew unlink ; brew unlink ; brew unlink ; brew unlink ; brew link  --force --overwrite; } &> /dev/null && php -v'

To switch from any version of PHP to PHP 8.1, I now only need to run

8.1

I hope it helps!

I've got a brand new Mac yesterday and noted that it states PHP will be removed from future Mac OS versions by default.

I'm pretty surprised they go this way. By default, it comes with PHP 7.3, and I needed 7.4 for my project so let me guide you through setting up PHP on your Mac!

Mac's warning looks like this:

WARNING: PHP is not recommended
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.

However, don't be scared. It's pretty easy to install PHP and even install multiple versions if you like.

Installing Homebrew

When it comes to installing software on your Mac, we need only one package manager, and it's Homebrew.

It can install any package or software you want and even install specific versions.

Read more on Homebrew - Package manager for Mac.

Quick guide: Run the following command in your terminal:

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

Installing PHP with Homebrew on Mac

To install PHP, we can run the following command:

brew install php

This will install the latest stable version of PHP (At the moment of writing, this is PHP 8.0).

Before running any brew commands, it's a good habit to run the following commands first. These will check if Homebrew is all up to date and running the latest versions.

brew update
brew doctor

Installing PHP 7.4 with Homebrew

In my case, I wanted to install PHP 7.4 since it's the version our server is running.

To install a specific version, we can use the @ notation.

brew install [email protected]

This will run the installer, and it should end with a success notice in your terminal.

However, even though this installed PHP, it didn't change our running instance yet.

So if we run the php -v command, we might still see a different version like PHP 7.3.14 (CLI) or whatever you have installed.

To fix this, we need to link the correct PHP version.

Switching PHP Versions with Homebrew on Mac

Now that we installed versions, we can easily switch between them using the link command.

First, check which version of PHP is currently running:

php -v

# PHP 8.0.1 (cli) (built: Jan  8 2021 01:27:28) ( NTS )
# Copyright (c) The PHP Group

Then we can unlink that version by using:

brew unlink [email protected]

The next step is to link the version we want:

brew link [email protected]

It will tell you to run a script to add the path:

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

This will make sure the right PHP version is loaded, now if you run php -v again it should show:

# PHP 7.4.14 (cli) (built: Jan  8 2021 01:35:35) ( NTS )
# Copyright (c) The PHP Group

And there we go. We switched to the PHP version.

Php -v is still showing the wrong version

I had the issue when upgrading from 7.4 to 8.0 for my demo that I kept seeing 7.4 when running php -v. Remove the old line in your .zshrc file to fix this manually.

nano ~/.zshrc

Remove the line that points to your old instance of PHP.

export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"

Note: This is an example of my version. It might differ from what you installed before.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

How do I change PHP version on Mac?

Switch Between PHP Versions.
PHP 8.1 brew install php. Update the path: echo 'export PATH="/opt/homebrew/opt/php/bin:$PATH"' >> ~/.zshrc echo 'export PATH="/opt/homebrew/opt/php/sbin:$PATH"' >> ~/.zshrc..
PHP 8.0 brew install [email protected]. ... .
PHP 7.4 arch -arm64 brew install [email protected]..

How do I update PHP in Terminal?

In short what you need to do is:.
Ensure the php packages for the version you want are installed..
Update any configuration for that PHP version..
Update your web server configuration to point to the correct PHP version..
Point PHP CLI to the correct PHP version..
Restart your web server and php-fpm..
Mask the old php-fpm daemon..

How do I upgrade PHP to 7.4 on Mac?

6 Answers.
brew install [email protected]..
brew link --force --overwrite [email protected]..
brew services start [email protected]..
export PATH="/usr/local/opt/[email protected]/bin:$PATH".
export PATH="/usr/local/opt/[email protected]/sbin:$PATH".

How do I find PHP version on Mac?

Within the Terminal, run vim ~/. bash_profile Type i and then paste the following at the top of the file: export PATH=/user/local/bin:$PATH Hit ESC, Type :wq, and hit Enter In Terminal, run source ~/. bash_profile In Terminal, type in which php again and look for the updated string.