Install Memcached on Ubuntu 22.04

In this guide, we want to teach you to Install and Configure Memcached on Ubuntu 22.04.

Memcached is pronounced as mem-cash-dee or mem-cached. It is a free, open-source, high-performance, distributed memory object caching system. Memcached is used to speed up dynamic web applications by reducing the database load. It is used by all the major websites having huge data, for example, YouTube, Wikipedia, Twitter, etc.

Memcached is used in memory caching software because it is very easy to install on any Windows or Unix system. It offers API integration for all the major languages like PHP, Java, C/C++, Python, Ruby, Perl, etc.

It stores the data based on key values for small arbitrary strings or objects including:

  • Results of database calls
  • API calls
  • Page rendering

Steps To Install and Configure Memcached on Ubuntu 22.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can check our guide the Initial Server Setup with Ubuntu 22.04.

Install Memcached on Ubuntu 22.04

First, you need to update and upgrade your system with the following command:

sudo apt update && sudo apt upgrade -y

By default, Memcached packages are available in the default Ubuntu repository. So use the following command to install Memcached:

sudo apt install memcached libmemcached-tools

Next, verify Memcached was installed correctly on your server by using the command below:

sudo apt-cache policy memcached

You should get the following output:

Output
memcached:
  Installed: 1.6.14-1
  Candidate: 1.6.14-1
  Version table:
 *** 1.6.14-1 500
        500 http://de.archive.ubuntu.com/ubuntu jammy/main amd64 Packages
        100 /var/lib/dpkg/status

By default, Memcached should be activated on Ubuntu 22.04. Verify it by running the command below:

sudo systemctl status memcached
Output
● memcached.service - memcached daemon
     Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor prese>
     Active: active (running) since Wed 2023-01-25 16:54:25 UTC; 52s ago
       Docs: man:memcached(1)
   Main PID: 1715 (memcached)
      Tasks: 10 (limit: 4575)
     Memory: 1.7M
        CPU: 103ms
     CGroup: /system.slice/memcached.service
...

To enable the Memcached service on system boot, run the following command:

sudo systemctl enable memcached

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

ps -ef | grep memcached
Output
memcache    1715       1  0 16:54 ?        00:00:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid
root        1977    1100  0 16:56 pts/0    00:00:00 grep --color=auto memcached

Configure Memcached on Ubuntu 22.04

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 vi editor:

sudo vi /etc/memcached.conf

Find the following line and check the “-l” parameter that exists. Leave as the default unless you have internal IP on a local network or external IP from outside; you must modify the default IP address from 127.0.0.1 to the new IP address.

-l 127.0.0.1

It is recommended to disable UDP. Unless you require this function to be enabled, add the following line to disable it:

-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 Ubuntu 22.04:

sudo systemctl restart memcached

Configure Firewall for Memcached

If you have UFW installed, you need to create UFW-allow rules on the TCP port 11211. Depending on your installation and requirements if using singular or in a cluster network, some examples are below:

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

Install PHP Extention For Memcached

Memcached comes with various extensions for the programming languages, but it is primarily used for PHP. To install the PHP library enter 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 service apache2 restart

Install Additional Libraries on Ubuntu 22.04

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

Access Memcached from CLI

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 3012
STAT uptime 199
STAT time 1674666266
STAT version 1.6.14
STAT libevent 2.1.12-stable
STAT pointer_size 64
STAT rusage_user 0.074519
STAT rusage_system 0.068292
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
...
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 to Install and Configure Memcached on Ubuntu 22.04.

Hope you enjoy it.

You may be interested in these articles:

Install and Configure XWiki on Ubuntu 22.04

Install Joomla on Ubuntu 22.04

How To Install Wireshark on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!