Refers to the previous post on how to install/upgrade PHP:
https://calvin.my/posts/install-upgrade-php-in-macos
Use default version
-
To check the default PHP version on your machine:
% which php /opt/homebrew/opt/php@8.3/bin/php -
With above configuration, whenever you enter "php xxxxxxx" in your terminal, it executes php@8.3
-
If your application is built for a different PHP version (e.g. 8.1), running it with the default PHP might cause unexpected behaviors.
Change default version
-
Edit ~/.zshrc (on a mac)
-
Comment / uncomment the version you need
# export PATH="/opt/homebrew/opt/php@8.3/bin:$PATH" # export PATH="/opt/homebrew/opt/php@8.3/sbin:$PATH" export PATH="/opt/homebrew/opt/php@8.4/bin:$PATH" export PATH="/opt/homebrew/opt/php@8.4/sbin:$PATH" -
Save and reload
source ~/.zshrc
Change default version (Temporarily)
-
Directly set the PATH for a terminal session.
export PATH="/opt/homebrew/opt/php@8.3/bin:$PATH" -
This uses PHP 8.3 until you close the terminal session.
Directly use non-default versions
-
To check all the PHP versions you have on your machine:
% ls /opt/homebrew/opt | grep php php php@7.4 php@8.0 php@8.1 php@8.2 php@8.3 -
You can pick the correct version you need and run it with:
% /opt/homebrew/opt/php@8.1/bin/php <command> % /opt/homebrew/opt/php@8.1/bin/php -v PHP 8.1.29 (cli) (built: Jun 5 2024 05:51:57) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.29, Copyright (c) Zend Technologies with Zend OPcache v8.1.29, Copyright (c), by Zend Technologies
PHP Composer
-
Composer is the tool in PHP to manage dependencies. For example, it is used in a Laravel application.
-
Composer is tied to PHP versions. That means setup composer with PHP 8.1 and later executing it using PHP 8.3, might not work properly.
-
In the composer installation guide, the steps create a global composer executable. When you complete the installation steps following the guide, you get a default composer compatible with your PHP.

-
You should do this once every time you install a newer PHP version on your machine.
Using non-default composer version
Now that you have replaced the default composer, what happens to the older projects?
-
Go to your project directory.
-
Follow the same composer installation guide, but replace all the "php" command with the exact version you need. e.g.
% php composer-setup.php % /opt/homebrew/opt/php@8.1/bin/php composer-setup.php -
This gives you a "composer.phar" executable in the project directory.

-
Do not run the final step to copy this as default.

-
Now you can use the correct version of composer. e.g.
// This runs composer (for 8.1) using php 8.1 % /opt/homebrew/opt/php@8.1/bin/php composer.phar install // This runs composer (for 8.3) using php 8.3 % composer installNote: Add composer.phar into your gitignore so it won't go into your repository.