Best Memcached Installation Steps on Debian 12 – OrcaCore
This tutorial provides a comprehensive guide to the Best Memcached Installation Steps on Debian 12 Bookworm. Memcached is a powerful, open-source, distributed memory object caching system. Its primary function is to accelerate dynamic web applications by alleviating database load. By caching frequently accessed data in RAM, Memcached drastically reduces the number of database queries, leading to improved performance and responsiveness. Follow these Best Memcached Installation Steps on Debian 12, outlined below and accessible on the Orcacore website, to harness the power of Memcached.
Before embarking on the Best Memcached Installation Steps on Debian 12, ensure you have the necessary prerequisites. This includes server access with a non-root user account that has sudo privileges, and a properly configured basic firewall. A detailed guide for setting this up can be found on Orcacore: Initial Server Setup with Debian 12 Bookworm.
Now, let’s proceed with the steps to complete the Best Memcached Installation Steps on Debian 12.
Step 1 – Command To Install Memcached on Debian 12
The Memcached package is readily available in the default Debian 12 repository, simplifying the installation process. Begin by updating your system’s package list to ensure you have the latest versions. Execute the following command:
sudo apt update && sudo apt upgrade -y
Next, install Memcached and the accompanying libmemcached-tools
package, which provides command-line utilities for managing and monitoring your Memcached server. Use the following command:
sudo apt install memcached libmemcached-tools -y
To confirm the successful installation of Memcached, you can use the apt-cache policy
command. This will display information about the installed Memcached package, including its version and origin.
sudo apt-cache policy memcached
**Output**
memcached:
Installed: 1.6.18-1
Candidate: 1.6.18-1
Version table:
*** 1.6.18-1 500
500 https://ftp.debian.org/debian bookworm/main amd64 Packages
100 /var/lib/dpkg/status
Step 2 – Verify Memcached Service Status on Debian 12
Upon installation, Memcached is typically enabled and actively running. To verify its status, use the systemctl status
command. This command provides detailed information about the Memcached service, including its current state, uptime, and resource usage.
sudo systemctl status memcached
**Output**
● memcached.service - memcached daemon
Loaded: loaded (/lib/systemd/system/memcached.service; enabled; preset: en>
Active: **active** (**running**) since Tue 2023-09-19 08:43:37 EDT; 3min 33s ago
Docs: man:memcached(1)
Main PID: 1115 (memcached)
Tasks: 10 (limit: 4653)
Memory: 6.1M
CPU: 106ms
CGroup: /system.slice/memcached.service
...
If Memcached is not running or enabled, you can start and enable it using the following command. This ensures that Memcached starts automatically upon system boot.
sudo systemctl enable --now memcached
Memcached, by default, listens on port 11211. To confirm this, you can use the ps
command in conjunction with grep
to filter for processes related to Memcached.
ps -ef | grep memcached
**Output**
memcache 1115 1 0 08:43 ? 00:00:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid
root 1246 593 0 08:49 pts/0 00:00:00 grep memcached
Step 3 – Memcached Configuration on Debian 12
After completing the initial Best Memcached Installation Steps on Debian 12, you can further optimize Memcached’s performance by adjusting its configuration file.
Open the Memcached configuration file using your preferred text editor. In this example, we use the vi editor:
sudo vi /etc/memcached.conf
Locate the line containing the -l
parameter. This parameter specifies the IP address on which Memcached listens for connections. By default, it is set to 127.0.0.1
, which means Memcached only accepts connections from the local machine. You can either leave it as default or change it to an internal IP address if you need to access Memcached from other machines on your network.
-l 127.0.0.1
It is generally recommended to disable UDP to mitigate potential security risks. To do this, add the following line to the configuration file:
-U 0
Also, find the -m
parameter, which defines the maximum amount of memory (in MB) that Memcached can use. Adjust this value based on your server’s available RAM and the caching requirements of your applications.
-m 2000
Save the changes to the configuration file and exit the editor.
To apply the modifications, restart the Memcached service:
sudo systemctl restart memcached
Step 4 – Configure UFW Firewall for Memcached
If you are using the UFW firewall, you need to configure it to allow traffic on TCP port 11211, which Memcached uses. The specific command will depend on your network setup and access requirements.
To allow connections from any source, use the following command:
sudo ufw allow 11211
To allow connections from a specific IP address, use the following command:
sudo ufw allow proto tcp from <ip address> to any port 11211
For a cluster IP network connection with multiple instances, use the following command (note the subnet rule):
sudo ufw allow proto tcp from <ip address>/24 to any port 11211
Note: Exercise caution when allowing connections from entire subnets. Ensure your internal network is secure and trustworthy.
After adding the necessary rules, reload the firewall to apply the changes:
sudo ufw reload
Step 5 – Memcached PHP Extensions Installation
Memcached offers extensions for various programming languages, with PHP being a common use case. To install the PHP extension for Memcached, use the following command:
sudo apt install php-memcached apache2 libapache2-mod-php php php-cli php-memcached php-memcached
Note: For Apache users, enable the Memcached extension and restart Apache:
# phpenmod memcached
# sudo systemctl restart apache2
Install Additional Libraries
You can install Python or Perl support with the following commands:
-
Python support:
sudo apt install python3-pymemcache
-
Perl support:
sudo apt install libcache-memcached-libmemcached-perl
Step 6 – Access Memcached from Command Line
Following the Best Memcached Installation Steps on Debian 12, you can interact with Memcached directly from the command line using Telnet.
telnet localhost 11211
**Output**
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Once connected, you can retrieve statistics about your Memcached instance using the stats
command:
stats

You can further refine your queries by examining the different Memcached slabs (memory partitions). For example, to list the slabs in the connected instance:
stats slabs
For more in-depth information, consult the Memcached Wikis page.
Conclusion
By following these Best Memcached Installation Steps on Debian 12, you have successfully installed Memcached, configured it for optimal performance, installed necessary language extensions, and learned how to interact with it from the command line.
We hope you found this tutorial helpful. If you require assistance or have any suggestions, please leave a comment below.
You may also be interested in these articles:
Install MySQL in Docker Container on Debian 12
4 Ways To Install OpenCV on Debian 12 Bookworm
Alternative Solutions for Caching in Debian 12
While Memcached is an excellent choice for many caching scenarios, there are alternative solutions that might be more suitable depending on your specific needs. Here are two alternatives: Redis and Varnish.
1. Redis: The Versatile Data Structure Server
Redis (Remote Dictionary Server) is an in-memory data structure store, often used as a cache, database, and message broker. Unlike Memcached’s simple key-value store, Redis supports a wide range of data structures like strings, hashes, lists, sets, and sorted sets. This makes it more versatile for complex caching scenarios where you need to store and manipulate data in more sophisticated ways.
Explanation:
Redis offers persistence options (e.g., snapshots and append-only files) that Memcached lacks, allowing data to survive server restarts. It also supports more advanced features like pub/sub messaging, transactions, and Lua scripting.
Installation Steps:
-
Install Redis:
sudo apt update sudo apt install redis-server
-
Verify Redis Status:
sudo systemctl status redis-server
-
Connect to Redis (using redis-cli):
redis-cli
Code Example (Python using redis-py):
First, install the redis-py library:
pip install redis
Then, use the following Python code:
import redis
# Connect to Redis
r = redis.Redis(host='localhost', port=6379, db=0)
# Set a key-value pair
r.set('mykey', 'Hello Redis!')
# Get the value of a key
value = r.get('mykey')
print(value.decode('utf-8')) # Output: Hello Redis!
This example demonstrates setting and retrieving a string value in Redis. Redis’s ability to handle more complex data structures allows for more nuanced caching strategies. For instance, you could store a list of recently viewed products for each user as a Redis list, providing a personalized caching experience.
2. Varnish: The HTTP Accelerator
Varnish is an HTTP accelerator designed for caching HTTP content. It sits in front of your web server and caches static and dynamic content based on HTTP headers. Varnish is particularly effective for websites with a high volume of traffic and a significant amount of cacheable content.
Explanation:
Varnish excels at caching entire HTTP responses, reducing the load on your web server. It uses a powerful configuration language (VCL) that allows you to define complex caching policies based on request and response headers.
Installation Steps:
-
Install Varnish:
sudo apt update sudo apt install varnish
-
Configure Varnish (e.g., /etc/varnish/default.vcl): This step involves configuring Varnish to point to your backend web server and defining caching rules.
-
Start/Restart Varnish:
sudo systemctl restart varnish
Code Example (VCL Snippet):
backend default {
.host = "127.0.0.1"; # Your web server's IP address
.port = "8080"; # Your web server's port
}
sub vcl_recv {
if (req.url ~ ".(jpg|jpeg|png|gif|css|js)$") {
return (hash); # Cache static files
}
return (pass); # Don't cache dynamic requests
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
return (deliver);
}
This VCL snippet demonstrates a basic Varnish configuration that caches static files (images, CSS, JavaScript) and passes through dynamic requests. The vcl_deliver
subroutine adds an X-Cache
header to responses, indicating whether the content was served from the cache (HIT) or the backend server (MISS). Varnish is highly customizable, allowing you to tailor its caching behavior to your specific website’s needs.