Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP cannot find the Memcache class

ErrorFatal error: Class 'Memcache' not found

Short 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.

  • Applies to PHP 8.3, PHP 8.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 --ini
  2. 2Match what you see, then jump to its fix

    1. 1php -m prints nothing for memcache.The extension is not installedFix
    2. 2php -m lists memcache, but the page still fails.It is enabled for the CLI but not for the web PHPFix
    3. 3php -m lists memcached (with a d), not memcache.Memcached is installed, but the code asks for MemcacheFix

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 memcache

2Fix

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 load

3Verify

The extension is listed.

php -m | grep -i memcache

Confirmed by php.net

Did this fix it?

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-fpm

3Verify

Request a page that runs class_exists('Memcache').

<?php var_dump(class_exists('Memcache'));

Confirmed by php.net

Did this fix it?

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

Did this fix it?

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 fix

Back 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 fix

Back 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.