Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start your free 14-day trial today.

Upgrade to PHP 8 with Homebrew on Mac

Note: PHP 8 hasn't been released yet, so it'll be a few more hours before you can use Homebrew to update to it.

# Upgrading with Homebrew

Start by making sure brew is up-to-date:

brew update

Next, upgrade PHP. You can either use the built-in php recipe, use tap shivammathur/homebrew-php. I'd recommend the second approach, since it allows you to easily install several PHP versions and switch between them.

# Normal upgrade

brew upgrade php

# Upgrade with shivammathur/homebrew-php

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

To switch between versions, use the following command:

brew link --overwrite --force php@8.0

You can read more in the repository.

# Next steps

Check the current version by running php -v:

php -v

Restart Nginx or Apache:

sudo nginx -s reload
sudo apachectl restart

And make sure that your local web server also uses PHP 8 by visiting this script:

# index.php, accessible to your web server


The version should show 8.0.x.

Note: if you're using Laravel Valet, please keep on reading, you need some extra steps in order for the web server to properly work.

Interested in more?

I'm writing a new series on how I manage and grow my blog called "Blogs for Devs". You might like the behind the scenes stories and also be inspired to start your own blog.

Check it out!

# Valet

If you're using Laravel Valet, you should do the following steps to upgrade it:

composer global update

You can use valet use to switch between PHP versions:

valet use php@8.0
valet use php@7.4

Note that if you're switching from PHP 8 to PHP 7.4, there's a bug in Valet that doesn't properly update the changes. You can solve it by manually removing the valet socket after having run valet use php@7.4:

cd ~/.config/valet
rm valet.sock
valet restart

# Extensions

PHP extensions are installed using pecl. I personally use Imagick, Redis and Xdebug. They can be installed like so:

pecl install imagick
pecl install redis
pecl install xdebug

You can run pecl list to see which extensions are installed:

pecl list

# Installed packages, channel
# =========================================
# 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
# =======================================
# Package Stable/(Latest) Local
# pdflib  4.1.2 (stable)        Creating PDF on the fly with the PDFlib library

Make sure to restart your web server after installing new packages:

sudo nginx -s reload
sudo apachectl restart

If you're using Laravel Valet, you should restart it as well.

valet restart

Make sure all extensions are correctly installed and loaded by checking both your PHP webserver and CLI installs:

php -i | grep redis

If extensions aren't properly loaded, there are two easy fixes.

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,

Now check the ini file:


Note that if you're testing installed extensions via the CLI, you don't need to restart nginx, apache or Valet when making changes to ini settings.

The second thing you can do, if you're updating from an older PHP version which also used pecl to install extension; is to reinstall every extension individually.

pecl uninstall imagick
pecl install imagick

# Last step

Finally you should test and upgrade your projects for PHP 8 compatibility.