Installing Redis on Ubuntu: A Step-by-Step Guide

Redis is a powerful in-memory data structure store that can be used as a cache, database, and message broker. In this guide, we will walk you through the steps to install Redis on an Ubuntu server. Whether you’re setting up Redis for the first time or need a refresher, this tutorial will help you get Redis up and running quickly.

Prerequisites

Before you begin, ensure that you have:

  • A server running Ubuntu 20.04 or later
  • A user with sudo privileges

Step 1: Update Your Package Index

First, update your package index to ensure you have the latest information about available packages:

sudo apt-get update

Step 2: Install Redis

Install Redis by running the following command:

sudo apt-get install redis-server

This command will install Redis along with any necessary dependencies.

Step 3: Configure Redis

Once Redis is installed, you’ll want to make some basic configuration changes to optimize its performance. The Redis configuration file is located at /etc/redis/redis.conf.

Enable Persistence

To ensure data persistence, you can configure Redis to save snapshots of your data to disk periodically. To do this, open the configuration file:

sudo nano /etc/redis/redis.conf

Find the following lines and adjust them as needed:

save 900 1
save 300 10
save 60 10000

These lines specify the frequency of saving data to disk.

Set Maximum Memory Usage

If you’re using Redis primarily as a cache, you may want to set a maximum memory limit. To do this, locate the maxmemory directive in the configuration file and set it to a suitable value:

maxmemory 256mb
maxmemory-policy allkeys-lru

This configuration tells Redis to use up to 256MB of memory and to evict the least recently used keys when the limit is reached.

Save and close the file when you’re done.

Step 4: Start and Enable Redis

Start the Redis service and enable it to start on boot:

sudo systemctl start redis
sudo systemctl enable redis

Step 5: Verify Redis Installation

To verify that Redis is working correctly, use the redis-cli command to ping the server:

redis-cli ping

If everything is working, Redis will respond with PONG.

You can also check the status of the Redis service:

sudo systemctl status redis

This command will display the current status of the Redis service.

Conclusion

You have successfully installed and configured Redis on your Ubuntu server. Redis is now ready to be used for caching, session management, or any other purpose you need. Be sure to tune the configuration settings according to your specific use case to get the best performance.

For more tips on using Redis with Magento 2, check out our other blog on Redis and Magento 2.

Updated: