How to Install Redis on Ubuntu + Configuration and Common Redis Commands

How to Install Redis on Ubuntu + Configuration and Common Redis Commands

Developers often look for systems that would increase the speed and performance of their projects. One popular system like that is Redis. It is an open-source, in-memory database used as a cache and message broker. It is also known as a data structure server.

What makes it unique compared to relational database systems is the ability to store high-level data types, such as maps, lists, and sets. It also offers an easy-to-use interface, atomic manipulation of data, and exceptional performance.

In this tutorial, we’ll go over the Redis installation process on Ubuntu 18.04, 20.04, and 22.04, as well as any configuration required to make it run properly.

What Makes Redis Useful?

Performance and exceptional features make Redis a better system than traditional databases. The most popular use-cases for Redis are as follows:

  • Caching – Redis’s enhanced capability to persist the data to a disk makes it a superior alternative to traditional caching solutions.
  • Queuing – the system can be used for queuing jobs in the background.
  • Countering – Redis allows the simple creation and implementation of counters without the need to read data or update the database. The counters in Redis will remain consistent.
  • Publishing and subscribing – users can easily distribute data using the Pub/Sub paradigm.

How to Install Redis on Ubuntu in 4 Steps

In order to install and configure the Redis server on Ubuntu, you will need to have an already running virtual private server (VPS) with Ubuntu operating system installed. With all the prerequisites done, connect to SSH and start the installation.

Step 1 – Update APT Repository

Redis is already included in the official package repository of Ubuntu. Nevertheless, we recommend to frequently update the APT repository to get the latest version possible.

sudo apt-get update

Step 2 – Install Redis Server on Ubuntu Using the APT Command

Installing Redis is as simple as using the following command with the sudo privileges:

sudo apt install redis

Press “y” and then hit Enter to continue.

Step 3 – Check the Redis Version

Once Redis is installed, you can check if the installation was successful by using this command:

redis-cli --version

The output will display the version of the Redis server currently installed on your machine.

Step 4 – Start Redis Service

Once the installation is complete, we recommend checking if the Redis instance is running. In order to test connectivity, you can use the following command:

sudo systemctl status redis

In the output, locate “Active: active (running)” line.

Command-prompt window displaying Redis server status on an Ubuntu system. Red border indicates the active status

If Redis hasn’t been started and the status is inactive, you can enable Redis client by entering the following command:

Pro Tip

If Redis is already running and you want to stop the process, you can use the sudo systemctl stop redis command. After this, you will see “Active: inactive (dead)” in the status command output. If you need an option to restart the Redis service, use the sudo systemctl restart redis-server command.

How to Find and Edit Redis Configuration File on Ubuntu

The default Redis configuration file is located in /etc/redis/redis.conf directory. The default configuration is when the Redis server listens for all available connections.

You can make it listen to the interfaces of your choice. This can be done by using the bind configuration directive that would be followed by a single or multiple IP addresses.

To instruct the Redis server to listen for a particular IP address, you need to edit the /etc/redis/redis.conf file. Open it with your preferred editor. In our case, we are using the nano editor.

sudo nano /etc/redis/redis.conf

Locate the line “bind 127.0.0.1 ::1”.

Change the IP address by entering the values of the connections you want the Redis server to listen for. Here’s an example:

bind 70.25.220.238

In order to add multiple IP addresses, simply separate the IP addresses with a space like so:

bind 70.25.220.238 70.25.220.239

Here you need to enter the IP addresses of your network.

However, if you want the server to listen to all the interfaces on the network, you can comment out the bind line entirely:

redis.conf file opened in the nano editor. Lines indicating settings on which connections Redis should listen to

Once you are done making changes, save and close the file. Then restart the Redis service to apply the changes:

sudo systemctl restart redis-server

Using Redis Commands

There are several different groups of commands in Redis which include:

  • String commands
  • List commands
  • Set commands
  • Hash commands
  • Sorted set commands
  • Pub/Sub commands

Here we listed some of the commands used in the Redis prompt:

  • Redis-server /path/redis.conf – this Redis config command starts Redis with the particular configuration file.
  • Redis-cli – a command to run Redis CLI client
  • APPEND key value – append a value to a key.
  • BITCOUNT key [start end] – sets the number of set bits in a string.
  • SET key value – set a value in a key.
  • EXPIRE key 120 – expire a key in 120 seconds.
  • INCR key – increment the value in a key.
  • KEYS pattern – finds all keys matching a particular pattern.
  • DEL key – deletes a key.
  • STRLEN key – get the length of a key.
  • MSET key value [key value …] – set multiple keys and value pairs.
  • MGET key [key …] – get values from multiple keys.
  • GETSET key value – sets new value while returning the old value.
  • INCRBY key increment – increases key count.
  • DECRBY key increment – decrease key count

Renaming Dangerous Commands (Optional)

A common practice for securing Redis is renaming commands or disabling any possibly unsafe ones. Such commands are hazardous because any unauthorized users can use them and manipulate or even destroy all of the database data.

Keep in mind that this process is entirely optional, and you can decide whether you want to rename, disable or leave the command active. In order to begin, open the /etc/redis/redis.conf file with your preferred editor. We will be using nano in this example:

sudo nano /etc/redis/redis.conf

Then, find the SECURITY section, where you can either rename or disable a command. In our example, we are renaming FLUSHALL, SHUTDOWN, DEL commands to CANTSEE_FLUSHALL, CANTGUESS_SHUTDOWN, CANTHEAR_DEL

We are also disabling DEBUG and CONFIG commands entirely:

redis.conf file SECURITY section. FLUSHALL, SHUTDOWN, and DEL commands are the ones to be renamed

Other possibly unsafe commands include RENAME, SAVE, SREM, FLUSHDB, PEXPIRE, and BGSAVE.

In order to test everything, restart the Redis service:

sudo systemctl restart redis.service

Then, log in to the Redis command-line client:

redis-cli

To test out a disabled command, you just need to try it. For example, testing out the DEBUG command should look like this:

Redis CLI tool showcasing the use of the DEBUG command. In our example, it gives out an error because this command was disabled

An error will be shown because DEBUG command is disabled completely. Next, test out the renamed command. In our case, it’s FLUSHALL.

Redis client showcasing FLUSHALL command. In our example, it was renamed to CANTSEE_FLUSHALL

As you can see, FLUSHALL command doesn’t work, while our renamed one CANTSEE_FLUSHALL is working perfectly.

Conclusion

Redis database server is one of the most popular alternatives to relational databases such as MySQL. With Redis, users can expect high performance and speed, especially for high-traffic websites.

In this tutorial, we’ve covered Redis’s main aspects and why it is so valuable. We’ve also reviewed the installation process on Ubuntu 18.04, 20.04, and 22.04 systems. Lastly, we’ve shown a couple of helpful Redis commands and how to secure Redis by renaming unsafe commands.

We hope that you found this tutorial useful. If you have any questions or insights, leave them in the comments section below.

Author
The author

Ignas R.

Ignas is a Technical Content Writer at Hostinger. After helping people tackle even the most complex technical issues as a Customer Success Specialist, his current goal is to write easy-to-follow articles so that these issues will not happen at all. During his free time, Ignas likes to play video games and fix up things around his house.