How to install Redis on Ubuntu Linux

Redis is open source software used as a database and cache that sits in memory, allowing for exceptional performance. When you’re ready to give this lightning fast program a try, the developers recommend installing Redis on a Linux system, and what better candidate than Ubuntu Linux?

In this tutorial, we’ll guide you through the step by step instructions of installing Redis (both server and client) on Ubuntu. Then, we’ll verify that it’s connectable and configure the UFW firewall to allow incoming connections.

In this tutorial you will learn:

  • How to install Redis Server and Client on Ubuntu Linux
  • How to perform a connection test and configure UFW to allow Redis

Redis on Ubuntu

Redis on Ubuntu
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux
Software Redis
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install Redis Client on Ubuntu

The first thing we need to do is install Redis by opening a command line terminal and typing the following command.

If you are only using your machine to connect to Redis (hosted elsewhere), you’ll only need to install the Redis client. Use this command:

$ sudo apt install redis-tools


Once it’s been installed, you’ll be able to use the redis-cli command to open a Redis terminal to a remote server. For example, this would be the command used to connect to a Redis server with hostname redis-ubuntu. Notice we also use the ping command to verify connectivity.

$ redis-cli -h redis-ubuntu
redis-ubuntu:6379> ping
PONG
redis-ubuntu:6379>
Ping Redis on Ubuntu

Ping Redis on Ubuntu

If the Redis server isn’t using the default port, you can specify a port in your redis-cli command with the -p option, like so:

$ redis-cli -h redis-ubuntu -p 1234

In case you’re getting a “connection refused” error message, we’ll give you some troubleshooting tips further into this article.

Could not connect to Redis at redis-ubuntu:6379: Connection refused

Install Redis Server on Ubuntu

If you’re planning to host a Redis server, you’ll need the server package. This will also automatically install the Redis client package. Use this command in terminal:

$ sudo apt install redis-server

You can verify that Redis is installed on a system and check the installed version with the following command:

$ redis-server -v
Redis server v=5.0.7 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=636cde3b5c7a3923

Furthermore, you can use the ss command to confirm that Redis is listening for incoming connection on its default port of 6379:


$ ss -nlt
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process      
LISTEN      0           5                    127.0.0.1:631                 0.0.0.0:*                     
LISTEN      0           511                  127.0.0.1:6379                0.0.0.0:*                     
LISTEN      0           4096             127.0.0.53%lo:53                  0.0.0.0:*                     
LISTEN      0           5                        [::1]:631                    [::]:*                     
LISTEN      0           511                      [::1]:6379                   [::]:*

By default, the Redis server will start automatically when your system is rebooted. You can change this behavior by using systemd’s systemctl command. You can also use it to check the current status of Redis.

$ sudo systemctl disable redis-server	#disable Redis from starting up automatically
$ sudo systemctl enable redis-server	#enable Redis to start up automatically
$ systemctl status redis-server			#check the current status of Redis server


By default, the Redis server will only listen on local loopback interface 127.0.0.1, meaning that it doesn’t accept remote connections. You can configure Redis to listen on a different network interface, or all network interfaces, by opening the Redis conf file with nano or your favorite text editor:

$ sudo nano /etc/redis/redis.conf

To let Redis listen on all network interfaces, just comment the following line by inserting a preceding #:

bind 127.0.0.1 ::1
Comment this line for Redis to listen on all interfaces

Comment this line for Redis to listen on all interfaces

There’s one other line we’ll need to change if we want Redis to accept remote connections. Find the protected-mode part of the config file and change it to this:

FROM:
protected-mode yes
TO:
protected-mode no
Turning off protected mode

Turning off protected mode

Save your changes to this file and close it. Be sure to restart Redis for the changes to take effect:

$ sudo systemctl restart redis-server


You should now see that Redis is listening on 0.0.0.0, which represents all network interfaces.


$ ss -nlt
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process      
LISTEN      0           5                    127.0.0.1:631                 0.0.0.0:*                     
LISTEN      0           511                    0.0.0.0:6379                0.0.0.0:*                     
LISTEN      0           4096             127.0.0.53%lo:53                  0.0.0.0:*                     
LISTEN      0           5                        [::1]:631                    [::]:*                     
LISTEN      0           511                       [::]:6379                   [::]:*

The last thing you may need to do in order to accept incoming connections is to allow port 6379 through UFW firewall.

$ sudo ufw allow from any to any port 6379 proto tcp
Rules updated
Rules updated (v6)

The Redis server should now accept incoming connections.

Conclusion

In this guide, we learned how to install Redis client and server on Ubuntu Linux. We also saw how to configure Redis server to listen for incoming connections on all network interfaces, as well as how to make a firewall exception for Redis in UFW. You should now be able to host Redis for remote clients, or use the Redis client to connect to other servers.