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

PhpMacos

Php Problem Overview


I would like to test my application on PHP 5.3 up to PHP 7.0.

Where and how can I install the versions and how can I switch them by running a small script?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

I found this very good tutorial on how to install and switch php versions on OSX.

I can switch the version like

$ sphp 7.0 => PHP 7.0
$ sphp 7.3 => PHP 7.3
$ sphp 7.4 => PHP 7.4

Exactly what I want!

Solution 3 - Php

Example: Let us switch from php 7.4 to 7.3

brew unlink php@7.4
brew install php@7.3
brew link php@7.3

If you get Warning: [email protected] is keg-only and must be linked with --force Then try with:

brew link php@7.3 --force

Solution 4 - Php

Using brew

Show current version

$ php -v

Change to different version
(eg. changing from 5.5.x to version 7.0.latest) :

$ brew unlink php55
$ brew install php70

Solution 5 - Php

If you install PHP with homebrew, you can switch between versions very easily. Say you want php56 to point to Version 5.6.17, you just do:

brew switch php56 5.6.17

Solution 6 - Php

i think unlink & link php versions are not enough because we are often using php with apache(httpd), so need to update httpd.conf after switch php version.

i have write shell script for disable/enable php_module automatically inside httpd.conf, look at line 46 to line 54 https://github.com/dangquangthai/switch-php-version-on-mac-sierra/blob/master/switch-php#L46

Follow my steps:

  1. Check installed php versions by brew, for sure everything good

    > brew list | grep php #output php56 php56-intl php56-mcrypt php71 php71-intl php71-mcrypt

  2. Run script

    > switch-php 71 # or switch-php 56 #output PHP version [71] found Switching from [php56] to [php71] ... Unlink php56 ... [OK] and Link php71 ... [OK] Updating Apache2.4 Configuration /usr/local/etc/httpd/httpd.conf ... [OK] Restarting Apache2.4 ... [OK] PHP 7.1.11 (cli) (built: Nov 3 2017 08:48:02) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

  3. Finally, when your got above message, check httpd.conf, in my laptop:

    vi /usr/local/etc/httpd/httpd.conf

You can see near by LoadModule lines

LoadModule php7_module /usr/local/Cellar/php71/7.1.11_22/libexec/apache2/libphp7.so
#LoadModule php5_module /usr/local/Cellar/php56/5.6.32_8/libexec/apache2/libphp5.so

4) open httpd://localhost/info.php

i hope it helpful

Solution 7 - Php

I liked the switcher idea because I'm working of different version at the moment so what you need is

   brew install brew-php-switcher

then brew-php-switcher version

for example brew-php-switcher 7.4

Solution 8 - Php

How to:

  • Find installed PHP versions
  • Switch from default PHP package to a versioned package
% brew search php
brew-php-switcher   php-cs-fixer        php@7.3             phplint             phpstan             pup
php ✔               php-cs-fixer@2      php@7.4 ✔           phpmd               phpunit
php-code-sniffer    php@7.2             phpbrew             phpmyadmin          pcp

% brew unlink php
% brew link php@7.4

Solution 9 - Php

brew link --overwrite php@7.3

Works perfectly for me :-)

Solution 10 - Php

if you are using homebrew, then it allows multiple versions of a formula to be installed. hence,

  1. brew unlink is used to detach a version of formula from usage.
  2. brew link is used to attach a version of formula to usage.

for example, if you have php 7.4 and 8.0 both installed and want to switch between them assuming you are currently using 7.4, then it can be done as :

brew unlink [email protected]

brew link [email protected]

Solution 11 - Php

I prefer to use phpbrew where you can easily install ($ phpbrew install 8.1.3 +default) and switch (phpbrew switch 8.1.3) the used php version.

https://github.com/phpbrew/phpbrew

Solution 12 - Php

If you have installed php with latest version on your local system and using using homebrew then to switch to older version, run following commands, Here, we are switching to 7.4

brew unlink php
brew install php@7.4
brew link php@7.4

Solution 13 - Php

Old question but it's still a difficulty to have a quick way to switch between php versions in 2022. I use Laravel valet and switching PHP versions requires a re install of valet to work. I basically have two custom functions defined in my .zhsrc file, but I think this will work for bash too:


switchphp7() {
    valet stop
    brew unlink php@8.1
    brew services stop php@8.1
    brew link --force --overwrite [email protected]
    brew services start php@7.4
    composer global update
	rm -f ~/.config/valet/valet.sock
    valet install
    valet start
}


switchphp8() {
    valet stop
    brew unlink php@7.4
    brew services stop php@7.4
    brew link --force --overwrite [email protected]
    brew services start php@8.1
    composer global update
	rm -f ~/.config/valet/valet.sock
    valet install
    valet start
}

And then just run them in the command line:

switchphp8

This works reliably for me, however, if you use Valets virtual SSL certificates, the command line will ask for admin account password for every ssl certificate you have linked which is potentially annoying if you have loads of them. It's not a bug if it keeps asking, eventually you will allow all if you keep typing password/use touch id.

Solution 14 - Php

delete all row with "php" in: nano ~/.zshrc

after that it will work: brew unlink [email protected] && brew link [email protected]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionXaverView Question on Stackoverflow
Solution 1 - PhpGoke ObasaView Answer on Stackoverflow
Solution 2 - PhpXaverView Answer on Stackoverflow
Solution 3 - PhpitsazzadView Answer on Stackoverflow
Solution 4 - PhpkrisView Answer on Stackoverflow
Solution 5 - PhpMark SetchellView Answer on Stackoverflow
Solution 6 - Phpthai.hulkView Answer on Stackoverflow
Solution 7 - PhpYuseferiView Answer on Stackoverflow
Solution 8 - PhpleepowersView Answer on Stackoverflow
Solution 9 - PhpSboniso Marcus NzimandeView Answer on Stackoverflow
Solution 10 - PhpAdesh KhannaView Answer on Stackoverflow
Solution 11 - PhpFelix GeenenView Answer on Stackoverflow
Solution 12 - PhpShreyasikhar26View Answer on Stackoverflow
Solution 13 - PhpJonathan PortView Answer on Stackoverflow
Solution 14 - PhpLavrenovPavelView Answer on Stackoverflow