Memcached on Debian gives web applications a fast in-memory cache for database query results, sessions, API responses, and WordPress object caching. The safest way to install Memcached on Debian is the default APT package because it starts a systemd service, binds to localhost, and receives Debian security maintenance. Build from source only when you need a newer upstream release or a custom compile option that the Debian package does not provide.
Memcached does not persist data and does not make an application faster by itself. Your application, PHP extension, Python client, Perl module, or WordPress cache plugin must connect to the Memcached daemon and decide what to cache. If your workload needs persistence, richer data structures, replication, or authentication-first server behavior, Install Redis on Debian may fit better.
Install Memcached on Debian
APT is the recommended Memcached installation method for Debian 13, 12, and 11. It installs the daemon, creates the memcache service account, enables the systemd unit, and uses Debian’s package-maintained configuration file at /etc/memcached.conf.
Choose the Memcached Installation Method
| Method | Source | Version and Update Behavior | Best Use |
|---|---|---|---|
| APT package | Debian package sources | Debian 13 ships 1.6.38, Debian 12 ships 1.6.18, and Debian 11 ships 1.6.9 from default package sources; updates arrive through APT. | Most servers, production maintenance, and security updates |
| Source build | Memcached upstream releases | Manual version selection from upstream releases; the source example uses 1.6.42 and needs manual rebuilds. | Custom builds or features not available in the Debian package |
The daemon package is memcached. Install php-memcached later only when a PHP application needs the Memcached extension; it is not the server daemon.
Update Debian Packages Before Installing Memcached
Refresh the APT index before installing Memcached:
sudo apt update
If your account cannot run
sudo, add it to the sudo group before continuing. The add a user to sudoers on Debian guide covers the Debian-specific steps.
Install the Memcached Package on Debian
Install Memcached and the command-line tools package:
sudo apt install memcached libmemcached-tools
The memcached package provides the daemon and systemd unit. The libmemcached-tools package adds utilities such as memcstat, which is useful for checking live cache statistics without opening an interactive telnet session.
Verify the Memcached Service on Debian
Check the installed Memcached version first:
memcached -V
On Debian 13, the default package currently reports:
memcached 1.6.38
Debian 12 reports memcached 1.6.18, and Debian 11 reports memcached 1.6.9. The package revision can change with Debian security updates, but those major upstream versions are the default package families for the supported releases.
Confirm that systemd enabled and started Memcached:
systemctl is-enabled memcached
systemctl is-active memcached
enabled active
Check the listener next. Debian keeps Memcached local-only by default:
ss -H -ltn 'sport = :11211'
Debian 13 shows both IPv4 and IPv6 localhost listeners:
LISTEN 0 1024 127.0.0.1:11211 0.0.0.0:* LISTEN 0 1024 [::1]:11211 [::]:*
Debian 12 and Debian 11 normally show the IPv4 localhost listener only unless you add an IPv6 listen line yourself.
Use memcstat to confirm that the daemon answers local requests:
memcstat --servers=127.0.0.1 | grep -E '^(Server|[[:space:]]+version|[[:space:]]+max_connections|[[:space:]]+curr_connections)'
Relevant output includes:
Server: 127.0.0.1 (11211)
version: 1.6.38
max_connections: 1024
curr_connections: 2
Configure Memcached on Debian
Memcached has no built-in access control in a normal default deployment, so the safest configuration is to keep it on localhost and let only local application processes connect. Change the network binding only for private application networks where a firewall restricts the client addresses.
Review the Debian Memcached Configuration File
APT-installed Memcached reads its settings from /etc/memcached.conf through Debian’s systemd wrapper:
sudo nano /etc/memcached.conf
The most important default lines are:
-m 64
-p 11211
-u memcache
-l 127.0.0.1
Debian 13 also includes -l ::1 by default. Keep localhost binding unless another server on a trusted private network must reach this cache.
Adjust Memcached Memory Allocation
The -m value controls the memory limit in megabytes. For example, change the default 64 MB allocation to 256 MB with:
-m 256
Choose a value that leaves enough RAM for the operating system, database, PHP workers, and other services. Memcached evicts old items when it reaches the memory limit, so a larger cache is useful only when your workload can reuse those cached items.
Bind Memcached to a Private Network Address
If an application on another private server must connect, add the private interface address while keeping localhost available:
-l 127.0.0.1
-l 10.0.0.10
Do not bind Memcached to a public interface. Memcached has historically been abused for amplification attacks, and a public unauthenticated cache can expose or corrupt application data.
Confirm Memcached UDP Is Disabled
Modern Memcached defaults UDP to off. Confirm that behavior before adding extra configuration:
memcached -h | grep -- '--udp-port'
-U, --udp-port=<num> UDP port to listen on (default: 0, off)
If you inherit an older configuration that explicitly enables UDP, set it back to zero:
-U 0
Restart Memcached After Configuration Changes
Restart Memcached after editing /etc/memcached.conf:
sudo systemctl restart memcached
systemctl is-active memcached
active
If the restart fails, read the recent unit logs before changing more settings:
journalctl -u memcached -n 30 --no-pager
Allow Remote Memcached Access with UFW
Skip this section for localhost-only deployments. If you bind Memcached to a private network address, allow only the application server or private subnet that needs access. For broader firewall setup details, see Install UFW on Debian.
Install UFW on Debian
Install UFW if the firewall is not already available:
sudo apt install ufw
If you manage the server over SSH, allow SSH before enabling UFW. The Install SSH on Debian guide covers OpenSSH setup if the service is not ready yet.
sudo ufw allow OpenSSH
Allow a Private Memcached Client
Allow one application server to reach TCP port 11211:
sudo ufw allow from 192.168.1.100 to any port 11211 proto tcp
For a private subnet, use CIDR notation instead:
sudo ufw allow from 192.168.1.0/24 to any port 11211 proto tcp
Enable UFW after the SSH and Memcached rules are in place:
sudo ufw --force enable
Verify Memcached Firewall Rules
Review numbered UFW rules:
sudo ufw status numbered
Status: active
To Action From
-- ------ ----
[ 1] OpenSSH ALLOW IN Anywhere
[ 2] 11211/tcp ALLOW IN 192.168.1.100
Install Memcached Client Libraries on Debian
The Memcached daemon only stores cache entries. Your application still needs a language client or framework integration to use it.
Install PHP Memcached Support on Debian
Install the PHP extension for Memcached-backed applications:
sudo apt install php-memcached php-cli
Confirm that PHP can load the extension:
php -m | grep '^memcached$'
memcached
Restart the PHP runtime that serves your application. Use the Apache command only on Apache-backed sites:
if systemctl list-unit-files apache2.service --no-legend | grep -q '^apache2.service'; then
sudo systemctl restart apache2
fi
For Nginx or other PHP-FPM deployments, restart the installed PHP-FPM unit instead:
PHP_FPM_SERVICE="$(systemctl list-unit-files 'php*-fpm.service' --no-legend | awk '{print $1; exit}')"
if [ -n "$PHP_FPM_SERVICE" ]; then
sudo systemctl restart "$PHP_FPM_SERVICE"
fi
Debian 13 uses php8.4-fpm, Debian 12 uses php8.2-fpm, and Debian 11 uses php7.4-fpm when installed from default Debian PHP packages. The broader Install PHP on Debian guide covers PHP package choices before you add Memcached support. For web server setup, see Install Apache on Debian or Install Nginx on Debian.
Install Python Memcached Support on Debian
Install the Debian-packaged pymemcache client for Python applications:
sudo apt install python3-pymemcache
Check the import path:
python3 -c "import pymemcache.client; print('pymemcache import OK')"
pymemcache import OK
Install Perl Memcached Support on Debian
Install the Perl Memcached module when Perl applications need cache access:
sudo apt install libcache-memcached-libmemcached-perl
Verify that Perl can load the module:
perl -MCache::Memcached::libmemcached -e 'print "Module loaded successfully\n"'
Module loaded successfully
Use Memcached with WordPress on Debian
WordPress can use Memcached for object caching, but WordPress needs a cache plugin or drop-in that speaks to the Memcached server. W3 Total Cache and LiteSpeed Cache can use Memcached for object cache storage when the PHP extension is available. Redis-only plugins are not replacements for this Memcached workflow.
- For W3 Total Cache, enable Object Cache and choose Memcached as the cache method.
- For LiteSpeed Cache, enable Object Cache and set the Memcached host to
127.0.0.1with port11211. - For Memcached-specific drop-ins, confirm the plugin uses PHP’s
Memcachedextension and supports your WordPress and PHP versions before relying on it.
After enabling object caching, browse a few dynamic WordPress pages and check Memcached statistics:
memcstat --servers=127.0.0.1 | grep -E 'curr_items|cmd_get|cmd_set|get_hits'
curr_items: 42 cmd_get: 128 cmd_set: 64 get_hits: 96
If the counters stay at zero after real traffic, WordPress is not sending cache operations to Memcached yet. Review the plugin’s object-cache settings before changing the Debian service.
For full WordPress stack setup, see Install WordPress with Nginx on Debian or Install WordPress with Apache on Debian.
Check Memcached from the Command Line
Memcached does not include a built-in web viewer. Use command-line checks for the daemon itself, and treat application or cache-plugin screens as views into that application’s own cached objects.
The memcstat command is the cleanest monitoring check after the APT install:
memcstat --servers=127.0.0.1 | sed -n '1,20p'
For manual protocol checks, install netcat:
sudo apt install netcat-openbsd
Set, Read, and Delete a Test Memcached Key
This command stores a short key, reads it back, deletes it, and exits:
printf "set testkey 0 60 9\r\ntest data\r\nget testkey\r\ndelete testkey\r\nquit\r\n" | nc -w 3 127.0.0.1 11211
STORED VALUE testkey 0 9 test data END DELETED
List Memcached Keys for Debugging
Key listing is a debugging tool, not a routine production monitoring command. Create a temporary key in one connection before running the LRU crawler metadata command in a separate connection:
printf "set listkey 0 60 9\r\ntest data\r\nquit\r\n" | nc -w 2 127.0.0.1 11211
printf "lru_crawler metadump all\r\n" | nc -w 5 127.0.0.1 11211 | sed -n '1,20p'
printf "delete listkey\r\nquit\r\n" | nc -w 2 127.0.0.1 11211
The metadata dump should include a line beginning with key=listkey before END:
Flush Memcached Cache Entries
flush_allclears cached entries from the Memcached instance. Applications can usually rebuild cache data, but run it only when you intentionally want a cold cache.
printf "flush_all\r\nquit\r\n" | nc -w 2 127.0.0.1 11211
OK
Troubleshoot Memcached on Debian
Memcached Service Fails to Start
Check the unit state and recent logs:
systemctl status memcached --no-pager
journalctl -u memcached -n 30 --no-pager
A bad configuration line usually appears in the journal output. Reopen the configuration, fix the option, and restart:
sudo nano /etc/memcached.conf
sudo systemctl restart memcached
Applications Cannot Connect to Memcached
Confirm the listener address and service state:
systemctl is-active memcached
ss -H -ltn 'sport = :11211'
If Memcached listens only on 127.0.0.1, remote clients cannot connect until you add a private listen address and a matching firewall rule. If the application runs on the same server, leave the binding as localhost and point the client at 127.0.0.1:11211.
Memcached Evicts Items Too Often
Check memory limit and eviction counters:
memcstat --servers=127.0.0.1 | grep -E 'bytes|limit_maxbytes|evictions'
bytes: 10485760 limit_maxbytes: 67108864 evictions: 0
A rising evictions value means Memcached is discarding old objects to stay within the memory limit. Increase -m only after confirming the server has enough free RAM.
PHP Sessions Fail with a Memcached Path Error
If PHP reports a session error that names a Memcached path or host, verify the PHP extension and the target server address:
php -m | grep '^memcached$'
memcstat --servers=127.0.0.1 | sed -n '1,8p'
If php -m does not show memcached, install php-memcached and restart Apache or PHP-FPM. If memcstat works locally but PHP points at another IP address, update the application configuration or allow that private client through the firewall.
Build Memcached from Source on Debian
The source build is an advanced path for users who need the upstream release instead of Debian’s package-maintained version. The example uses Memcached 1.6.42 and the SHA1 checksum published on the official downloads page. Check Memcached downloads before running the commands, and update the version and checksum variables if a newer release is listed.
Do not run the APT package and a source-built service under the same
memcached.servicename. Remove the APT package first or use a separate custom service name.
If you already installed the Debian package while testing the APT method, stop and remove that daemon before creating the source-built service:
if systemctl list-unit-files memcached.service --no-legend | grep -q '^memcached.service'; then
sudo systemctl stop memcached
fi
sudo apt remove --purge memcached
Install Memcached Source Build Dependencies
Install the compiler, libevent headers, downloader, and certificate bundle:
sudo apt install build-essential libevent-dev wget ca-certificates
Download and Verify Memcached Source
Set the release variables in the same terminal session:
MEMCACHED_VERSION="1.6.42"
MEMCACHED_SHA1="de453f58745238c70091fe243549c406aabdc3c5"
BUILD_DIR="$HOME/memcached-source-build"
Prepare a clean build directory. The first command deletes any previous Memcached source build directory at that exact path. Keeping the source under BUILD_DIR makes later updates and cleanup target one known path:
rm -rf -- "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR" || {
echo "Cannot enter $BUILD_DIR"
false
}
Download the release tarball:
wget "https://memcached.org/files/memcached-${MEMCACHED_VERSION}.tar.gz" -O "memcached-${MEMCACHED_VERSION}.tar.gz"
Verify the saved file against the checksum from the official downloads page:
printf '%s %s\n' "$MEMCACHED_SHA1" "memcached-${MEMCACHED_VERSION}.tar.gz" | sha1sum -c -
memcached-1.6.42.tar.gz: OK
Extract the source and enter the build directory:
tar -xzf "memcached-${MEMCACHED_VERSION}.tar.gz"
cd "memcached-${MEMCACHED_VERSION}" || {
echo "Cannot enter memcached-${MEMCACHED_VERSION}"
false
}
Compile Memcached Source on Debian
Configure and build Memcached:
./configure --prefix=/usr/local
make -j"$(nproc)"
./memcached -V
memcached 1.6.42
The upstream test suite is useful for maintainers but can take several minutes on small VMs. For routine server installs, the version check confirms the compiled binary was produced. Run make test only if you are prepared for the longer validation time.
Install the compiled binary:
sudo make install
sudo ldconfig
Create a Source-Built Memcached Service
Create the service account if it does not already exist:
getent passwd memcache || sudo useradd -r -s /usr/sbin/nologin -U -M memcache
Create an environment file for common runtime settings:
printf '%s\n' \
'MEMCACHED_MEMORY=64' \
'MEMCACHED_PORT=11211' \
'MEMCACHED_LISTEN=127.0.0.1' \
'MEMCACHED_OPTIONS=' | sudo tee /etc/memcached-local.conf > /dev/null
Create the systemd unit:
sudo tee /etc/systemd/system/memcached.service > /dev/null <<'EOF'
[Unit]
Description=Memcached Service (Source Build)
After=network.target
[Service]
Type=simple
User=memcache
Group=memcache
EnvironmentFile=/etc/memcached-local.conf
ExecStart=/usr/local/bin/memcached -m ${MEMCACHED_MEMORY} -p ${MEMCACHED_PORT} -u memcache -l ${MEMCACHED_LISTEN} -U 0 $MEMCACHED_OPTIONS
Restart=on-failure
PrivateTmp=true
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
EOF
Reload systemd and start the source-built service:
sudo systemd-analyze verify /etc/systemd/system/memcached.service
sudo systemctl daemon-reload
sudo systemctl enable memcached --now
Confirm that the service is active and listening on localhost:
systemctl is-active memcached
ss -H -ltn 'sport = :11211'
active LISTEN 0 1024 127.0.0.1:11211 0.0.0.0:*
Update Source-Built Memcached on Debian
APT updates the Debian package automatically during normal package maintenance. Source-built installs need a repeatable update command. Create this helper script and update the version and checksum variables from the official downloads page before each source upgrade. The setup block refuses to overwrite an unrelated existing command at /usr/local/bin/update-memcached-source.
HELPER_PATH="/usr/local/bin/update-memcached-source"
if [ -e "$HELPER_PATH" ] && ! sudo grep -q 'LinuxCapable Memcached source updater' "$HELPER_PATH"; then
echo "$HELPER_PATH already exists and was not created by this article."
else
sudo tee "$HELPER_PATH" > /dev/null <<'EOF'
#!/bin/bash
# LinuxCapable Memcached source updater
set -euo pipefail
MEMCACHED_VERSION="1.6.42"
MEMCACHED_SHA1="de453f58745238c70091fe243549c406aabdc3c5"
WORKDIR="${HOME}/memcached-source-build"
ARCHIVE="memcached-${MEMCACHED_VERSION}.tar.gz"
for cmd in grep wget tar make gcc sha1sum sudo systemctl nproc ss; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Missing required command: $cmd"
exit 1
fi
done
if ! sudo sh -c 'command -v ldconfig >/dev/null 2>&1'; then
echo "Missing required root command: ldconfig"
exit 1
fi
INSTALLED_PATH=""
INSTALLED_VERSION=""
if command -v memcached >/dev/null 2>&1; then
INSTALLED_PATH="$(command -v memcached)"
INSTALLED_VERSION="$(memcached -V 2>&1 || true)"
fi
if [ "$INSTALLED_PATH" = "/usr/local/bin/memcached" ] && [ "$INSTALLED_VERSION" = "memcached ${MEMCACHED_VERSION}" ]; then
echo "Memcached ${MEMCACHED_VERSION} is already installed."
exit 0
fi
if [ -d "$WORKDIR" ]; then
sudo rm -rf -- "$WORKDIR"
fi
mkdir -p "$WORKDIR"
cd "$WORKDIR" || {
echo "Cannot enter $WORKDIR"
exit 1
}
wget "https://memcached.org/files/${ARCHIVE}" -O "$ARCHIVE"
printf '%s %s\n' "$MEMCACHED_SHA1" "$ARCHIVE" | sha1sum -c -
tar -xzf "$ARCHIVE"
cd "memcached-${MEMCACHED_VERSION}" || {
echo "Cannot enter memcached-${MEMCACHED_VERSION}"
exit 1
}
./configure --prefix=/usr/local
make -j"$(nproc)"
./memcached -V
sudo systemctl stop memcached
sudo make install
sudo ldconfig
sudo systemctl start memcached
memcached -V
systemctl is-active memcached
ss -H -ltn 'sport = :11211'
EOF
sudo chmod +x "$HELPER_PATH"
hash -r
command -v update-memcached-source
fi
Successful setup prints the helper path:
/usr/local/bin/update-memcached-source
Run the updater manually so you can see build or checksum failures before the service changes. If the installed source-built version already matches the helper variables, the helper exits without rebuilding:
update-memcached-source
Memcached 1.6.42 is already installed.
Remove Memcached from Debian
Use the removal path that matches the installation method you used.
Remove APT-Installed Memcached
Stop the service and remove the Debian packages:
sudo systemctl stop memcached
sudo apt remove --purge memcached libmemcached-tools
Confirm that APT no longer has installed or residual Memcached package records:
dpkg-query -W -f='${db:Status-Abbrev} ${binary:Package}\n' memcached libmemcached-tools 2>/dev/null | grep -E '^(ii|rc)' || echo "Memcached packages purged"
Memcached packages purged
Clear any cached shell path and confirm the daemon command is gone:
hash -r
command -v memcached || echo "memcached command removed"
memcached command removed
Review any orphaned packages before removing them. This matters on long-lived systems because APT may list old kernels or unrelated desktop helpers alongside Memcached dependencies:
sudo apt autoremove --dry-run
If the preview contains only packages you intend to remove, run:
sudo apt autoremove
Remove Source-Built Memcached
The following commands remove the source-built binary, man page, headers, custom systemd unit, environment file, helper script, and local build directory. Memcached cache entries live in RAM, so stopping the service already discards cached data.
Stop and disable the custom service first:
sudo systemctl stop memcached
sudo systemctl disable memcached
Remove the files installed by make install. When the source tree still exists, use its uninstall target. If the source tree is gone, remove the fallback paths only when the active binary is the documented source-built path:
SOURCE_DIR=""
if [ -d "$HOME/memcached-source-build" ]; then
SOURCE_DIR="$(find "$HOME/memcached-source-build" -maxdepth 1 -type d -name 'memcached-*' | sort | tail -n 1)"
fi
if [ -n "$SOURCE_DIR" ] && [ -f "$SOURCE_DIR/Makefile" ]; then
(cd "$SOURCE_DIR" && sudo make uninstall)
elif [ "$(command -v memcached 2>/dev/null || true)" = "/usr/local/bin/memcached" ]; then
sudo rm -f /usr/local/bin/memcached
sudo rm -f /usr/local/share/man/man1/memcached.1 /usr/local/share/man/man1/memcached.1.gz
sudo rm -rf /usr/local/include/memcached
else
echo "No source-built /usr/local/bin/memcached binary found"
fi
Review the custom service, helper, and build paths before deleting them:
for path in /etc/systemd/system/memcached.service /etc/memcached-local.conf /usr/local/bin/update-memcached-source "$HOME/memcached-source-build"; do
if [ -e "$path" ]; then
ls -ld "$path"
fi
done
Remove the custom service files, article-owned update helper, and build directory:
sudo rm -f /etc/systemd/system/memcached.service
sudo rm -f /etc/memcached-local.conf
if [ -f /usr/local/bin/update-memcached-source ] && sudo grep -q 'LinuxCapable Memcached source updater' /usr/local/bin/update-memcached-source; then
sudo rm -f /usr/local/bin/update-memcached-source
fi
if cd "$HOME"; then
sudo rm -rf -- "$HOME/memcached-source-build"
else
echo "Cannot change to $HOME; build directory not removed"
fi
sudo systemctl daemon-reload
Remove the dedicated service account only if no other Memcached package or custom unit uses it:
if ! dpkg-query -W -f='${db:Status-Abbrev}\n' memcached 2>/dev/null | grep -q '^ii' && ! systemctl list-unit-files '*memcached*.service' --no-legend | grep -q .; then
sudo userdel memcache 2>/dev/null || true
else
echo "Memcache account still has package or service references; leaving it in place"
fi
Verify removal:
hash -r
command -v memcached || echo "memcached command removed"
memcached command removed
Conclusion
Memcached is running on Debian with a local-only listener, systemd service management, and optional client libraries for PHP, Python, Perl, and WordPress. Keep APT installs on the Debian package path unless you have a clear source-build requirement, and restrict any remote cache access to trusted private hosts.


Formatting tips for your comment
You can use basic HTML to format your comment. Useful tags currently allowed in published comments:
<code>command</code>command<strong>bold</strong><em>italic</em><blockquote>quote</blockquote>