Xaver
Xaver

Reputation: 11652

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

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

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

Upvotes: 222

Views: 449623

Answers (20)

risingPhoenix1979
risingPhoenix1979

Reputation: 1144

Below is my phpv shell function which is working great!

How to set up:

  • As I'm using Oh My Zsh I added the code to ~/.oh-my-zsh/custom/aliases.zsh file (which I created).

  • I also tested this code in my .zshrc file located at .zshrc (in your Mac terminal do cd and press Enter to find your .zshrc file) as I'm using the Zsh shell on my Mac (macOS Sonoma 14.7.1).

  • I've also tested on my Windows 11 laptop using WSL 2 on the Windows Terminal app (with Oh My Zsh installed)

  • I installed Homebrew and the brew-php-switcher Homebrew Formulae (plugin) by running brew install brew-php-switcher

  • I added if command -v apachectl > /dev/null 2>&1; then conditional to stop Apache with sudo apachectl stop inside of this block ONLY if the apachectl command exists (although this can removed if needed). The apachectl command is often used to stop and stop the Apache server on your command line.

    I've been using the Local free desktop app (owned by WP Engine) and the Apache server was running locally which was taking up Port 80 and causing Local to not work. This command stops the Apache server so Local can work. And you may want to NOT have Apache use Port 80 when doing local development when NOT using Local.

    If needed below are some aliases to stop and start the Apache server (which also live in my .zshrc file):

    ## Stop Apache
    alias apachestop="sudo apachectl stop"
    ## Start Apache
    alias apachestart="sudo apachectl start"
    
  • In my terminal I run source ~/.zshrc to refresh my terminal and make the phpv function available as I'm using the Zsh shell

Example usage:

  • Switch to PHP ver. 7.2:
    phpv 7.2 (and you will need to enter your Mac login or sudo password)
  • Switch to PHP ver. 8.2:
    phpv 8.2
  • Double-check which version of PHP you are using in your terminal:
    php -v

phpv shell function

###### PHP ######
## Change PHP version function, example:
## `phpv 7.2` to switch to PHP version 7.2
function phpv() { 
  if ! command -v brew > /dev/null 2>&1 || ! brew list brew-php-switcher > /dev/null 2>&1 || [[ -z "$1" ]]; then
    echo "Unfortunately, your PHP version change attempt failed.\n\n- An example of this command is 'phpv 7.2' to change to PHP ver. 7.2.\n- Also you must have both the Homebrew and 'brew-php-switcher' Homebrew packages installed."
    # Exit script with an error
    return 1
  fi

  local version="$1"
  # Install the specified PHP version
  brew install --quiet php@"$version"
  # Switch to the specified PHP version
  brew-php-switcher "$version"

  if command -v apachectl > /dev/null 2>&1; then
    # Stop apache from running, as this
    # causes port 80 conflicts with Localwp.com app
    # and possible other localhost software 
    sudo apachectl stop
  fi
  # Display the active PHP version
  php -v
}

Upvotes: 1

Goke Obasa
Goke Obasa

Reputation: 4898

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 [email protected]
brew link [email protected]

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

Upvotes: 382

joeljpa
joeljpa

Reputation: 539

While the OP has specifically asked for switching from PHP 5.3 up to PHP 7.0, a lot has been removed in Homebrew due to their official support ending.

So, as of Jan 2024: All versions <= PHP 8 have reached their end of life. See the official lists of Supported Versions and Unsupported Versions. For example, PHP 8.0 lost support on 26 Nov 2023.

So if we are to switch versions and install any of these unsupported versions, brew install [email protected] will fail (see answer for "Error: [email protected] 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 or even newer versions not officially available in brew.

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

Switching henceforth is simple:

brew unlink [email protected] 
brew link [email protected] //OP asked 5.3 but 5.6 is the lowest version supported by the tap

Notes:

  • As more versions reach EOL and newer versions get released, these steps should remain the same as long as that tap remains in development.
  • The guide "macOS 13.0 Ventura Apache Setup: Multiple PHP Versions" provided by Xaver's answer is a good further reading. See the section "PHP Installation". It seems to be reasonably up-to-date about each PHP release and explains more about using the tap by shivammahtur.
  • If switching needs to be done regularly, see Yuseferi's answer recommending Brew PHP Switcher. I've been using it these past months.

Upvotes: 7

vintagexav
vintagexav

Reputation: 2057

Make switching PHP versions easy adding this in your .zshrc.

Try with 5.6, 7.0, 7.1, 7.2, 7.3, 7.4 and 8.0

function phpv() { # $ phpv 7.4 to invoque
  brew unlink php
  brew link --overwrite --force "php@$1"
  php -v
}

Bash script that will do all the heavy work:

$ phpv 5.6

If needed, do:

$ brew unlink php
$ brew install shivammathur/php/[email protected]

Upvotes: 4

David
David

Reputation: 139

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.

Upvotes: 1

Joffrey Outtier
Joffrey Outtier

Reputation: 920

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

echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.bash_profile

source ~/.bash_profile

Upvotes: 2

Emmanuel Iyen
Emmanuel Iyen

Reputation: 439

  • First install or reinstall the version you want

    brew reinstall [email protected]

  • Secondly you need to switch to the version you want

    echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc echo 'export PATH="/usr/local/opt/[email protected]/sbin:$PATH"' >> ~/.zshrc

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

    brew-php-switcher 7.4

Upvotes: 0

Shreyasikhar26
Shreyasikhar26

Reputation: 853

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 [email protected]
brew link [email protected]

Upvotes: 24

CekacBTym6oke
CekacBTym6oke

Reputation: 53

My purpose was install old patch of [email protected] 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 [email protected] 

Upvotes: 0

LavrenovPavel
LavrenovPavel

Reputation: 139

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

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

Upvotes: 10

Jonathan P
Jonathan P

Reputation: 458

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 [email protected]
    brew services stop [email protected]
    brew link --force --overwrite [email protected]
    brew services start [email protected]
    composer global update
    rm -f ~/.config/valet/valet.sock
    valet install
    valet start
}


switchphp8() {
    valet stop
    brew unlink [email protected]
    brew services stop [email protected]
    brew link --force --overwrite [email protected]
    brew services start [email protected]
    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.

Upvotes: 5

Felix Geenen
Felix Geenen

Reputation: 2707

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

Upvotes: 3

leepowers
leepowers

Reputation: 38298

How to:

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

% brew unlink php
% brew link [email protected]

Upvotes: 8

Yuseferi
Yuseferi

Reputation: 8670

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

Upvotes: 28

Adesh Khanna
Adesh Khanna

Reputation: 242

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]

Upvotes: 4

brew link --overwrite [email protected]

Works perfectly for me :-)

Upvotes: 9

itsazzad
itsazzad

Reputation: 7277

Example: Let us switch from php 7.4 to 7.3

brew unlink [email protected]
brew install [email protected]
brew link [email protected]

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

brew link [email protected] --force

Upvotes: 71

thai.hulk
thai.hulk

Reputation: 179

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

Upvotes: 17

kris
kris

Reputation: 12572

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

Upvotes: 50

Mark Setchell
Mark Setchell

Reputation: 207335

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

Upvotes: 12

Related Questions