How to install Memcached on Amazon Linux 2

Memcached is a widely-used open-source, in-memory caching system that speeds up web applications by reducing the amount of data that must be retrieved from a database. Installing Memcached on Amazon Linux 2 is a straightforward process.

Update the package repository:

Run the following command to update the package repository and ensure that you have the latest available packages installed:

sudo yum update -y

Install Memcached:

Once the package repository has been updated, you can install memcached using the following command:

sudo yum install memcached -y

Start Memcached:

Once memcached is installed, you can start the service with the following command:


sudo systemctl start memcached

Verify Memcached is running:


To verify that Memcached is running, use the following command:

systemctl status memcached

Configure Memcached:


By default, memcached listens on the loopback interface on port 11211 and has a maximum memory allocation of 64 MB. To configure these settings, you can edit the Memcached configuration file, “/etc/sysconfig/memcached”

Enable Memcached on startup:

To ensure that Memcached starts automatically when your Amazon Linux 2 instance starts, use the following command:

sudo systemctl enable memcached

After you have installed and configured Memcached on Amazon Linux 2, you can test the installation by inserting some data into the cache. You can do this by using a client library that is compatible with the memcached protocol, such as libmemcached.

Here’s how you can test memcached by inserting data using the command-line interface:

Install the Memcached command-line tool:
The Memcached command-line tool is not installed by default when you install the Memcached package. To install the tool, run the following command:

sudo yum install memcached-tools -y

Insert data into the cache:
Once you have installed the Memcached command-line tool, you can insert data into the cache by using the following command:

echo "set key 0 60 5" | nc localhost 11211
echo "value" | nc localhost 11211

The first line sets the key with a value of value and an expiration time of 60 seconds. The second line sets the actual value of the key.

Retrieve data from the cache:
You can retrieve data from the cache by using the following command:

echo "get key" | nc localhost 11211

This command retrieves the value of the key from the cache. If the key exists and has not expired, the value of the key will be returned.

Delete data from the cache:
To delete data from the cache, use the following command:

echo "delete key" | nc localhost 11211

This command deletes the key and its associated value from the cache.

By testing the insertion, retrieval, and deletion of data, you can confirm that Memcached is working properly on your Amazon Linux 2 instance.

That’s it! You have successfully installed and configured Memcached on Amazon Linux 2. You can now use Memcached to cache data from your web applications and improve their performance.

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

This Post Has One Comment

  1. Swarup ghosh

    Hi Atiqur,

    I am unable to get memcached run on php cli but not php web server , memcached is not working in php ZTS (thread safe) for web

Leave a Reply