Memcached Installation Steps on Debian 12

This tutorial intends to show you the Memcached Installation Steps on Debian 12 Bookworm. Memcached is a free and open-source memory caching software that is used to increase the speed of dynamic web apps. Memcached does this by reducing the database load. You can follow the steps below to install and configure Memcached on Debian 12 Bookworm.

Memcached Installation Steps on Debian 12

Before you start, you must have access to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow this guide on Initial Server Setup with Debian 12 Bookworm.

Now follow the steps below to install Memcached.

Step 1 – Command To Install Memcached on Debian 12

The Memcached package is available in the default Debian 12 repository. First, run the system update with the command below:

sudo apt update && sudo apt upgrade -y

Then, use the following command to install Memcached and the required package:

sudo apt install memcached libmemcached-tools -y

You can verify your Memcached installation by using the command below:

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

By default, Memcached should be enabled and activated on Debian 12 Bookworm during the installation. Verify it by running the command below:

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 it is not activated, you can start and enable the Memcached service on the system boot by running the following command:

sudo systemctl enable --now memcached

Memcached is listening on port 11211. To verify that, run the below command:

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

At this point when you have Memcached installed on your server, you must make some configuration changes to the Memcached configuration file.

Open the file with your favorite text editor, here we use the vi editor:

sudo vi /etc/memcached.conf

Find the following line and check the “-l” parameter that exists. You can leave it as default or you must modify the default IP address from 127.0.0.1 to the internal IP address.

-l 127.0.0.1

It is recommended to disable UDP. To do this, add the following line to the file:

-U 0

Also, find the “-m” parameter and set this to something reasonable for your server:

-m 2000

When you are done, save and close the file.

To apply the changes, restart Memcached on Debian 12:

sudo systemctl restart memcached

Step 4 – Configure UFW Firewall for Memcached

If you have UFW installed, you need to allow the UFW rules on the TCP port 11211. Depending on your installation, you can use the following commands:

sudo ufw allow 11211

Singular IP network connection:

sudo ufw allow proto tcp from <ip address> to any port 11211

Cluster IP network connection with many instances:

sudo ufw allow proto tcp from <ip address>/24 to any port 11211

Note: The second UFW rule is a subnet rule. Note, make sure the internal network is secure and trustworthy before allowing it.

To apply the new rules, reload the firewall:

sudo ufw reload

Step 5 – Memcached PHP Extensions Installation

Memcached comes with various extensions for the programming languages, but it is primarily used for PHP. To install the PHP library, you can run the following command:

sudo apt install php-memcached apache2 libapache2-mod-php php php-cli php-memcached php-memcached

Note: Apache users can execute the following code to enable Memcached on their system:

# phpenmod memcached 
# sudo systemctl restart apache2

Install Additional Libraries

You can install Python and or Perl support by executing 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

At this point, you can use Memcached directly from the command line. To do this, use Telnet in your service:

telnet localhost 11211
Output
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

Next, you can get an overview of your Memcached service by using the following command:

stats
Output
STAT pid 11769
STAT uptime 30
STAT time 1695128489
STAT version 1.6.18
STAT libevent 2.1.12-stable
STAT pointer_size 64
STAT rusage_user 0.056929
STAT rusage_system 0.028309
STAT max_connections 1024
STAT curr_connections 1
STAT total_connections 2
STAT rejected_connections 0
STAT connection_structures 2
STAT response_obj_oom 0
STAT response_obj_count 1
STAT response_obj_bytes 16384
STAT read_buf_count 2
STAT read_buf_bytes 32768
STAT read_buf_bytes_free 0
STAT read_buf_oom 0
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT cmd_meta 0
STAT get_hits 0
STAT get_misses 0
STAT get_expired 0
STAT get_flushed 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
...
END

You can refine the search by looking into the different Memcached slabs (partitions) of memory to return results.

For example, list the slabs in the instance connected:

stats slabs

For more information, you can visit the Memcached Wikis page.

Conclusion

At this point, you have learned the Installation steps of Memcached on Debian 12 Bookworm install the Memcached libraries and access it from the command line.

Hope you enjoy it. If you are looking for any help or if you have an idea, please comment for us.

You may like these articles:

Install MySQL in Docker Container on Debian 12

4 Ways To Install OpenCV on Debian 12 Bookworm

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!