89

I've pasted the example from php.net for using memcached in php and I'm getting:

Fatal error: Class 'Memcache' not found

I have this in my php.ini:

[memcache]
memcache.hash_strategy = "consistent"
memcache.max_failover_attemps = 100
memcache.allow_failover = 1

Here's info from php -i and php -m respectively:

php -i | grep -i memcache
memcached
memcached support => enabled
libmemcached version => 0.37
Registered save handlers => files user sqlite memcached

php -m | grep -i memcache
memcached

So php seems to have loaded memcached as a module, and php info says that it is loaded and is using v .37. I have not tried yet via apache, I've only been using this through cli right now. Any thoughts?

Community
  • 1
  • 1
jpfuentes2
  • 2,085
  • 1
  • 18
  • 19
  • 1
    possible duplicate...http://stackoverflow.com/questions/1171313/memcache-not-found-in-command-line-php-script –  Apr 17 '10 at 16:10
  • I just took a look at that and I already have the extension listed in my php.ini and I've already ensured my extension_dir is pointed to the correct place. – jpfuentes2 Apr 17 '10 at 16:26
  • See my answer below for the simple command to install 'MEMCACHE'. – Phill Healey Nov 22 '16 at 11:15
  • 1
    `sudo apt-get install php-memcache` is what worked for me. https://packages.ubuntu.com/search?keywords=memcache&searchon=names&suite=yakkety&section=all shows available packages. – Ryan Jul 05 '17 at 21:59
  • I hve the same problem with php 8.1 and php8.2 right now. It seems like the [object-cache.php](https://plugins.trac.wordpress.org/browser/memcached/trunk/object-cache.php) file is not compatible any more? – rubo77 Apr 19 '23 at 09:04
  • I tried to install the module with "d" but failed. I installed Redis now instead, see: https://unix.stackexchange.com/a/743405/20661 – rubo77 Apr 19 '23 at 10:16

7 Answers7

148

There are two extensions for memcached in PHP, "memcache" and "memcached".

It looks like you're trying to use one ("memcache"), but the other is installed ("memcached").


In the ~13 years since this answer was written, much has changed in the PHP community. The old memcache extension seems to be unmaintained, so please use the memcached extension instead if you can. It's a better option regardless! See this other answer below for up to date installation and build links.

Charles
  • 50,943
  • 13
  • 104
  • 142
  • 7
    It is kind of silly, but it's an unfortunately frequent pattern in PHP. Rather than updating the older extension to work with newer things in better ways, but breaking backwards compt, they simply created a new extension. See also mysql and mysqli. – Charles Apr 17 '10 at 20:48
  • This seems like the right problem. But all the instructions I find seem to say to install memcached, but see memcache. This is a bit confusing. – Patrick Jun 27 '13 at 18:02
  • 7
    Ok, I found a simple fix (since everything else seemed to not work). Just run this command: sudo aptitude install memcached php5-memcached – Patrick Jun 27 '13 at 18:36
  • Note that those package names will only work on certain specific Debian derivatives. The package names will be different in other environments. – Charles Jun 27 '13 at 18:48
  • This is ridiculous... thanks for the tip. I have both extensions loaded- would this make any impact on performance? – Matt Fletcher Apr 10 '15 at 10:05
  • 1
    There should be no real performance impact from having both loaded, no. – Charles Apr 10 '15 at 20:02
  • For the correct way to install 'Memcache' see my answer below. – Phill Healey Jan 28 '16 at 09:29
  • memcache (without "d") is not updated for 3 years, so it seems not to work with php8 – rubo77 Apr 19 '23 at 08:13
  • 1
    @rubo77 -- It does, according to the PECL site for memcache. See also the post below: https://stackoverflow.com/a/76093380/19980636 – Krokomot Apr 24 '23 at 15:12
  • 1
    @Krokomot It's been thirteen years and eight days since I wrote this answer, and back then memcache and memcached were both active and maintained. I've added a note about memcache being unmaintained and linked to your post. Thanks for diving in! – Charles Apr 25 '23 at 18:24
24

Dispite what the accepted answer says in the comments, the correct way to install 'Memcache' is:

sudo apt-get install php5-memcache

NOTE Memcache & Memcached are two distinct although related pieces of software, that are often confused.

EDIT As this is now an old post I thought it worth mentioning that you should replace php5 with your php version number.

Phill Healey
  • 3,084
  • 2
  • 33
  • 67
  • A couple years later, I think this answer is outdated. `sudo apt-get install php-memcache` is what worked for me. https://packages.ubuntu.com/search?keywords=memcache&searchon=names&suite=yakkety&section=all shows available packages. – Ryan Jul 05 '17 at 22:00
  • 1
    @Ryan Surely, that only applies if not using PHP5. Plenty of servers still use PHP5 so it's just as valid as it was at the time. Additionally the question is tagged ubuntu-9.10 so this further validates my answer since it relates directly to that version. – Phill Healey Jul 06 '17 at 11:46
  • "Memcached & Memcached" ?? – Brian Leishman Dec 06 '18 at 13:49
  • Is there a solution for windows? – IrfanAnwar Nov 05 '20 at 10:50
2

I found solution in this post: https://stackoverflow.com/questions/11883378/class-memcache-not-found-php#=

I found the working dll files for PHP 5.4.4

I don't knowhow stable they are but they work for sure. Credits goes to this link.

http://x32.elijst.nl/php_memcache-5.4-nts-vc9-x86.zip

http://x32.elijst.nl/php_memcache-5.4-vc9-x86.zip

It is the 2.2.5.0 version, I noticed after compiling it (for PHP 5.4.4).

Please note that it is not 2.2.6 but works. I also mirrored them in my own FTP. Mirror links:

http://mustafabugra.com/resim/php_memcache-5.4-vc9-x86.zip http://mustafabugra.com/resim/php_memcache-5.4-nts-vc9-x86.zip

Community
  • 1
  • 1
Xman Classical
  • 5,179
  • 1
  • 26
  • 26
2

For OSX users:

Run the following command to install Memcached:

brew install memcached
Maihan Nijat
  • 9,054
  • 11
  • 62
  • 110
2

To avoid confusion as this is an older question, a small prologue:

rubo77 refreshed this question by placing a bounty and asking for a canonical answer. Reading the little bit old but nevertheless still valid answers -- see esp. the accepted one -- , I realized that indeed some basic but important, so to say canonical, links miss.

So here shall be provided not a canonical answer as such -- which could change in details over the years -- but canonical links which will provide the answer.


The Missing Link

Sorry for the fossil joke ^^. Ok, back to serious. The main canonical source to answer the question is of course php.net, and more precisely:

The extensive and up-to-date ServerPilot installation guide for Memcached as well as Memcache is also pretty useful.

Naturally, the question comes up -- What about PHP8? Lets take a look into the respective repository, the PHP Extension Community Library (PECL), and we find:

  • The last Memcache package stable release 8.0 from 2020-12-06 depends on PHP>=8.0.0. For PHP>=7.0 take release 4.0.5.2 from 2019-12-20.
  • The last Memcached package stable release 3.2.0 from 2022-03-24 depends on PHP>=7.0.0, so can also be used with PHP8.

Note, that Memcache (without 'd') is no longer maintained.


So these sources should be the very first places to look for (after Stackoverflow, of course), before starting a deeper web search campaign (which won't yield that much more, tbh).

Krokomot
  • 3,208
  • 2
  • 4
  • 20
1

The right is php_memcache.dll. In my case i was using lib compiled with vc9 instead of vc6 compiler. In apatche error logs i got something like:

PHP Startup: sqlanywhere: Unable to initialize module Module compiled with build ID=API20090626, TS,VC9 PHP compiled with build ID=API20090626, TS,VC6 These options need to match

Check if you have same log and try downloading different dll that are compiled with different compiler.

halfer
  • 19,824
  • 17
  • 99
  • 186
-10

I went into wp-config/ and deleted the object-cache.php and advanced-cache.php and it worked fine for me.

neofetter
  • 3,004
  • 2
  • 26
  • 26
  • 3
    All that did was prevent your WordPress install from being able to do any object caching. It's not actually a fix for anything, let alone the question asked by the OP. – Phill Healey Aug 16 '18 at 12:19