Objective

The objective is to Install Redis server or client on Ubuntu 18.04 Bionic Beaver

Operating System and Software Versions

  • Operating System: - Ubuntu 18.04 Bionic Beaver
  • Software: - Redis 4.0.8 or higher

Requirements

Privileged access to your Ubuntu System as root or via sudo command is required.

Difficulty

EASY

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

Instructions

Install Redis Client on Ubuntu

In case that you only need to connect to a remote Redis server you only need to install redis-cli client on your local Ubuntu host. To do so execute:
$ sudo apt install redis-tools
Once the installation of Redis client is completed you can use redis-cli command to remotely connect to Redis server.

For example the following linux command will connect to Redis server via hostname redis-ubuntu. Optionally suffix the bellow command with -p PORT-NUMBER to connect to a specific Redis server port:
$ redis-cli -h redis-ubuntu 
redis-ubuntu:6379> ping
PONG
redis-ubuntu:6379>
See below on how to configure Redis server to accept remote client connections in case you are getting the error message:
Could not connect to Redis at redis-ubuntu:6379: Connection refused

Install Redis Server on Ubuntu

In this section we will be installing Redis server on Ubuntu 18.04 Linux. The installation is as simple as:
$ sudo apt install redis
Once the Redis server installation is finished you can check the Redis server version:
$ redis-server -v
Redis server v=4.0.8 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=2d97cb0719f78c3e
Furthermore, confirm that Redis server is up and running as expected by checking for its listening socket on port number 6379:
$ ss -nlt
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN      0      128       0.0.0.0:22                    0.0.0.0:*
LISTEN      0      128     127.0.0.1:6379                  0.0.0.0:*
LISTEN      0      128          [::]:22                       [::]:*
LISTEN      0      128         [::1]:6379                     [::]:*
The Redis server will start after reboot. To manipulate this default behavior you can either disable or enable the Redis start after reboot by:
$ sudo systemctl disable redis-server
OR
$ sudo systemctl enable redis-server
By default the Redis server will listen only on a local loop-back interface 127.0.0.1.

If you need to configure your Redis server to listen on all networks you will need to configure its main configuration file /etc/redis/redis.conf:
$ sudo nano /etc/redis/redis.conf
and comment the bind 127.0.0.1 ::1:
FROM:
bind 127.0.0.1 ::1
TO:
# bind 127.0.0.1 ::1
Furthermore, if you wish to connect to your Redis server remotely you need to turn off redis protected mode. While still editing /etc/redis/redis.conf find protected-mode yes line and change it:
FROM:
protected-mode yes
TO:
protected-mode no
Once the configuration is completed restart Redis server:
service redis-server restart
The Redis server should be now listening on socket 0.0.0.0:6379. You can confirm this by executing the ss command:
$ ss -nlt
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN      0      128       0.0.0.0:22                    0.0.0.0:* 
LISTEN      0      128       0.0.0.0:6379                  0.0.0.0:*
LISTEN      0      128          [::]:22                       [::]:*
LISTEN      0      128          [::]:6379                     [::]:*
Lastly, if you have the UFW firewall enabled you can open the Redis's port 6379 to any TCP incoming traffic by executing the below command:
$ sudo ufw allow from any to any port 6379 proto tcp
Rule added
Rule added (v6)
If you wish to make your firewall rules more strict visit our How to Open/Allow incoming firewall port guide for more information.
Please refer all article related questions to:

You may also be interested in: