Skip to content

Memcached - Environment

To start using Memcached, you need to install the Memcached server and, for application development, a client library for your chosen programming language. Here we cover modern setup methods for local development.

Section titled “Option 1: Installing with Docker (Recommended)”

Using Docker is the easiest and most consistent way to run Memcached across different operating systems (Windows, macOS, Linux). It isolates the service and avoids conflicts with other software.

If you don’t have it, download and install Docker Desktop from the official Docker website.

Open your terminal or command prompt and run the following command:

docker run --name my-memcached -p 11211:11211 -d memcached:latest

This command does the following:

  • docker run: Creates and starts a new container.
  • --name my-memcached: Assigns a memorable name to the container.
  • -p 11211:11211: Maps port 11211 on your local machine to port 11211 inside the container.
  • -d: Runs the container in detached mode (in the background).
  • memcached:latest: Specifies the official Memcached image from Docker Hub.

If you prefer a native installation on a Linux system like Ubuntu:

sudo apt update

Install the server and a suite of helpful command-line utilities.

sudo apt install memcached libmemcached-tools

You can check if the service is running:

sudo systemctl status memcached

By default, the package often configures Memcached to listen only on the local interface (127.0.0.1), which is secure. If you ever configure it manually, ensure it is NOT listening on a public IP address (0.0.0.0) unless protected by a firewall, as Memcached has no built-in authentication.

To use Memcached in a Java project, you need a modern build tool like Maven or Gradle to manage dependencies. Manually downloading JAR files is an outdated and error-prone practice.

In your project’s pom.xml file, add the following dependency for the spymemcached client:

<dependencies>
<!-- other dependencies -->
<dependency>
<groupId>net.spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.12.3</version> <!-- Check for the latest version on Maven Central -->
</dependency>
</dependencies>

In your build.gradle file, add the following to the dependencies block:

dependencies {
// other dependencies
implementation 'net.spy:spymemcached:2.12.3' // Check for the latest version
}

With this setup, your build tool will automatically download and manage the required library, making your project portable and easy to build.