Installing memcache on Windows for PHP

Posted on January 10, 2008. Filed under: Apache, Memcache, PHP | Tags: , , , , , |

LAST UPDATED MAY 7, 2009

Installing memcache on Windows XP / Vista is kind of like voodoo for those of us who are not disciplined with compiling code from source. I initially attempted to install memcache a few months ago after reading a few articles about how much performance it can pump into your web application. The problem is that memcache was written with Linux in mind, not windows. So you can’t download any installers or exe files from memcache’s site for windows … which leaves people like me, who use WAMP stacks to develop applications, out in the cold.

So after a few hours of Googling I found a cocktail of methods and files to get memcache to work for win32.

A few things about memcache:
Memcache is a daemon, meaning it runs as a separate service on your machine. Just like MySQL runs as a separate service. In fact, to use memcache in PHP you have to connect to it, just like MySQL.

Think of memcache as the $_SESSION variable for PHP, but instead of it working on a per-user basis, it runs over the entire application — like MySQL. In fact, you can use memcache as you session handler for PHP.

 

This is how I got memcache to work on my windows machine:

  1. Download memcache from code.jellycan.com/memcached/ [grab the 'win32 binary' version]
  2. Install memcache as a service:
    • Unzip and copy the binaries to your desired directory (eg. c:\memcached) [you should see one file, memcached.exe] – thanks to Stephen for the heads up on the new version
    • If you’re running Vista, right click on memcached.exe and click Properties. Click the Compatibility tab. Near the bottom you’ll see Privilege Level, check “Run this program as an administrator”.
    • Install the service using the command: c:\memcached\memcached.exe -d install from the command line
    • Start the server from the Microsoft Management Console or by running one of the following commands: c:\memcached\memcached.exe -d start, or net start "memcached Server"

 

Now that you have memcache installed, you’ll have to tie it in with PHP in order to use it.

  1. Check your php extensions directory [should be something like: C:\php\ext] for php_memcache.dll
    If you don’t have any luck finding it, try looking at one of these sites:
    - downloads.php.net/pierre/ [thanks to Henrik Gemal]
    - pecl4win.php.net/ext.php/php_memcache.dll [currently down]
    - www.pureformsolutions.com/pureform.wordpress.com/2008/06/17/php_memcache.dll for PHP 5.2.*
    - kromann.info/download.php?strFolder=php5_1-Release_TS&strIndex=PHP5_1 for PHP 5.1.* [thanks, Rich]
  2. Now find your php.ini file [default location for XP Pro is C:\WINDOWS\php.ini] and add this line to the extensions list:
    
    extension=php_memcache.dll
    
    
  3. Restart apache
  4. Run this code to test the installation: [found on www.php.net/memcache]
    
    <?php
        $memcache = new Memcache;
        $memcache->connect("localhost",11211); # You might need to set "localhost" to "127.0.0.1"
    
        echo "Server's version: " . $memcache->getVersion() . "<br />\n";
    
        $tmp_object = new stdClass;
        $tmp_object->str_attr = "test";
        $tmp_object->int_attr = 123;
    
        $memcache->set("key",$tmp_object,false,10);
        echo "Store data in the cache (data will expire in 10 seconds)<br />\n";
    
        echo "Data from the cache:<br />\n";
        var_dump($memcache->get("key"));
    ?>
    
    

If you see anything but errors, you are now using memcache!

EDIT

Memcached, by default, loads with 64mb of memory for it’s use which is low for most applications. To change this to something else, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server in your registry, find the ImagePath entry and change it to look something like this:

“C:\memcached\memcached.exe” -d runservice -m 512

Now when you start the service via net start “memcached Server”, it will run with 512mb of memory at it’s disposal.

Make a Comment

Make a Comment: ( 76 so far )

blockquote and a tags work here.

76 Responses to “Installing memcache on Windows for PHP”

RSS Feed for Adventures in PHP / DHTML / CSS and MySQL Comments RSS Feed

Thanks, its so useful!!

any tips on how to increase max memory? -m in command line doesn’t seem to do it for me.

colum,

I added to the post addressing the max memory.

Really very useful article.
I got everything .
Thank you very much.

Thank you – this is great!

[...] on May 21, 2008. Memcache is a great caching tool available for nearly every scripting or programming environment. I use it [...]

Dude, thank you for this! I was stuck at 65Megs and couldn’t figure out how to change that setting. Thanks!

The pecl4win.php.net site is down. Anyone know where I can find the windows binary for the pecl memcache client?

Try here: http://www.pureformsolutions.com/pureform.wordpress.com/2008/06/17/php_memcache.dll

This is 6 for PHP 5.2.6 [though it will probably work for 5.2.x]

Bloody awesome guide, thanks mate, Me love you long time :P

Use the 1.2.5 binaries from here:

You can get a 1.2.5 binary here:
http://code.jellycan.com/memcached/

Or here:
http://code.google.com/p/beitmemcached/downloads/list

Both of these are much newer and most importantly remove a very troublesome problem with Memcached and Windows machines with multi-cores where one core would be eaten up by Memcached.

thanks, very useful and simple manual!

Kudos, my friend. This was a great help :)

Remember to use the non-thread-safe version of the pecl library if your php is non-thread safe, otherwise the dll won’t load [check using phpinfo()]

Thanks brad ! That’s a great useful work.

Man this really rocked for me. I was hunting it down for months and got it now. Like you I have my test and development environment on my local PC which is windows and it was always difficult for me to test the things equally on my devel box and prod box :)

Great jobs ! thanks a lot. Your tutorial is very clear.

Thank you very much

[...] XP: http://pureform.wordpress.com/2008/01/10/installing-memcache-on-windows-for-php/ This was written by dani. Posted on Monday, November 17, 2008, at 7:24 am. Filed under [...]

This didn’t work for me… I got a “failed to install service or service already installed” error on installation.

Furthermore, I got the latest version of memcache from Jellycan but it only includes the .exe file mentioned here, not the .dll.

No one else had this problem?

if you are missing this file MSVCR71.dll just like me then download it from: http://www.dll-files.com/dllindex/dll-files.shtml?msvcr71

It worked for me.

r u sure u r executing first install command then start command?
i got the same error “fail to install service” then i realize i was directly executing start command without executing install command. make sure u have execute first install command

Dude, you saved my lot of time!
Thank you so much.. it is really very worth information!

if you are trying to install memcached on windows vista, right click on the memcached.exe, go to its properties and make sure on the compatibility, run as administrator is selected.
good luck

Very helpful.Thanks for the guidence.

Thanks a lot. Great job!

Hey! thank a bunch man!
was planning on experimenting with memcache for a while, but to lazy to figure out how to get it working on win32..
saved my day!

greate article! thanks!!

super super :)

Thanks You! Very much. It is very good article to install memcache in widows. It helps me a lot.

while restating the Apache after extension added I am getting following error in the error.log and extension not loading

PHP Warning: PHP Startup: memcache: Unable to initialize module\nModule compiled with module API=20050922, debug=0, thread-safety=1\nPHP compiled with module API=20060613, debug=0, thread-safety=1\nThese options need to match\n in Unknown on line 0

Please can anybody help me

php_memcache.dll can be found/downloaded here:

http://downloads.php.net/pierre/

very detailed description.. very useful. saved lot of my time. thanks.

How to install it in a IIS7.5 (w2k8 R2) which is only x64 not x86.
memcached.exe install fails looking for msvcr71.dll
Cheers!

Thanks a lot!

I am running EasyPHP 2.0b1.
I installed memcached from the windows binary.
I started the memcached service and can telnet into it just fine.
The php_memcache.dll file is present in the ext directory.
The extension is enabled in php.ini and shows up with phpinfo().
I’ve tried turning off my firewall.

Still, I get this error when trying to connect from PHP:

Warning: Memcache::connect() [function.Memcache-connect]: Can’t connect to localhost:11211, A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060)

Any suggestions would be greatly appreciated!

very helpful! thanks a lot

OK, I changed
$memcache->connect(‘localhost’,11211);
to
$memcache->connect(‘127.0.0.1′,11211);
and it worked!

!st, This is my most favorite blog post in the whole internet – thank you and others who helped make a binary…

In general, is it ok or normal to install memcached on the webserver machine (localhost) or better to use on external machines in the same LAN ?

Anyone know where I can find a .dll for PHP 5.3 ??

And I need one for PHP 5.1.6. . .

And I (finally) found one that worked here: http://kromann.info/download.php?strFolder=php5_1-Release_TS&strIndex=PHP5_1. Hopefully this will help someone else who can’t get memcached to load.

[...] статья — вольный перевод, оригинал: Installing memcache on Windows for PHP Эта запись была опубликована в Вторник 12-го Мая 2009 [...]

Thanks mate, got it up and running in no time thanks to this guide

[...] Thursday 14th May, 2009 Handy resource on installing memcache on windowsCheck out this website I found at here [...]

[...] Installing memcache on Windows for PHP « Adventures in PHP / DHTML / CSS and MySQL [...]

Awesome! This made setup a breeze!

Скажите а материалы с Вашего сайта можно у себя размещать?

Very useful! Thanks

this document really rocking anyone can do this instalation .This really clear.nice .I am impressed .with out admin i did instalation

Something I found a while ago (sorry, I can’t remember where, so can’t give proper credit). Makes a “nicely” named service, and sets the max memory at 2gigs and port at (the default) 11211:

“sc create memcached11211 binPath= “C:\php\memcached\memcached.exe -d runservice -p 11211 -m 2048″ start= auto DisplayName= “MemCached 11211″
(you MUST put the little spaces after the “=” signs!)
This will register a service called “memcached 11211″, with auto start enabled, the DisplayName “MemCached 11211″ and the parameters in the binPath.
On a side note, we’re currently running Kenneth’s 1.2.4 Win32-preview build (http://www.splinedancer.com/memcached-win32/) in production with pretty heavy traffic (largest webshop in northern Europe), working great so far. Only problem we have detected is that the service refuses to stop, you must kill the process to stop it. Only a minor annoynance. :-)”

this article helped me a lot to install memcache.
thanks a lot…..

i downloaded the windows version but there is no memcached.exe file. Anyone else run into this issue?

nevermind..i downloaded teh wrong file. doh.

Great installation guide. Thanks for sharing with us.

thanks a lot! made it with some problems on W7 :)

Its Done Boss, really working!!!

Thanks, got me up and running in no time!

Thank you. Excellent!

hi great post 10x, however i was unable to execte the example i am getting:
“Fatal error: Class ‘Memcache’ not found”
i added the dll to the ext list in the php.ini and restart apache with no luck please advise.

You ought to check and see if the memcached extension is complied for the version of php you have installed… You can check apache’s error_log to see if that’s the issue. The files listed above are for php 5.1 and php 5.2, so if you’re running 5.3 or a 6.0 build, it wont work properly.

thanks for the help however i am using php 5.2.0 here are some more details that may shade some light.

running the phpinfo i get:
php version 5.2.0
system Windows NT USER-PC 6.0 build 6001
apache api version 20020903

i using Vista

where else should i check to get more details that may help to track the problem.

10x Oded

I manage to work it out, it seems that PHP 5.2.0 cannot load the memcached dll, after upgrading to 5.2.10 everything went well.

Thanks for the help.

Oded

great work!

Thank u so much.

it’s the first tutorial about this topic, that really worked for me. thank you so much.

Great article – thanks very much!

I’m looking for the PECL Win32 php_memcache.dll for PHP 5.3 I only found the one for PHP 5.2
Thanks!

tks for the post :D

Thank you for the fantastic Step by Step expanation.
The Links to the memcached.dll were just what I needed to make it work.
Thank you very much.

thanks! nice tutorial

gee, thank u so much guys, my memcache is working now…

wow!!!simply great, thanks alot, i really found it helpful. Can you write more examples of using memcached.


Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...