Memcached is a free and open-source memory object caching system that speeds up dynamic web applications by caching data in memory. It is designed for PHP-based web applications such as WordPress, Joomla, Drupal, and Python-based applications. It can increase database performance by temporarily storing information in memory. This will reduce the number of direct requests to databases.

This tutorial will show you how to install Memcached on an Ubuntu 22.04 server.

Requirements

  • A server running Ubuntu 22.04.
  • A root password is configured on your server.

Install Memcached on Ubuntu 22.04

The Memcached package is included in the Ubuntu 22.04 default repository by default. You can install it by running the following command:

apt install memcached libmemcached-tools -y

Once the Memcached is installed, you can verify the Memcached version using the following command:

memcached --version

You should see the following output:

memcached 1.6.14

Manage Memcached Service

The memcached service is managed by systemd. So you can easily manage it with systemctl command.

To start the Memcached service, run the following command:

systemctl start memcached

To enable the Memcached service to start it after the system reboot, run the following command:

systemctl enable memcached

You can also check the status of the Memcached service using the following command:

systemctl status memcached

You should see the following output:

? memcached.service - memcached daemon
     Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-06-17 15:28:04 UTC; 22s ago
       Docs: man:memcached(1)
   Main PID: 17184 (memcached)
      Tasks: 10 (limit: 2292)
     Memory: 1.7M
        CPU: 64ms
     CGroup: /system.slice/memcached.service
             ??17184 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

Jun 17 15:28:04 ubuntu2204 systemd[1]: Started memcached daemon.

By default, Memcached listens on port 11211. You can check it using the following command:

ss -antpl | grep memcache

You should see the Memcached listening port in the following output:

LISTEN 0      1024       127.0.0.1:11211      0.0.0.0:*    users:(("memcached",pid=17184,fd=22))                                                                                                                                                                                                                                                                           

Configure Memcached

Memcached default configuration file is located at /etc/memcached.conf. You can edit it to change the default settings as per your requirements.

nano /etc/memcached.conf

Change the following lines as per your requirement:

## Specify the IP address on which Memcached listens on. 
-l 127.0.0.1

## Disable the UDP

-U 0

## Define the memory to store the cache.

-m 1000

Save and close the file then restart the Memcached service to apply the configuration changes:

systemctl restart memcached

Install Memcached PHP Extensions

If you are using a PHP-based application, then you will need to install some PHP extensions to enable Memcached. You can install all of them by running the following command:

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

Once all the packages are installed then enable the PHP Memcached module with the following command:

phpenmod memcached

Next, restart the Apache service to apply the changes:

service apache2 restart

Add Python and Perl Memcached Support

If you are using Python-based web applications, you must install the Python Memcached library on your server.

apt install python3-pymemcache -y

If you are using Perl-based web applications then you will need to install the Perl Memcached library on your server.

apt install libcache-memcached-libmemcached-perl -y

Access Memcached CLI

Memcached provides a command-line interface to interact with Memcached directly using the command line.

First, run the Telnet command to connect to Memcached:

telnet localhost 11211

You should see the following output:

Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

Next, run the following command to get an overview of your Memcached service:

stats

You should see the following output:

STAT pid 17184
STAT uptime 104
STAT time 1655479786
STAT version 1.6.14
STAT libevent 2.1.12-stable
STAT pointer_size 64
STAT rusage_user 0.036669
STAT rusage_system 0.045970
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

Conclusion

Congratulations! You have successfully installed Memcached on the Ubuntu 22.04 server. You can now use Memcached with your web application to increase its performance. For more useful information, visit the Memcached documentation

<img alt="Hitesh Jethva" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/hitesh-80.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="76" src="data:image/svg xml,” width=”80″>

About Hitesh Jethva

Over 8 years of experience as a Linux system administrator. My skills include a depth knowledge of Redhat/Centos, Ubuntu Nginx and Apache, Mysql, Subversion, Linux, Ubuntu, web hosting, web server, Squid proxy, NFS, FTP, DNS, Samba, LDAP, OpenVPN, Haproxy, Amazon web services, WHMCS, OpenStack Cloud, Postfix Mail Server, Security etc.