How to Install Redis on Ubuntu 20.04 LTS

Redis is an open-source, in-memory key-value data structure store used as a database, cache, and message broker. Redis supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyper log logs, geospatial indexes, and streams. Redis also provides high availability with Redis Sentinel software logic, automatically partitioning across commodities by ensuring data is replicated in multiple locations. For example, when adding a new node to the cluster, the system will automatically detect the new node and begin replicating data to it. If a node fails or becomes unresponsive, the system automatically removes it from the cluster and routes traffic to the next available node. This way, you can be sure that your data is always accessible. As a result, Redis is an ideal solution for mission-critical applications that require high availability and scalability.

The following tutorial will teach you how to install Redis on Ubuntu 20.04 LTS Focal Fossa using the terminal command line with the official Redis APT repository for users to install the latest version or for existing users to upgrade their Redis version that Ubuntu, which is often outdated along with some basic setup instructions to get you started.

Update Ubuntu

Before installing Redis, start by ensuring your systems’ packages are up-to-date to avoid any conflicts during the installation using the following command.

sudo apt update && sudo apt upgrade

Install Required Packages

To complete the installation, you will need to install the following software packages using the following command in your terminal.

sudo apt install software-properties-common apt-transport-https curl ca-certificates gnupg2 curl -y

Install Redis

Default Ubuntu Repository Method

By default, Redis 5 series comes included in the Ubuntu 20.04 LTS Focal Fossa repository, which can be installed with the following command.


sudo apt install redis

Ubuntu considers this version stable, but it is far behind. In fact, at the time of this tutorial, Redis version 7 was released with many improvements. If you prefer more recent, the following section can also be used for existing Redis installs to upgrade.

See also  How to Install VSCodium on Fedora 37/36/35 APT Repository

The second method is to import the APT repository from the official repository. This is always up-to-date for bug and security fixes and the newest features, which can be handy for users wanting to stick with Ubuntu 20.04 but require or like to use a more updated version of Redis.

First, import the GPG key.

curl -fsSL | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

Now import the repository.

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

Update your sources list to reflect the new repository source.

sudo apt-get update

Now install Redis; you may see an upgrade if you have Redis installed. I would still advise using the install command.


sudo apt install redis -y

Check using the apt-cache policy command to ensure you have installed the version.

apt-cache policy redis

Example output:

How to Install Redis on Ubuntu 20.04 LTS

As above, you installed the latest Redis version.

Now activate and enable on system boot the Redis instance.

sudo systemctl enable redis-server --now

Next, verify the status and make sure Redis is running and, more importantly, with no errors:

systemctl status redis-server

Example output:


How to Install Redis on Ubuntu 20.04 LTS

Note that Reddis actively listens to localhost on the default port 6379. To confirm this type, the following:

ps -ef | grep redis

Example output:

redis       4171       1  0 23:35 ?        00:00:00 /usr/bin/redis-server
joshua      4702    4691  0 23:36 pts/0    00:00:00 grep --color=auto redis

While testing, everything is working and operational, connecting to your Redis service is good, and performing a ping test.

To perform the test, enter the following command:


Once connected, your terminal will display ( Now ping the Redis service as follows:


Example output:

Advertisement> ping

Type the following to exit the Redis instance:


Congratulations, you have installed Redis on your system and verified it is operational.

Next, you can configure Redis.

How to Configure Redis

Redis can be configured in a few ways. The most notable action of why people use Redis is for caching purposes. To do this, you need to open the “/etc/redis/redis.conf” file using nano editor.

sudo nano /etc/redis.conf

Configure Max Memory

Now, add the following to the end of the file. Note that you can change the memory value to whatever you like or, more importantly, optimal for your web application and server hardware.

maxmemory 500mb 
maxmemory-policy allkeys-lru

As you can see, the setting in the guide has 500MB dedicated to Redis as it is on a dedicated host with lots of RAM to spare. Once the 500MB is exhausted, Redis removes any key per the LRU algorithm.


Configure Network Access

Another option is to listen to all services or set an IP address/subnet if you like your Redis service.

See also  How to Install Nginx Mainline on Ubuntu 22.10/22.04/20.04

First, find line 69 in the configuration file.

First, to listen to all network interfaces, Comment “#” the line bind to IP.


# bind ::1

Alternative Method:


Ensure your internal network is trustworthy and has appropriate security controls.


To bind to an IP address, make sure it is a static IP address.



To bind a network subnet.



Note that it is highly suggested to set a password when using a subnet or to access all interfaces to listen to.

Configure Password

Another security feature to further harden Redis is to set a password on the Redis instance.


Navigate to line 507, uncomment the “# requiredpass” line, and set a password.


requiredpass APASSWORD

Make sure this password is robust, numbers, letters, special symbols, and capitals randomized, as Redis servers can be brute forced on a decent box very well.

Next, when invoking the Redis-CLI, use the following command with the password that was set for the user.



“THEPASSWORDSET” is the password that was created.


Users who fail to log in will see the following error message.

(error) NOAUTH Authentication required.

When a user successfully logs in, they will see the following message.


Once done, save your changes CTRL+O then exit CTRL+X. Now restart the Redis service by typing:

sudo systemctl restart redis-server

Configure UFW Rules

If you use the UFW firewall installed by default on your system, you must create allow rules on the TCP port 6379.

First, ensure UFW is installed.

sudo apt install ufw -y

Next, enable UFW.


sudo ufw enable

Some examples are below, depending on your installation and requirements if using singular or in a cluster network.

Additional network IP server instance:

sudo ufw allow proto tcp from <ip address> to any port 6379

Cluster network with many instances:

sudo ufw allow proto tcp from <ip address>/24 to any port 6379

Note that the second UFW rule is a subnet rule; ensure the internal network is secure and trustworthy before allowing it.

See also  How to Upgrade Mesa Drivers on Linux Mint 21 LTS

Now, as you tested at the start of the guide by pinging your Redis service to make sure it was operational, you can try the firewall rules and changes in IP by using the “redis-cli” command:

redis-cli -h  <ip address> ping

If setup correctly, the output should be:



Comments and Conclusion

Redis is a robust open-source data structure store that can be used as a database, cache, and message broker. By using Redis in your applications, you can improve performance and scalability. For more information about managing your Redis installation, visit the Redis documentation page.

Not what you were looking for? Try searching for additional tutorials.

Leave a Comment