Upgrade to the new PHP 8 on macOS

Cloudeya - PHP 8 Release

There’s excitement in the developer community with the release of PHP 8 on November 26, 2020. The major release brings with it new features and performance improvement for applications developed with PHP technology. Likewise, developers and business owners can expect deprecation warnings and errors, so they have to make some changes in the application’s codebase to get it running on PHP 8.

Upgrade with Homebrew

Open your terminal and make sure brew is up-to-date:

brew update

Now, you can upgrade the current PHP version to the latest release. Upgrading support using the built-in PHP recipe, or you can use this tap shivammathur/homebrew-php. We recommend using the tap since you can install multiple PHP versions allowing you to switch when needed.

Upgrading normally

brew upgrade php

Upgrading with shivammathur/homebrew-php

brew tap shivammathur/php && brew install shivammathur/php/php@8.0

You can switch between versions using the following command:

brew link --overwrite --force php@8.0

You can learn more about the tap here.

Verify the current version:

php -v

Restart Apache or Nginx:

sudo nginx -s reload
sudo apachectl restart

You can verify your local web server uses PHP 8 by adding the code below in info.php:

# info.php, accessible to your web server

phpinfo();

Note: Developers with Laravel Valet should follow the steps below to ensure the web server works properly.

Valet

Developers with Laravel Valet should follow the steps below to ensure the web server works properly.

composer global update

Now, you can install valet by running:

valet install

Extensions

You can install PHP extensions with pecl. We prefer using Imagick, Redis and Xdebug:

pecl install imagick
pecl install redis
pecl install xdebug

You can run pecl list to see installed extensions:

pecl list

# Installed packages, channel pecl.php.net:
# =========================================
# Package Version State
# imagick 3.4.4   stable
# redis   5.1.1   stable
# xdebug  2.8.0   stable

You can search for other extensions using pecl search:

pecl search pdf

# Retrieving data...0%
# ..
# Matched packages, channel pecl.php.net:
# =======================================
# Package Stable/(Latest) Local
# pdflib  4.1.2 (stable)        Creating PDF on the fly with the PDFlib library

Don’t forget to restart web server after installing new packages:

sudo nginx -s reload
sudo apachectl restart

For Laravel Valet, you can restart by using the following command:

valet restart

Ensure you installed the extensions correctly:

php -i | grep redis
var_dump(extension_loaded('redis'));

There are two fixes for extensions that didn’t load properly.

First, make sure the extensions are added in the correct ini file. You can run php –ini to know which file is loaded:

Configuration File (php.ini) Path: /usr/local/etc/php/7.4
Loaded Configuration File:         /usr/local/etc/php/7.4/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.4/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.4/conf.d/ext-opcache.ini,
/usr/local/etc/php/7.4/conf.d/php-memory-limits.ini

Check the ini file:

extension="redis.so"
extension="imagick.so"
zend_extension="xdebug.so"

Reminder: you don’t have to restart Nginx, Apache or Valet if you testing installed extension via the CLI especially when you make changes to the ini settings.

The second thing you can do is to reinstall extensions individually if you are updating from an older PHP version.

pecl uninstall imagick
pecl install imagick

Last Note

Don’t forget to check your project(s) is compatible with PHP 8. Need help upgrading your application to PHP 8? Don’t hesitate to shoot us a message to see how we can help.