12

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?

user agent
  • 3,018
  • 10
  • 38
  • 89
  • 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

4 Answers4

40

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
  • 6,821
  • 3
  • 27
  • 39
2

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
  • 350
  • 3
  • 11
1

Until I restarted Terminal I kept seeing the old version.

chillywilly
  • 303
  • 1
  • 8
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