21

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 php@7.2

brew services install php@7.3

brew link php@7.3

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?

Sidney Sousa
  • 3,378
  • 11
  • 48
  • 99
  • 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: http://hexlator.blogspot.com/2018/04/macos-1013-high-sierra-apache-setup_14.html – Gino Pane Dec 02 '19 at 16:20

5 Answers5

65

Here is my installation script:

  • brew install php@7.2
  • brew link --force php@7.2
  • brew services start php@7.2
  • export PATH="/usr/local/opt/php@7.2/bin:$PATH"
  • export PATH="/usr/local/opt/php@7.2/sbin:$PATH"

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.

chenrui
  • 8,910
  • 3
  • 33
  • 43
8

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 php@7.4 && brew link php@7.2 --force

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

    zshrc file

  3. Save It.

  4. brew services stop php@7.4

  5. brew services start php@7.2

enter image description here

Voila. To go back to 7.4 Run brew unlink php@7.2 && brew link php@7.4 --force & uncomment export files. That's it.

Zaid Haider
  • 530
  • 6
  • 11
5

Until I restarted Terminal I kept seeing the old version.

chillywilly
  • 405
  • 3
  • 11
4

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/php@7.4/bin:$PATH"
#export PATH="/usr/local/opt/php@7.4/sbin:$PATH"
export PATH="/usr/local/opt/php@8.2/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

Hamed Yarandi
  • 1,085
  • 1
  • 12
  • 21
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

pannu
  • 1