25

I have High Sierra installed and it comes with php 7.1. During the environment I ended up being able to upgrade to php7.2 but wasn't able to document it, so I dont exactly know what I did. Now, I am trying to switch to php 7.3

Using brew, I ran the following commands:

brew unlink [email protected]

brew services install [email protected]

brew link [email protected]

If I restart my terminal and check for the php version:

php -v

I still see 7.2.25 version and not 7.3 as I desire

I also tried with a node package that I found in this link here but no success.

How do I successfully switch between php versions?

1
  • Actually it is possible to have multiple PHP versions installed and switch between them. Not sure what exactly wrong in your case, but take a look at the article, maybe it will give you some clues: hexlator.blogspot.com/2018/04/… Commented Dec 2, 2019 at 16:20

10 Answers 10

74

Here is my installation script:

Now my output would be as:

$ php -v
PHP 7.2.25 (cli) (built: Nov 22 2019 10:27:28) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.25, Copyright (c) 1999-2018, by Zend Technologies

I think the PATH environment setup is something matters. And it does show in instructions as part of the installation process though.

Hope it helps resolving your issue.

Sign up to request clarification or add additional context in comments.

3 Comments

in my terminal it's showing the updated version but php is not working at all
close your terminal and reopen terminal and check by php -v
I had errors for the first 3 above and finally the export path fixed it!
13

Since I had to face this issue as well, let me share how to make this work. If you have to switch back and forth on mac then this is what works for me.

Let's say you have multiple PHP versions installed 7.2 and 7.4

Services List and Current version

Now my current PHP version is 7.4 & I have to switch back to 7.2, steps will be.

  1. brew unlink [email protected] && brew link [email protected] --force

  2. nano ~/.zshrc -> Update Export Path From 7.4 to 7.2

    zshrc file

  3. Save It.

  4. brew services stop [email protected]

  5. brew services start [email protected]

enter image description here

Voila. To go back to 7.4 Run brew unlink [email protected] && brew link [email protected] --force & uncomment export files. That's it.

2 Comments

Nothing worked for me until I edited the PATH variable (with the nano ~./zshrc... command), and removed the references to old versions. +1
Adding the paths above as text for easy copying: #export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH" #export PATH="/opt/homebrew/opt/[email protected]/sbin:$PATH" export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH" export PATH="/opt/homebrew/opt/[email protected]/sbin:$PATH"
9

Open the terminal then run

nano ~/.zshrc

In the file that you open, you'll find the exported path of PHP as follows:

#export PATH="/usr/local/opt/[email protected]/bin:$PATH"
#export PATH="/usr/local/opt/[email protected]/sbin:$PATH"
export PATH="/usr/local/opt/[email protected]/sbin:$PATH"

Then comment the old version by adding # at the first of line and save file CTRL+x

after that close the terminal or open new one then get the php version again

php --version

I hope you have completely switched to the new PHP version

Comments

7

Until I restarted Terminal I kept seeing the old version.

Comments

2

here is my approach to switch between php7.3 and php 8.2 and want to use 8.2

PHP 7.3.33 (cli) (built: Jul 13 2023 17:29:11) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.3.33, Copyright (c) 1999-2018, by Zend Technologies

we can change to php 7.3 by this steps

PHP 8.2.10 (cli) (built: Aug 31 2023 18:52:55) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.10, Copyright (c) Zend Technologies with Zend OPcache v8.2.10, Copyright (c), by Zend Technologies

Comments

2

You can easily switch between any number of PHP versions on MacOS. I had the same problem while switching between PHP 7.4 and 8.3. Just follow the steps mentioned below and you are ready to go.

First install the PHP versions you need. If you've already installed it, you can skip this step or it'll tell you that the version is already installed.

brew install [email protected]
brew install [email protected]

Now if you are using the default terminal on macOS, you'll use /~.bash_profile but if you are using zsh terminal, use /~.zshrc

sudo nano /~.zshrc

Add these lines at the end of that file

# Add PHP 7.4 paths
export PATH="/usr/local/opt/[email protected]/bin:$PATH"
export PATH="/usr/local/opt/[email protected]/sbin:$PATH"

# Add PHP 8.3 paths
export PATH="/usr/local/opt/[email protected]/bin:$PATH"
export PATH="/usr/local/opt/[email protected]/sbin:$PATH"

# Aliases
alias usephp8='brew unlink [email protected] && brew link --overwrite --force [email protected] && export PATH="/usr/local/opt/[email protected]/bin:$PATH" && export
PATH="/usr/local/opt/[email protected]/sbin:$PATH"'
alias usephp7='brew unlink [email protected] && brew link --overwrite --force [email protected] && export PATH="/usr/local/opt/[email protected]/bin:$PATH" && export
PATH="/usr/local/opt/[email protected]/sbin:$PATH"'

Now run this command

source ~/.zshrc

To switch between php8.3 and php7.4, use these aliases

usephp7
usephp8

You can also run a PHP project at localhost without installing XAMPP. Open terminal and go to the project path and run this command

php -S localhost:8000

1 Comment

you sire are the real mvp thanks
1

Most of these answers is for php-cli (Command Line Interface). It's for when you use php scripts from command line only. With a

php -v

PHP 8.1.29 (cli) (built: Jun 5 2024 05:51:57) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.29, Copyright (c) Zend Technologies with Zend OPcache v8.1.29, Copyright (c), by Zend Technologies

You only get CLI php version. It's not the same as php-FPM. If you want to know what version apache uses there are other ways. You can use in your php file

echo PHP_VERSION;

or function to know more details (including version)

phpinfo();

And load it via browser. That's why i'm writing this comment. I'm new to mac and was looking for a way to make httpd use other versions. brew link && brew unlink only changes CLI php versions for me, but not what httpd uses. I don't know if it's a bug or i did something wrong. Only way i can change php versions httpd uses is to modify my httpd.conf (/opt/homebrew/etc/httpd/httpd.conf) file: I commented the line:

#LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so

Because "/opt/homebrew/opt/php/" is a link to php version that apache uses. Add some lines and uncomment only the one with php version you need (they have to be installed with "brew install php@version":

LoadModule php7_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp7.so
#LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so
#LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so
#LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so
#LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so

<FilesMatch \.php$>
      SetHandler application/x-httpd-php
</FilesMatch>

Finally, check DirectoryIndex includes index.php

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

After that:

brew services restart httpd 

And it works. No need no brew link/unlink.

I don't know if it's a best way, but for me was the only one that worked.

Comments

0

@chenrui's is perfect. I just had to create the sbin directory additionally as well.

You can find it [question]: brew link php71: Could not symlink sbin/php-fpm

Comments

0

In 2024, the simplest option, by far, is to use Laravel's Herd, even when not using Laravel.

Comments

0

If you're using Valet:

  • brew link [email protected] --overwrite, if it doesn't work, rerun it again and again, literally. or you can copy the command it suggests in the printed error.
  • After you've successfully changed your host php version, now you have to communicate that to Valet: valet use [email protected] --force
  • Finally: export PATH="/usr/local/opt/[email protected]/bin:$PATH" and export PATH="/usr/local/opt/[email protected]/sbin:$PATH"

Sometimes, when you are upgrading/downgrading, for some reason, you might need to change the host version, change Valet version, THEN you export the PATH.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.