189

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?

Xaver
  • 11,144
  • 13
  • 56
  • 91
  • 8
    Develop in a virtual machine (vagrant, docker, etc). It lets you use/switch environments without cluttering your os install – JimL Jan 20 '16 at 19:46
  • 1
    @JimL That's not a good idea, you'll have a terrible experience when using VM. –  Mar 13 '18 at 07:57
  • 1
    @xetra-su I dont use Mac OS but Id assume the experience would be pretty similar as on Linux which I have no problems with. What makes it a terrible experience? – JimL Mar 13 '18 at 08:13

19 Answers19

328

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.

Goke Obasa
  • 4,328
  • 1
  • 18
  • 26
  • 1
    I've generalized this to a shell script, see https://gist.github.com/infostreams/3753a31205e3b6c47039046e9fa40ba5 . This allows me to have many (many) versions of PHP on my machine (all of them, between 5.6 and 8.1), and change by simply typing 'php 5.6' or 'php 8.0'. Works like a treat. – Edward Nov 08 '21 at 10:21
  • 2
    If (after doing this) your CLI's `php -v` is still the previous PHP version: Make sure to restart your terminal (or launch a new one) – jakxnz Jun 05 '22 at 07:24
76

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!

Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
Xaver
  • 11,144
  • 13
  • 56
  • 91
56

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: php@7.3 is keg-only and must be linked with --force Then try with:

brew link php@7.3 --force
itsazzad
  • 6,868
  • 7
  • 69
  • 89
  • I do not think this works correctly. Once I did this it was still running on 7.4 in phpinfo() – gstlouis Oct 16 '20 at 13:24
  • 1
    Possible that you were seeing the webserver version of your php not the command line version. Update your nginx or apache to use the new version of php. – Mangopop Apr 12 '21 at 19:52
45

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
Community
  • 1
  • 1
kris
  • 11,868
  • 9
  • 88
  • 110
20

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

Yuseferi
  • 7,931
  • 11
  • 67
  • 103
15

If you have installed php with latest version on your local system and 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
Shreyasikhar26
  • 455
  • 5
  • 10
14

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

thai.hulk
  • 149
  • 1
  • 4
13

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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
9

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

after that it will work: brew unlink php@7.4 && brew link php@8.0

LavrenovPavel
  • 129
  • 1
  • 3
  • This was the key for me. I had a PATH export on my .zshrc that was rendering everything else ignored – Azurlake Aug 09 '23 at 07:37
8
brew link --overwrite php@7.3

Works perfectly for me :-)

8

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
leepowers
  • 37,828
  • 23
  • 98
  • 129
4

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

brew link php@8.0

Adesh Khanna
  • 172
  • 2
  • 9
4

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 php@7.4
    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 php@8.1
    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.

Jonathan P
  • 418
  • 5
  • 14
  • 1
    If ur using valet, just go the directory / project want run specific version just run valet isolate. Example: # valet isolate php@7.4 – Muhammad Hamizi Jaminan Oct 09 '22 at 04:55
  • I have php7.4, 8.0, 8.1, 8.2 installed on my local machine. IDK which PHP version is active. Thus I just used `brew unlink php` and `brew link --force --overwrite php@x.y` and this works just fine. I don't have valet on my machine, and composer just does fine for all the PHP versions. So no need to change that one too. – ssi-anik Aug 20 '23 at 17:57
3

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

Felix Geenen
  • 2,465
  • 1
  • 28
  • 37
3

Since the OP has specifically asked for switching from "PHP 5.3 up to PHP 7.0", a lot has been deprecated and/or removed in brew due to official support ending for them - as of Aug 2023.

All versions <= php@7.4 have been deprecated and removed from brew because they reached their end of life, see the official Unsupported Branches at php.net. For example, 7.4 lost support on 28 Nov 2022. As of Aug 2023, the officially Supported Versions listed there are 8.0 (Security fixes only), 8.1, 8.2 - which brew supports.

So if we are to switch versions and install any of these unsupported versions, brew install php@7.3 will fail (see answer for "Error: php@7.3 has been disabled because it is a versioned formula"). You'll need to use the tap shivammathur/php (praise the devs responsible for this) which can be used to install unsupported versions of PHP.

brew tap shivammathur/php //need to do only once
brew install shivammathur/php/php@7.0 //or brew install php@7.4 depending on your choice
brew search php //optional, shows all the versions available if you want some other version
brew link php@7.0

Switching henceforth is simple:

brew unlink php@7.0 
brew link php@5.6 //OP asked 5.3 but 5.6 is the lowest version supported by the tap

These too eventually will get outdated as more versions reach EOL but the steps should remain the same as long as that tap remains in development.

The article "macOS 13.0 Ventura Apache Setup: Multiple PHP Versions" (getgrav.org) provided by Xaver's answer, has more minor information beyond all this which may be useful. See its section "PHP Installation".

If switching needs to be done regularly, I endorse Yuseferi's recommendation of using Brew PHP Switcher. I've been using it these past months.

joeljpa
  • 317
  • 2
  • 13
2

Don't forget to change you environment PATH with this command :

echo 'export PATH="/usr/local/opt/php@8.1/bin:$PATH"' >> ~/.bash_profile

source ~/.bash_profile
Joffrey Outtier
  • 880
  • 10
  • 9
1

If you don't need to play with apache then there is a very easy way with one function, just add it to your ~/.bash_profile or ~/.zshrc (depends on your terminal)

function php-toggle() {
  currentVersion=$(php -v | tail -r | tail -n 1 | cut -d " " -f 2 | cut -c 1-3)
  switchTo='7.4'
  [[ $currentVersion == '7.4' ]] && switchTo='8.1'

  brew unlink php@"$currentVersion"
  brew services stop php@"$currentVersion"

  brew link php@"$switchTo"
  brew services start php@"$switchTo"

  exportString="export PATH=\"/opt/homebrew/opt/php@${switchTo}/bin:\$PATH\""
  echo $exportString > ~/.php_version

  source ~/.php_version
}

source ~/.php_version

Open new terminal and run php-toggle.

David
  • 129
  • 1
  • 5
0

My purpose was install old patch of php@7.4.24 to MacOS and I done that this way:

I found revision of brew, which contains old php patch, which i need, and reset git to them

repository: https://github.com/Homebrew/homebrew-core/commit/3b342c146da0452d71d982324977567960da2979

git -C $(brew --repo)/Library/Taps/homebrew/homebrew-core reset --hard 3b09794

brew reinstall php@7.4 
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 26 '22 at 05:56
0
  • First install or reinstall the version you want

    brew reinstall php@7.4

  • Secondly you need to switch to the version you want

    echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc

  • Then you need to add in your PATH by running below command

    brew-php-switcher 7.4