PHP cannot find the Memcache class
Fatal error: Class 'Memcache' not foundShort answer
Memcache is a PECL extension that PHP does not bundle. The class exists only if that extension is installed and loaded by the PHP your page runs under. Check php -m for the CLI and a probe page for FPM or Apache. Memcache and Memcached are two different extensions with different class names.
Fix it fast
1Find out which cause you have
List which of the two memcache extensions this PHP has loaded. Run it, then repeat the check through the web server with phpinfo.
php -m | grep -i memcache php --ini2Match what you see, then jump to its fix
What does the error look like?
Fatal error: Class 'Memcache' not found
Cause 1 of 3
The extension is not installed
1Check
The manual says Memcache is a PECL extension and is not bundled with PHP, so a stock install does not have it.
php -m | grep -i memcache2Fix
Install it for the PHP version you run, using your distribution's package or the PECL installer, then enable it in php.ini and restart the web service.
pecl install memcache
# then enable the extension in the php.ini you load3Verify
The extension is listed.
php -m | grep -i memcacheConfirmed by php.net
Cause 2 of 3
It is enabled for the CLI but not for the web PHP
1Check
The CLI and the web server can read different php.ini files. The manual lists a search order that depends on the SAPI, and the scan directory can add more files. Put phpinfo() on a page and read the loaded configuration file there.
php --ini
# on a web page: <?php phpinfo();2Fix
Enable the extension in the file the web PHP loads, then restart PHP-FPM or Apache; the PECL page says a restart is needed.
sudo systemctl restart php8.3-fpm3Verify
Request a page that runs class_exists('Memcache').
<?php var_dump(class_exists('Memcache'));Confirmed by php.net
Cause 3 of 3
Memcached is installed, but the code asks for Memcache
1Check
The manual documents a separate extension, Memcached, with its own class named Memcached. It does not provide a class called Memcache.
php -r 'var_dump(class_exists("Memcache"), class_exists("Memcached"));'2Fix
Either install the Memcache extension, or change the code to the Memcached class and its methods. Check the Memcached manual for how servers are added.
$m = new Memcached();3Verify
class_exists returns true for the class your code uses.
php -r 'var_dump(class_exists("Memcached"));'Confirmed by php.net
How do I stop it coming back?
- List extensions the app needs under require in composer.json, so a missing one shows on install.
- Pick one memcache extension per project and keep the choice in the README.
- After a PHP upgrade, check the loaded extensions again for every SAPI.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
PHP cannot load the phpredis extension (redis.so)
PHP Warning: PHP Startup: Unable to load dynamic library 'redis.so'
PHP loaded a php.ini that names redis.so, but no file with that name exists for this PHP build, or the CLI and FPM read different ini files.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack end
CodeIgniter 4 cannot write its cache files
CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/
CodeIgniter 4's file cache needs a cache directory that the web server can really write to.
2 causes, each with a fixBack end
CodeIgniter 3 loads the model but the property is undefined
The model name you are loading is the name of a resource that is already being used: user
CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.
2 causes, each with a fix