Let's get started with the basic installation of memcached on Ubuntu Linux 12.04LTS (long-time support) using apt-get. We have picked this particular version of Ubuntu because it's the latest LTS version that came out while writing this book, however, the steps of installation are the same for any other Ubuntu version. LTS is generally recommended for production servers because it gets a long period of maintenance and support from the folks at canonical.
You will need to have an administrator account on the Ubuntu box you are setting up. If you are performing some tests then most likely any machine would do the job but if you are setting this up as a production environment, you will need a machine with a decent amount of free memory for the caching job.
Update your apt local repository by using:
sudo apt-get update
When asked for the password, just enter your account password to give permission to the application to run as root.
Use
apt-get
to install the memcached service:sudo apt-get install memcached
Now, let's verify that memcached service has been started.
ps aux | grep memcached
You are supposed to see something similar to the following:
memcache 830 0.0 0.1 323220 1188 ? Sl 17:33 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
First, we pulled the latest packages information from the apt repository online to make sure we are downloading the latest version of memcached to our local server. Then we simply used the apt-get
command to download and auto-install the memcached package.
The installation script also starts the memcached daemon and marks this service to be auto-started on every boot of our Ubuntu box.
We validated that memcached was properly started by checking the running processes with the ps
command and grep-ing
to see only processes with the word memcached in them.
It's also important to note here that the default configuration of memcached limits the memcached daemon to listen only on the loopback device (localhost). This means that you can connect to your memcached daemons only from local processes running on the same computer.
Let's take a look at the configuration of the memcached daemon installed on our Ubuntu server.
Open the configuration file located at /etc/memcached.conf
and locate the line where you see something like the following:
-l 127.0.0.1
This tells the memcached daemon to listen only on the localhost, note that this is the only security measure that memcached can offer, so make sure it's listening on a firewalled interface.
Change it to the following if you want the daemon to listen on all interfaces.
-l 0.0.0.0
Also locate the following line:
-m 64
This configuration parameter configures the upper cap of how large the in-memory storage can grow to. The default here is 64 megabytes. This means that you can store up to 64 MB worth data on your memcached daemon, but this doesn't mean that the daemon will allocate this memory on its boot.
In most cases you will only need memcached on windows for development or testing, it's quite unlikely to see memcached installed on a production server.
Memcached is written in C so it's portable, but it is not officially supported or recommended to run on Windows. However, there have been a few ports to Windows, a popular one can be found at http://code.jellycan.com/memcached/, see memcached for win32. As advertised, it comes with no promises or support.