Install multiple PHP versions on MacOS Monterey 12

0
3628
Install multiple PHP versions on MacOS Monterey 12
Install multiple PHP versions on MacOS Monterey 12

Since the latest MacOS Monterey 12 has removed the operating system, we need to manage PHP manually. I will show you a tip to install multiple PHP versions on MacOS Monterey 12.

We will use Homebrew as package manager to do this. If you don’t have Homebrew installed, you might to set it up on your development machine.

Let’s start with installing the first PHP version.

Article Contents

Install one PHP version

The steps to install PHP is very simple, first, tap the PHP formulae and then select the appropriate PHP version to install.

$ brew tap shivammathur/php

$ brew install shivammathur/php/PHP_VERSION

Please replace PHP_VERSION with your version, the available options are:

  • php@5.6
  • php@7.0
  • php@7.1
  • php@7.2
  • php@7.3
  • php@7.4
  • php@8.0
  • php@8.1

Let say you want to install PHP version 7.3, you will need to issue this command:

$ brew install shivammathur/php/php@7.3

Once done, restart the terminal and confirm php version:

$ php -v
PHP 7.3.33 (cli) (built: Apr  5 2022 22:29:31) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.33, Copyright (c) 1999-2018, by Zend Technologies

Install multiple PHP versions

You can install another PHP versions by issuing the same command above with different PHP version options:

$ brew install shivammathur/php/php@8.1

Each PHP version is set in a separate directory under /opt/homebrew/Cellar:

$ ls -asl /opt/Cellar | grep php

0 drwxr-xr-x 3 petehouston admin 96 May 6 22:11 php
0 drwxr-xr-x 3 petehouston admin 96 May 6 22:11 php@7.3
0 drwxr-xr-x 3 petehouston admin 96 May 6 22:11 php@7.4

The default /opt/homebrew/Cellar/php is linked to whatever latest Homebrew link is set to.

You can switch the link to different PHP version by using this command:

$ brew unlink php && brew link --overwrite --force php@8.1

Switch PHP versions

Instead of using Homebrew link to set default PHP version, you can simply export the PHP bin directory to PATH environment variable to use the desired PHP version.

You can do this on the shell:

$ php -v
PHP 7.3.33 (cli) (built: Apr  5 2022 22:29:31) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.33, Copyright (c) 1999-2018, by Zend Technologies

$ export PATH="/opt/homebrew/opt/php@8.1/bin:$PATH"

$ export PATH="/opt/homebrew/opt/php@8.1/sbin:$PATH"

$ php -v
PHP 8.1.5 (cli) (built: Apr 16 2022 00:03:52) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.5, Copyright (c), by Zend Technologies

As you can see from the above commands, the default PHP command has been switched from PHP 7.3 to 8.1 easily.

You can write an alias or a function to handle the export:


# ~/.zshrc
alias set-php8.1="export PATH=\"/opt/homebrew/opt/php@8.1/bin:/opt/homebrew/opt/php@8.1/sbin:$PATH\""
alias set-php7.3="export PATH=\"/opt/homebrew/opt/php@7.3/bin:/opt/homebrew/opt/php@7.3/sbin:$PATH\""

switch-php() {
    local version=$1
    export PATH="/opt/homebrew/opt/php@$version/bin:$PATH"
    export PATH="/opt/homebrew/opt/php@$version/bin:$PATH"
    alias php=/opt/homebrew/opt/php@$version/bin/php
}

Then you can switch on the shell like this:

$ set-php8.1

$ set-php7.3

$ switch-php 7.3

$ switch-php 8.1

Conclusion

That’s how you install multiple PHP versions on MacOS Monterey 12 and manage them on the shell easily.

Have fun!