How To Install Redis On Ubuntu 20.04

On this short article we will learn how to install Redis on Ubuntu 20.04 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. On this article we will show you how to install Redis version 6.0.6 on Ubuntu 20.04 Linux operating system.

Redis is in the family of databases called key-value stores. The essence of a key-value store is the ability to store some data, called a value, inside a key. This data can later be retrieved only if we know the exact key used to store it. Often Redis it is called a data structure server because it has outer key-value shell, but each value can contain a complex data structure, such as a string, a list, a hashes, or ordered data structures called sorted sets as well as probabilistic data structures like hyperloglog.

Installing Redis 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 :

  • Prerequisite
  • Install Redis
  • Configuring Redis
  • Testing Redis

Prerequisite

We have to prepare the system and all related supported matters for installation.

  • Ubuntu 20.04 LTS operating system with enough free memory for Redis
  • System user with sudo privilege
  • Updated the Ubuntu 20.04 LTS

Install Redis

1. Add Redis Repository

At this tutorial, we will use a third-party PPA which contains the Redis package, we will use Chris Lea’s PPA. For this purpose we will use command line :

$ sudo add-apt-repository ppa:chris-lea/redis-server

Output :

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.

Get:1 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu focal InRelease [17.5 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [109 kB] 
Hit:3 http://us.archive.ubuntu.com/ubuntu focal InRelease 
Hit:4 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease 
Get:5 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Hit:6 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease 
Get:7 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [24.3 kB]
Hit:8 http://ppa.launchpad.net/wireshark-dev/stable/ubuntu focal InRelease 
Get:9 http://us.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB] 
Get:10 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu focal/main i386 Packages [496 B]
Get:11 http://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [56.5 kB]
Get:12 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [804 kB] 
Get:13 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu focal/main amd64 Packages [1,016 B] 
Get:14 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu focal/main Translation-en [584 B]
Get:15 http://us.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [417 kB]
Get:16 http://us.archive.ubuntu.com/ubuntu focal-updates/main Translation-en [194 kB]
Get:17 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [265 kB]
Get:18 http://us.archive.ubuntu.com/ubuntu focal-updates/main DEP-11 64x64 Icons [85.6 kB]
Get:19 http://us.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [301 kB]
Get:20 http://us.archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [2,468 B]
Get:21 http://us.archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [1,764 B] 
Fetched 2,495 kB in 6s (406 kB/s) 
Reading package lists... Done

2. Update System

After adding the Redis PPA, we need to update our system packages before proceeding to install the redis-server. To update our system, we just submit command line :

$ sudo apt-get update

The output :

ramans@diginet-app02:~$ sudo apt-get update
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://us.archive.ubuntu.com/ubuntu focal-updates InRelease 
Hit:5 http://us.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:6 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease
Hit:7 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease
Hit:8 http://ppa.launchpad.net/wireshark-dev/stable/ubuntu focal InRelease
Reading package lists... Done

3. Install Redis

After all are set, then we will begin install Redis by using command line :

$ sudo apt-get install redis-server -y

Output :

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 1,082 kB of archives.
After this operation, 6,355 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu focal/main amd64 redis-tools amd64 5:6.0.6-3chl1~focal1 [997 kB]
Get:2 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu focal/main amd64 redis-server amd64 5:6.0.6-3chl1~focal1 [84.5 kB]
Fetched 1,082 kB in 3s (385 kB/s) 
Selecting previously unselected package redis-tools.
(Reading database ... 186851 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) ...
Installing new version of config file /etc/redis/redis.conf ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for systemd (245.4-4ubuntu3.3) ...

4. Verify Redis Installation

After installation was completed done, the next step is to verify the installation. This task can be performed by querying Redis version.

$ redis-server --version

Output :

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

By default,  Redis is not managed as a service, on this stage we will set it to be supervised directive to systemd (Ubuntu’s init system). To perform this task we will edit /etc/redis/redis.conf file by using vi text editor.

$ sudo vi /etc/redis/redis.conf
supervised systemd 

After it was done, then we restart the redis service. by submitting command line.

$ sudo systemctl restart redis-server.service

Or we will umask the Redis system service by submitting the following command lines :

$ sudo systemctl unmask redis-server.service
$ sudo systemctl enable redis-server.service
$ sudo systemctl start redis-server.service
$ sudo systemctl status redis-server.service

Output :

ramans@diginet-app02:~$ sudo systemctl unmask redis-server.service 
Removed /etc/systemd/system/redis-server.service.
ramans@diginet-app02:~$ sudo systemctl enable redis-server.service 
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
ramans@diginet-app02:~$ sudo systemctl start 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 08:28:44 PST; 4s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Process: 8387 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
Main PID: 8389 (redis-server)
Tasks: 5 (limit: 9451)
Memory: 2.3M
CGroup: /system.slice/redis-server.service
└─8389 /usr/bin/redis-server 127.0.0.1:6379

Feb 11 08:28:43 diginet-app02 systemd[1]: Starting Advanced key-value store...
Feb 11 08:28:44 diginet-app02 systemd[1]: redis-server.service: Can't open PID file /run/redis/redis-server.pid>
Feb 11 08:28:44 diginet-app02 systemd[1]: Started Advanced key-value store.

Configuring And Fine Tuning Redis

On this stage we will modify the Redis performance by updating several parameters on redis configuration file. For this purpose we will open the Redis log on /var/log/redis/redis-sever.log file.

$ sudo tail /var/log/redis/redis-server.log

The output :

ramans@diginet-app02:~$ sudo tail /var/log/redis/redis-server.log
[sudo] password for ramans: 
8389:M 11 Feb 2021 08:28:44.011 # Server initialized
8389:M 11 Feb 2021 08:28:44.012 # 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.
8389:M 11 Feb 2021 08:28:44.012 # 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.
8389:M 11 Feb 2021 08:28:44.013 * Loading RDB produced by version 5.0.7
8389:M 11 Feb 2021 08:28:44.013 * RDB age 1366 seconds
8389:M 11 Feb 2021 08:28:44.013 * RDB memory usage when created 0.76 Mb
8389:M 11 Feb 2021 08:28:44.013 * DB loaded from disk: 0.000 seconds
8389:M 11 Feb 2021 08:28:44.013 * Ready to accept connections
8389:M 11 Feb 2021 08:28:44.013 # systemd supervision requested, but NOTIFY_SOCKET not found
8389:M 11 Feb 2021 08:28:44.013 # systemd supervision requested, but NOTIFY_SOCKET not found

To fix these warning, we will submit the following command line :

$ sudo echo 'vm.overcommit_memory = 1' | sudo tee -a /etc/sysctl.conf > /dev/null
$ sudo sysctl -p

The output :

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

And creating a new file named as /etc/rc.local with the following contents :

ramans@diginet-app02:~$ sudo vi /etc/rc.local

#!/bin/bash
echo never > /sys/kernel/mm/transparent_hugepage/enabled
systemctl restart redis-server.service
exit 0

Then modify this file to be runnable file.

ramans@diginet-app02:~$ sudo chown root:root /etc/rc.local
ramans@diginet-app02:~$ sudo chmod 770 /etc/rc.local

Login After Fine Tuning

After the tuning is done, we will get the log to be :

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
^C

Modify Firewall

to allow redis to be accessible from the outside, then we have to loosen the firewall embedded into the system. this can be done by opening the port so that it can be accessed from the outside.

$ sudo ufw allow 6379

Output :

ramans@diginet-app02:~$ sudo ufw allow 6379
Rules updated
Rules updated (v6)

Testing Redis

To do testing on redis, we will first install the tools that come with Redis. To install this tool, we will submit the following command line :

$ sudo apt-get install redis-tools

Output :

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.

The output above show us, if the tools set of Redis has been installed on the system.

On our scenario we will test to set an retrieve an assign value to a specific key. Since Redis is an in-memory key-value NoSQL database, you may also want to test whether it retrieves assigned values based on the specified key. On this testing we will use set and get reserved word of Redis. As shown below.

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

Redis Testing

Conclusion

On this short tutorial we have learn how to install Redis on Ubuntu 20.04 LTS operating system. I hope this article could be helpful.

Add a Comment

Your email address will not be published.