How to Install Memcached on Ubuntu 20.04

Install Memcached on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure Memcached on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Memcached is a free and open source in-memory caching system that speeds up web applications by caching large volumes of data in memory that are generated from page load requests or API calls. Memcached is particularly useful in speeding up PHP-based web applications such as WordPress, Joomla, Drupal, and many more.

Install Memcached on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install Memcached.

Memcached is available on official Ubuntu 20.04 repository, install Memcached alongside other dependencies and packages using following command:

sudo apt install memcached libmemcached-tools

Once the installation is completed, run the following command to start the Memcached server:

sudo systemctl start memcached
sudo systemctl enable memcached

Step 3. Configure Memcached.

Before configuring Memcached, you need to ensure that it is listening to the local interface 127.0.0.1. For default level configuration, we will check for the following settings under the Memcached configuration file found in /etc/memcached.conf:

sudo nano /etc/memcached.conf

Set or update parameters as follows, customizing them to your needs:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1"

This is a brief description of the parameters:

**PORT**: The default port used by Memcached to run.
**USER**: The user Memcached runs as.
**MAXCONN**: The maximum number of allowed connections to Memcached. You can increase to any number based on your requirements.
**CACHESIZE**: The cache size for memory.
**OPTIONS**: Set IP address of server, so that Apache or Nginx web servers can connect to it.

Restart the Memcached service for the changes to take effect:

sudo systemctl restart memcached

Step 4. Configure Firewall.

With the Firewall running, we can finally set up the firewall rules:

sudo ufw allow 11211/tcp
sudo ufw reload

Step 5. Install the Memcached PHP Extension.

To use Memcached as a caching database for your PHP application such as WordPress or Joomla, you need to install the php-memcached extension:

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

Now, restart Apache to apply the changes:

sudo systemctl restart apache2

That’s all you need to do to install Memcached on Ubuntu 20.04 Focal Fossa. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.