PHP 8 on macOS – installation guide
Tags: php, macos
Published: 2020-11-29 14:43:56

Big day, the newest version of PHP has been released. It means we should update our local, stage, and production environments. I will present how we can install PHP 8 easily on Mac OS and switch between different versions. Let’s start!

I would have forgotten. Below is a summary that shows how long the previous versions will be supported by PHP’s contributors. In other words, how many days do you have to upgrade your applications?

## Brew
I assume that you know what brew is and how to install it. If you are not familiar with this tool, you can check my previous article: “[Brew on macOS Explained In 1 Post](https://pilsniak.com/how-to-install-brew-on-macos)“.

## Current status of PHP
As you can see, my current version of PHP is 7.4.

```
% php --version
PHP 7.4.10 (cli) (built: Sep  3 2020 18:21:42) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.10, Copyright (c), by Zend Technologies
```
The command that is the most interesting for us is here:

```
% brew upgrade php
```

After execution of that command, you should see similar output:
```
==> Upgrading 1 outdated package:
php 7.4.10 -> 8.0.0
==> Upgrading php 7.4.10 -> 8.0.0 

==> Installing dependencies for php: openssl@1.1, c-ares, nghttp2, openldap, curl-openssl, unixodbc, freetds, freetype, libtiff, python@3.9, glib, gmp, krb5, libpq and oniguruma

==> Installing php
==> Pouring php-8.0.0.catalina.bottle.tar.gz
==> /usr/local/Cellar/php/8.0.0/bin/pear config-set php_ini /usr/local/etc/php/8.0/php.ini system
==> /usr/local/Cellar/php/8.0.0/bin/pear config-set php_dir /usr/local/share/pear system
==> /usr/local/Cellar/php/8.0.0/bin/pear config-set doc_dir /usr/local/share/pear/doc system
==> /usr/local/Cellar/php/8.0.0/bin/pear config-set ext_dir /usr/local/lib/php/pecl/20200930 system
==> /usr/local/Cellar/php/8.0.0/bin/pear config-set bin_dir /usr/local/opt/php/bin system
==> /usr/local/Cellar/php/8.0.0/bin/pear config-set data_dir /usr/local/share/pear/data system
==> /usr/local/Cellar/php/8.0.0/bin/pear config-set cfg_dir /usr/local/share/pear/cfg system
==> /usr/local/Cellar/php/8.0.0/bin/pear config-set www_dir /usr/local/share/pear/htdocs system
==> /usr/local/Cellar/php/8.0.0/bin/pear config-set man_dir /usr/local/share/man system
==> /usr/local/Cellar/php/8.0.0/bin/pear config-set test_dir /usr/local/share/pear/test system
==> /usr/local/Cellar/php/8.0.0/bin/pear config-set php_bin /usr/local/opt/php/bin/php system
==> /usr/local/Cellar/php/8.0.0/bin/pear update-channels
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /usr/local/etc/php/8.0/

To have launchd start php now and restart at login:
  brew services start php
Or, if you don't want/need a background service you can just run:
  php-fpm
==> Summary
🍺  /usr/local/Cellar/php/8.0.0: 499 files, 77.8MB
```
Again, when I execute the first command I will see this time PHP 8.0, like here:
```
% php --version
PHP 8.0.0 (cli) (built: Nov 26 2020 17:53:04) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.0, Copyright (c), by Zend Technologies
```

## Bonus: switching PHP versions
You can use the following command to switch between version:
```
% brew unlink php && brew link --overwrite php@7.2
% brew unlink php && brew link --overwrite php@7.3
% brew unlink php && brew link --overwrite php@7.4
```
## Bonus: Supported versions of PHP

* PHP 7.2  – active support unit 30 November 2019 (don’t use it!)
* PHP 7.3 – active support until 6 December 2020 (you should migrate asap)
* PHP 7.4 – active support until 28 November 2021 (good enough for a production)
* PHP 8.0 – active support until 26 November 2022 (perfect for new products)