How to Easily Switch Between PHP Versions on MAC Osx

How can I easily switch between PHP versions on Mac OSX?

If you have both versions of PHP installed, you can switch between versions using the link and unlink brew commands.

For example, to switch between PHP 7.4 and PHP 7.3

brew unlink php@7.4
brew link php@7.3

PS: both versions of PHP have be installed for these commands to work.

Switch of php versions not working on mac

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.

Change mac os x default php version

$ brew upgrade php : ( get the latest homebrew php packages )

$ brew install php@7.4 : ( Install php 7.4 )

$ brew link php@7.4 : ( create an alias to this keg-only version; see comments output during installation )

$ echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.bash_profile : ( Add the alias to your path; see comments output during installation )

$ source ~/.bash_profile : ( Reload .bash_profile to use the new settings immediately )

Reference : How to use the php that brew installed?

switch php version from php56 to php71

I solved my problem by set PATH in terminal config

-In fish, I set PATH in ~/.config/fish/config.fish this way :

set -gx PATH /usr/local/opt/php71/bin $PATH

-In base

export PATH="$(brew --prefix homebrew/php/php71)/bin:$PATH"

Mac / OSx - Change PHP version which is call in bash

I would recommend you to install php through "Mac Ports".

You have multiple versions of php installed.

With the cli command "which" you can:

DESCRIPTION
The which utility takes a list of command names and searches the path for each executable file
that would be run had these commands actually been invoked.

So read the man pages of the cli command "which".

DO this from your cli:

which php

and then you will see where is located your php executable. What i would recommend you is to replace your php executable for a symbolic link to your /usr/bin/php.

How use different versions of PHP in command line

You can use phpbrew, it's very simple and convenient when you want to switch between PHP versions as well as for PHP extension management.

For changing the PHP CLI version you just run one command:

$ phpbrew list
* php-7.1.12
php-5.5.15

$ php -v
PHP 7.1.12 (cli) (built: Jan 14 2018 22:25:40) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.1.12, Copyright (c) 1999-2017, by Zend Technologies

$ phpbrew use php-5.5.15
$ php -v
PHP 5.5.15 (cli) (built: Nov 9 2018 00:22:21)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies

Updating php version on mac

Use Homebrew:

I would probably recommend installing homebrew to manage such installations for you. With that installed you can just run the following command to install php7.1

brew update
brew install php@71


Related Topics



Leave a reply



Submit