Tutorial: Installing Memcache on Windows

There are plenty of guides on this, but I couldn’t find any that explained the actual importance and difference in PHP and Apache versions and bit versions – and thus which file to download and use. Based on the guides out there and my experience (many, many frustrating hours) on installing Memcache for localhost on two different Windows localhost setups, I created a guide on how you can install Memcache on Windows 7 – taking your installed software versions into account.

I use WampServer as my local webserver, but this guide should work on any webserver install, such as XAMPP.

As opposed to the quick and easy install-and-use on Linux, Memcache needs to be installed and started as separate process in Windows. And as far as I know, Memcache on Windows works only as Memcache, not Memcached with a ‘d’. Even though the install and process file is named Memcached, you still use Memcache (no ‘d’) when using it.

Before Searching for Files, Find Your Versions

Your PHP version, Apache version and webserver software’s bit version matters for getting Memcache to work. With WampServer you can open up localhost/ in a browser, and the PHP and Apache version should be listed in the info. I run Apache 2.2.21 and PHP 5.3.10 both at work and my stationary computer at home.

Then you need to find out if your webserver is 32 bit or 64 bit. Not Windows – but the webserver software, in my case WampServer (which can be installed both as 32 bit or 64 bit). If you still have the original install file it could give you a clue (with WampServer the install file ends with -x32 for 32 bit or -x64 for 64 bit. If you don’t have the original install file, open up Windows Task Manager and find httpd in Processes (assuming your webserver’s running). Running Windows 7 64 bit, the process name would be httpd *32 for 32 bit, or 64 bit if there are no asterisk and numbers after httpd. I run Windows 7 64 bit at both work and home, but at home I’ve installed 32 bit and at work I have 64 bit WampServer.

Written down your PHP version, Apache version and webserver’s bit version? Great, now you have all necessary info for finding and downloading the files.

What You Need to Find and Download

You need two things: a memcached.exe file which you use to install and start the Memcache process in Windows. The other is a php_memcache.dll file which you’ll add to your webserver so it recognizes Memcache as an extension. Both files need to match your webserver’s bit version. Let’s begin with the first one, the install file.

Either Google “memcached.exe windows 32 bit” (or 64 bit if you have that) or download the 32 bit file here.

Installing and Starting Memcache

Unzip the file into a separate folder, such as C:\memcache\.

Open up cmd.exe in Administrator mode. (That means right clicking on cmd.exe and choose “Run as administrator”). Navigate to the C:\memcache\ folder.

First we install Memcache by typing

memcached.exe -d install

When it’s completed, we start Memcache by typing

memcached.exe -d start

If you open up Windows Task Manager, you should find memcached.exe *32 (or without the *32) in Processes. That was it! Memcache is now listening to the default port 11211.

Increasing Memcache’s Memory Limit

Just one more thing. By default, Memcache is set to a 64Mb memory limit, which is generally too small. To correct this, open up Registry Editor (regedit.exe). Navigate to:

HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached Server/

In this folder, doubleclick on the ImagePath key, and add ” -m 512″ after its value data, so its full value becomes:

"C:\memcache\memcached.exe" -d runservice -m 512

Now we’ve expanded Memcache’s memory limit to 512Mb. Close the Registry Editor. Let’s move on to the extension file.

Installing the Memcache PHP Extension

The webserver won’t know how to refer to Memcache until we install it as a PHP extension. So now we need to find a php_memcache.dll file that matches the Apache and PHP version and webserver bit version. The important thing is that you can’t use a file targeted at a higher PHP or Apache version than you have (lower works just fine), and it must match the bit version. Google “php_memcache.dll” with your PHP version and bit version and you should find a matching file quite easily. If you are lazy and have the same versions as I do, you can download copies of my 32 bit for PHP 5.3 and Apache 2.2.6 or 64 bit for PHP 5.3 and Apache 2.2.6.

Unzip and move the php_memcache.dll file into your webserver’s PHP extension folder. For WampServer the folder is located at:

/wamp/bin/php/php5.3.10/ext/

Now we need to edit the webserver’s php.ini so we can tell it to load the Memcache library. With WampServer click the tray icon, hover over PHP and click php.ini to open it up in an editor.

Search for “extension=” and scroll down until you find several lines with “extension=php_….dll”. If the line has a preceding “;” it’s commented out and not active. At the end of the list of extensions, add a new line with:

extension=php_memcache.dll

Save, close php.ini and restart your webserver services.

If all went well and you used the right files, Memcache should be installed and ready to use! Write a simple Memcache test in PHP, see that it works and pop a beer. Cheers!

It’s Not Working!

Click the WampServer tray icon, navigate to PHP and then PHP extensions, and see that “php_memcache” is in the list and is checked. Also double check that “memcache” is in the list of extensions on localhost/. If it’s not there, your webserver couldn’t load the memcache extension.

The most likely problem is that the php_memcache.dll file did not match. Check the Apache error log. If it spits out something containing 32 or 64 bit, the bit version is wrong. Otherwise it could be a mismatch to the Apache and/or PHP version. Remember to restart Apache every time you edit php.ini or tries a new extension file.

If all else fails, Google is your friend.