PHP Opcache Error on MacOS
This happened to me, so i hope it helps others.
When running php cli commands, warnings about opcache loading were displayed:
$ php -v
Failed loading /usr/local/opt/php/lib/php/20220829/opcache.so: dlopen(/usr/local/opt/php/lib/php/20220829/opcache.so, 0x0009): tried: '/usr/local/opt/php/lib/php/20220829/opcache.so' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/usr/local/opt/php/lib/php/20220829/opcache.so' (no such file), '/usr/local/opt/php/lib/php/20220829/opcache.so' (no such file)
PHP 8.2.4 (cli) (built: Mar 16 2023 16:25:32) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies
with Zend OPcache v8.2.4, Copyright (c), by Zend Technologies
Version Info:
- MacOS Ventura 13.3.1 (but i'm pretty sure this stands for any MacOS version).
- Homebrew 4.0.11
Background
I had installed php 5.6 for a legacy project. I then switched to php 8.2. I had installed 5.6 with a download from php.net. I installed php 8.2 using brew
. (Spoiler alert, the 5.6 install seems to have left behind an extra ini file).
Locate opcache.so/php.ini
- Find your php.ini by looking at
phpinfo()
.
- Find all versions of opcache. I used
locate
which may or may not be enabled on your mac. Another option would be to use find / -name opcache.so
but that could take a while and would need to be run via sudo
maybe.
phpinfo() output
My phpinfo()
showed an additional ini file parsed:

[opcache]
zend_extension=/usr/local/opt/php/lib/php/20220829/opcache.so
More on this below, for now, let's find out what opcache.so exist....
locate opcache.so
Here are all the opcache.so that were found on my system:
$ locate opcache.so
/usr/local/Cellar/php/8.2.4/lib/php/20220829/opcache.so
/usr/local/Cellar/php@5.6/5.6.40_6/lib/php/20131226/opcache.so
/usr/local/php5-7.3.8-20190811-205217/lib/php/extensions/no-debug-non-zts-20180731/opcache.so
There are three. The 8.2 opcache.so (the one i want to use with php 8.2), the 5.6 opcache.so (a second one included by brew
, since i also installed 5.6 using brew), and then the php.net installed 5.7 opcache.so.
Now that we know (a) why php can't find it (errant ini file content) and (b) the path to the ini file we do want to use), we simply update the ini file to point to the right opcache.so file.
Shutup jj, fix the issue
The extra ini file was pointing to the wrong opcache.so file.
Edit the opcache ini file and fix php opcache:
[opcache]
;zend_extension=/usr/local/opt/php/lib/php/20220829/opcache.so
zend_extension=/usr/local/Cellar/php/8.2.4/lib/php/20220829/opcache.so
(The semi-colon is a comment; i left the old line there for future reference).
Result:
$ php -v
PHP 8.2.4 (cli) (built: Mar 16 2023 16:25:32) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies
with Zend OPcache v8.2.4, Copyright (c), by Zend Technologies
(The error is no longer evident now that the extra parsed ini file points to the correct opcache.so file.)