How do i install different versions of php on mac?

    Great post about having multiple PHP version in your console: https://getgrav.org/blog/macos-monterey-apache-multiple-php-versions.

    Few snippets from the post:

    brew tap shivammathur/php
    brew install shivammathur/php/
    brew install shivammathur/php/
    brew install shivammathur/php/
    brew install shivammathur/php/
    brew install shivammathur/php/
    brew install shivammathur/php/
    brew unlink php && brew link --overwrite --force 


    How do i install different versions of php on mac?
    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.

    • Install one PHP version
    • Install multiple PHP versions
    • Switch PHP versions
    • Conclusion

    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:

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

    $ brew install shivammathur/php/

    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/

    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 
    0 drwxr-xr-x 3 petehouston admin 96 May 6 22:11 
    

    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 

    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//bin:$PATH"
    
    $ export PATH="/opt/homebrew/opt//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//bin:/opt/homebrew/opt//sbin:$PATH\""
    alias set-php7.3="export PATH=\"/opt/homebrew/opt//bin:/opt/homebrew/opt//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!

    How do I install different versions of PHP?

    How to Install multiple versions of PHP?.
    Step 1 : Add ppa:ondrej/php to Ubuntu system. ... .
    Step 2 : Update the system. ... .
    Step 3 : Check php versions available. ... .
    Step 3 : Install different version of PHP..

    Can I have multiple versions of PHP installed?

    Add a PHP Repository So you'll need to add the PHP repository in your system to install the multiple PHP versions. Once the repository is up-to-date, you can proceed to install multiple PHP versions.

    How do I switch between PHP versions?

    Upgrading or Downgrading PHP Versions.
    Ensure the php packages for the version you want are installed..
    Update any configuration for that PHP version..
    Update your web server configuration to point to the correct PHP version..
    Point PHP CLI to the correct PHP version..
    Restart your web server and php-fpm..

    How do I upgrade PHP to 8.0 on Mac?

    Upgrading with Homebrew.
    Normal upgrade. brew upgrade php..
    Upgrade with shivammathur/homebrew-php. brew tap shivammathur/php brew install shivammathur/php/[email protected]. To switch between versions, use the following command: brew link --overwrite --force [email protected]. ... .
    Next steps. Check the current version by running php -v : php -v..