How to install Redis on Ubuntu 20.04 LTS

How To Install and Configure Redis 6.0 On Ubuntu 20.04 LTS

On this article we will discuss how to install and configure Redis version 6.0.x on Linux Ubuntu 20.04 LTS operating system.

Introduction

Redis is an open source (BSD licensed) in-memory data structure store, used as a database, cache and message broker. Redis is an acronym for Remote dictionary server, supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. The Redis project is developed and maintained by a project core team and as of 2015 is sponsored by Redis Labs. On this article we will show you how to install Redis version 6.0.6 on Ubuntu 20.04 Linux operating system.



Redis 6.0 Installation on Ubuntu 20.04 LTS

The installation and configuration of Redis 6.0 on Ubuntu 20.04 LTS will be briefly exlplained below, on this article we are using Redis PPA:chris-lea/redis-server. Installation steps will be explained below :

  1. Prerequisites
  2. Install Redis 6.0
  3. Configuring Redis 6.0
  4. Fine Tuning Redis 6.0
  5. Testing Redis

1. Prerequisites

Before the installation is started, we have to prepare our system and all related package for installation.

  • Ubuntu 20.04 LTS operating system with enough free memory for Redis
  • Account with sudo privilege
  • Updated Ubuntu 20.04 LTS and Firewall installed

2. Install Redis 6.0

2.1. Add PPA chris-lea/redis-server

We will use PPA chris-lea/redis-server repository for our Redis 6.0 source, for this purpose we will add this repository to the system by submitting command line : sudo add-apt-repository ppa:chris-lea/redis-server.

ramans@diginet-app02:~$ sudo add-apt-repository ppa:chris-lea/redis-server
  Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.
  More info: https://launchpad.net/~chris-lea/+archive/ubuntu/redis-server
 Press [ENTER] to continue or Ctrl-c to cancel adding it.
 Hit:1 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu focal InRelease
 Hit:2 http://us.archive.ubuntu.com/ubuntu focal InRelease           
 Hit:3 http://security.ubuntu.com/ubuntu focal-security InRelease    
 Hit:4 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease
 Hit:5 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease
 Hit:6 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease
 Hit:7 http://us.archive.ubuntu.com/ubuntu focal-backports InRelease
 Hit:8 http://ppa.launchpad.net/wireshark-dev/stable/ubuntu focal InRelease
 Reading package lists… Done
add PPA Redis repository
Add PPA Redis repository

2.2. Update the system

To take effect, we will update the system to the newest software package, by submitting command line : sudo apt-get update.

ramans@diginet-app02:~$ sudo apt-get update
 Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease    
 Hit:2 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu focal InRelease
 Hit:3 http://us.archive.ubuntu.com/ubuntu focal InRelease           
 Hit:4 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease
 Hit:5 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease
 Hit:6 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease
 Hit:7 http://ppa.launchpad.net/wireshark-dev/stable/ubuntu focal InRelease
 Hit:8 http://us.archive.ubuntu.com/ubuntu focal-backports InRelease
 Reading package lists… Done

2.3. Install Redis 6.0

Install Redis 6.0, by submitting command line : sudo apt-get install redis-server -y.

ramans@diginet-app02:~$ sudo apt-get install redis-server -y
 Reading package lists… Done
 Building dependency tree       
 Reading state information… Done
 The following packages were automatically installed and are no longer required:
   libatomic1 libeclipse-jdt-core-java libfprint-2-tod1 libjemalloc2 libllvm9 liblua5.1-0 libtcnative-1
   lua-bitop lua-cjson
 Use 'sudo apt autoremove' to remove them.
 The following additional packages will be installed:
   redis-tools
 Suggested packages:
   ruby-redis
 The following NEW packages will be installed:
   redis-server redis-tools
 0 upgraded, 2 newly installed, 0 to remove and 89 not upgraded.
 Need to get 0 B/1,082 kB of archives.
 After this operation, 6,355 kB of additional disk space will be used.
 Selecting previously unselected package redis-tools.
 (Reading database … 186846 files and directories currently installed.)
 Preparing to unpack …/redis-tools_5%3a6.0.6-3chl1~focal1_amd64.deb …
 Unpacking redis-tools (5:6.0.6-3chl1~focal1) …
 Selecting previously unselected package redis-server.
 Preparing to unpack …/redis-server_5%3a6.0.6-3chl1~focal1_amd64.deb …
 Unpacking redis-server (5:6.0.6-3chl1~focal1) …
 Setting up redis-tools (5:6.0.6-3chl1~focal1) …
 Setting up redis-server (5:6.0.6-3chl1~focal1) …
 Processing triggers for man-db (2.9.1-1) …
 Processing triggers for systemd (245.4-4ubuntu3.3) …
Install Redis 6.0 on Ubuntu 20.04 LTS
Install Redis 6.0 on Ubuntu 20.04 LTS

After the installation was completed done, than we will enable it service to run after reboot automatically. To enable it we have to submit the command line : sudo systemctl enable redis-server.service.

ramans@diginet-app02:~$ sudo systemctl enable redis-server.service 
 [sudo] password for ramans: 
 Synchronizing state of redis-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
 Executing: /lib/systemd/systemd-sysv-install enable redis-server
 Created symlink /etc/systemd/system/redis.service → /lib/systemd/system/redis-server.service.
 ramans@diginet-app02:~$ sudo vi /etc/redis/redis.conf
Enable Redis Server service
Enable Redis Server service

2.4. Verify Redis Version

We will verifyt the Redis version to ensure if our installation was succeeded. By submitting command line : redis-server --version.

ramans@diginet-app02:~$ redis-server --version
 Redis server v=6.0.6 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=d463d609620685c0
Redis 6.0 version
Redis 6.0 version

3. Configuring Redis 6.0

After Redis 6.0 installation was completed done, the next step is to configure Redis so that it can run properly and in accordance with the expected system. Redis configuration is located at /etc/redis/redis.conf file. At our scenario, Redis is installed on application server, where the binding IP Address is located at 127.0.0.1 ::1. Just uncomment the parameter :

############################ NETWORK
 By default, if no "bind" configuration directive is specified, Redis listens
 for connections from all the network interfaces available on the server.
 It is possible to listen to just one or multiple selected interfaces using
 the "bind" configuration directive, followed by one or more IP addresses.
 #
 Examples:
 #
 bind 192.168.1.100 10.0.0.1
 bind 127.0.0.1 ::1
 #
 ~ WARNING ~ If the computer running Redis is directly exposed to the
 internet, binding to all the interfaces is dangerous and will expose the
Install Redis | Binding on localhost
Binding on localhost

There are several parameters that we can configure in this file. The example is : maxmemory. By default, when maxmemory is reached, Redis will stop writing new data.

The last step is to restart the Redis service, by submitting command line : sudo systemctl restart redis-server.service and query it status by submitting command line : sudo systemctl status redis-server.service.

ramans@diginet-app02:~$ sudo systemctl restart redis-server.service 
 ramans@diginet-app02:~$ sudo systemctl status redis-server.service 
 ● redis-server.service - Advanced key-value store
      Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
      Active: active (running) since Thu 2021-02-11 09:10:01 PST; 9s ago
        Docs: http://redis.io/documentation,
              man:redis-server(1)
     Process: 4926 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
    Main PID: 4928 (redis-server)
       Tasks: 5 (limit: 9451)
      Memory: 2.6M
      CGroup: /system.slice/redis-server.service
              └─4928 /usr/bin/redis-server 127.0.0.1:6379
 Feb 11 09:10:00 diginet-app02 systemd[1]: Starting Advanced key-value store…
 Feb 11 09:10:01 diginet-app02 systemd[1]: redis-server.service: Can't open PID file /run/redis/redis-server>
 Feb 11 09:10:01 diginet-app02 systemd[1]: Started Advanced key-value store.
Restart Redis Service
Restart Redis Service

4. Fine Tuning Redis 6.0

Once the Redis service has been upgraded, we can see if Redis is running well or if there are still parameters that need to be fixed. This can be seen in the logs after starting the Redis service. Log Redis akan berada pada file : /var/log/redis/redis-server.log.

4.1 Chekcing log and Update Parameter

ramans@diginet-app02:~$ sudo tail /var/log/redis/redis-server.log
               `-.__.-'                                               
 4928:M 11 Feb 2021 09:10:01.018 # Server initialized
 4928:M 11 Feb 2021 09:10:01.018 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
 4928:M 11 Feb 2021 09:10:01.018 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
 4928:M 11 Feb 2021 09:10:01.019 * Loading RDB produced by version 6.0.6
 4928:M 11 Feb 2021 09:10:01.019 * RDB age 1 seconds
 4928:M 11 Feb 2021 09:10:01.019 * RDB memory usage when created 0.77 Mb
 4928:M 11 Feb 2021 09:10:01.019 * DB loaded from disk: 0.000 seconds
 4928:M 11 Feb 2021 09:10:01.019 * Ready to accept connections
Warning on Redis
Warning on Redis

We found if overcommit_memory parameter is set to 0, as recommendation the parameter shoud be =1. We will fix it by using a command line as shown below :

ramans@diginet-app02:~$ sudo echo 'vm.overcommit_memory = 1' | sudo tee -a /etc/sysctl.conf > /dev/null
ramans@diginet-app02:~$ sudo sysctl -p
vm.overcommit_memory = 1
ramans@diginet-app02:~$ sudo vi /etc/rc.local
Reid installation
Redis Andalan

So far, after we changed the vm.overcomit_memory paramter to be ‘1’, then we restart the Redis service and look back the log. We found if the warning message was disappeared.

ramans@diginet-app02:~$ sudo tail -f /var/log/redis/redis-server.log
       -._-..-'    _.-'                                                  -._        _.-'                                            -..-'                                               
 1018:M 11 Feb 2021 09:24:16.373 # Server initialized
 1018:M 11 Feb 2021 09:24:16.374 * Loading RDB produced by version 6.0.6
 1018:M 11 Feb 2021 09:24:16.374 * RDB age 0 seconds
 1018:M 11 Feb 2021 09:24:16.374 * RDB memory usage when created 0.76 Mb
 1018:M 11 Feb 2021 09:24:16.375 * DB loaded from disk: 0.000 seconds
 1018:M 11 Feb 2021 09:24:16.375 * Ready to accept connections

redis-server.log after fine tuning
redis-server.log after fine tuning

4.2 Allowing Firewall

If the an application and and Redis server are separate and the Redis server implements a firewall, then we have to open the firewall for this purpose by submitting command line : sudo ufw allow 6379, where Redis is binding on Port 6379.

ramans@diginet-app02:~$ sudo ufw allow 6379
 Rules updated
 Rules updated (v6)
Allow port 6379 Firewall Redis-server
Allow port 6379 Firewall

5. Redis 6.0 Installation Testing

On this section we will test, the Redis server installation by querying any request to the server and verify the response. The testing will be performed by installing redis client and making a connection to the server.

5.1 Install Redis Client

We will install Redis client on the same host, by submitting command line : sudo apt-get install redis-tools.

ramans@diginet-app02:~$ sudo apt-get install redis-tools
 [sudo] password for ramans: 
 Reading package lists… Done
 Building dependency tree       
 Reading state information… Done
 redis-tools is already the newest version (5:6.0.6-3chl1~focal1).
 redis-tools set to manually installed.
 The following packages were automatically installed and are no longer required:
   libatomic1 libeclipse-jdt-core-java libfprint-2-tod1 libjemalloc2 libllvm9 liblua5.1-0
   libtcnative-1 lua-bitop lua-cjson
 Use 'sudo apt autoremove' to remove them.
 0 upgraded, 0 newly installed, 0 to remove and 89 not upgraded.

5.2 ConnectionTesting

Then submit the command line to connect to the Redis-server : redis-cli -h 127.0.0.1.

ramans@diginet-app02:~$ redis-cli -h 127.0.0.1
 127.0.0.1:6379> set testkey ototest
 OK
 127.0.0.1:6379> get testkey
 "ototest"
 127.0.0.1:6379> exit

Conlussion

Redis 6.0 Installation on Ubuntu 20.04 LTS has been completed done. For more detail information about Redis, it can be found on Redis official webiste. Have a nice day and stay safe.

Share this article via :

Leave a Reply

Your email address will not be published. Required fields are marked *