How to install PHP 8.3 on MacOS

How to install PHP 8.3 on MacOS banner

PHP 8.3 will be released on November 23, 2023.

Until then, you can play around and test the beta versions which are already out. In this post, I'll show you how to install the beta version on your Mac.

Install PHP 8.3 with brew

You can use your well-known Mac package manager brew, but you'll need to add a third-party repository (called tap) with the newest PHP versions.

Install Homebrew

If you don't have brew, you can install it with this command:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Add the third-party repository

The repository we will use is https://github.com/shivammathur/homebrew-php which has all PHP versions starting from PHP 5.6. To add the repository, execute the following command:

$ brew tap shivammathur/php

Install PHP 8.3

To install PHP 8.3, run the following command

$ brew install shivammathur/php/[email protected]

You need to restart your terminal or reload your configuration

To validate that your installation is successful, run the following:

$ php --version

You should see

PHP 8.3.0-dev (cli) (built: Sep  9 2023 00:11:17) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.0-dev, Copyright (c) Zend Technologies

If you don't see the correct PHP version, that means you probably have other PHP versions already installed and linked in your $PATH. To change your linked PHP version, execute:

$ brew link --overwrite --force shivammathur/php/[email protected]

Now php --version should show 8.3.

If, at some point, you want to revert back to an older version of PHP, you can unlink and link another package like so:

$ brew unlink shivammathur/php/[email protected]
$ brew link --overwrite --force shivammathur/php/[email protected]

How can I have different PHP versions available in my $PATH?

Instead of linking and unlinking each time I want to use a different version of PHP, you can do the following:

First, check what PHP versions you have installed

$ ll /opt/homebrew/Cellar/ | grep php
drwxr-xr-x    3 nacholibre  admin    96 Sep  9 08:00 php
drwxr-xr-x    3 nacholibre  admin    96 Jul 10 23:53 [email protected]
drwxr-xr-x    3 nacholibre  admin    96 Jul 10 23:55 [email protected]
drwxr-xr-x    3 nacholibre  admin    96 Jul  7 15:37 [email protected]
drwxr-xr-x    3 nacholibre  admin    96 Sep  9 08:09 [email protected]
drwxr-xr-x    3 nacholibre  admin    96 Sep  9 07:58 [email protected]

As you can see, I have a total of 6 versions installed. If, for example, I want to use PHP 8.1, I can just do the following:

$ /opt/homebrew/Cellar/[email protected]/8.1.23/bin/php --version

Or you can create a symlink in your path like so

$ sudo ln -s /opt/homebrew/Cellar/[email protected]/8.1.23/bin/php /usr/local/bin/php81

And now you can just run

$ php81 --version
PHP 8.1.23 (cli) (built: Aug 31 2023 18:43:16) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.23, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.23, Copyright (c), by Zend Technologies

Common errors

During the installation on my local machine, I got the following error

dyld[20950]: Library not loaded: /opt/homebrew/opt/libavif/lib/libavif.15.dylib
Referenced from: <87C1A268-34E4-396F-8BBC-D5591064E333> /opt/homebrew/Cellar/gd/2.3.3_5/lib/libgd.3.dylibu
Reason: tried: '/opt/homebrew/opt/libavif/lib/libavif.15.dylib' (no such file),
'/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/libavif/lib/libavif.15.dylib' (no such file),
'/opt/homebrew/opt/libavif/lib/libavif.15.dylib' (no such file),
'/usr/local/lib/libavif.15.dylib' (no such file),
'/usr/lib/libavif.15.dylib' (no such file, not in dyld cache),
'/opt/homebrew/Cellar/libavif/1.0.1/lib/libavif.15.dylib' (no such file),
'/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Cellar/libavif/1.0.1/lib/libavif.15.dylib' (no such file),
'/opt/homebrew/Cellar/libavif/1.0.1/lib/libavif.15.dylib' (no such file),
'/usr/local/lib/libavif.15.dylib' (no such file), '/usr/lib/libavif.15.dylib' (no such file, not in dyld cache)

Warning: The post-install step did not complete successfully

How to fix library not loaded: /opt/homebrew/opt/libavif/lib/libavif.15.dylib error?

To fix this issue, you need to install or reinstall GD

$ brew install gd
# or if you have already gd installed, run
$ brew reinstall gd

similar error is library not loaded: /usr/local/opt/libavif/lib/libavif.15.dylib which can be fixed by installing or reinstalling gd as well.