How to install Redis server on Ubuntu Linux 16.04

last updated in Categories ,

Redis is a free and open source (BSD licensed) key-value store. It is in-memory data structure store, used as a database, cache and message broker. How can I install Redis server on an Ubuntu Linux 16.04 LTS server?

Redis used for caching data and other purposes. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. This page shows how to how to install and quickly configure Redis server on an Ubuntu Linux 16.04 LTS server using the apt command/apt-get command.

Redis installation on Ubuntu Linux

Type the following apt command:
$ sudo apt update
$ sudo apt install redis-server

Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libjemalloc1 redis-tools
Suggested packages:
  ruby-redis
The following NEW packages will be installed:
  libjemalloc1 redis-server redis-tools
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 517 kB of archives.
After this operation, 1505 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu xenial/universe amd64 libjemalloc1 amd64 3.6.0-9ubuntu1 [78.9 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial/universe amd64 redis-tools amd64 2:3.0.6-1 [95.3 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial/universe amd64 redis-server amd64 2:3.0.6-1 [343 kB]
Fetched 517 kB in 0s (3331 kB/s)      
Selecting previously unselected package libjemalloc1.
(Reading database ... 14907 files and directories currently installed.)
Preparing to unpack .../libjemalloc1_3.6.0-9ubuntu1_amd64.deb ...
Unpacking libjemalloc1 (3.6.0-9ubuntu1) ...
Selecting previously unselected package redis-tools.
Preparing to unpack .../redis-tools_2%3a3.0.6-1_amd64.deb ...
Unpacking redis-tools (2:3.0.6-1) ...
Selecting previously unselected package redis-server.
Preparing to unpack .../redis-server_2%3a3.0.6-1_amd64.deb ...
Unpacking redis-server (2:3.0.6-1) ...
Processing triggers for libc-bin (2.23-0ubuntu9) ...
Processing triggers for systemd (229-4ubuntu21) ...
Processing triggers for ureadahead (0.100.0-19) ...
Setting up libjemalloc1 (3.6.0-9ubuntu1) ...
Setting up redis-tools (2:3.0.6-1) ...
Setting up redis-server (2:3.0.6-1) ...
Processing triggers for libc-bin (2.23-0ubuntu9) ...
Processing triggers for systemd (229-4ubuntu21) ...
Processing triggers for ureadahead (0.100.0-19) ...

How do I start/stop/restart Redis server?

You need to use the systemctl command:
$ sudo systemctl stop redis # <- stop the server
$ sudo systemctl start redis # <- start the server
$ sudo systemctl restart redis # <- restart the server
$ sudo systemctl status redis # <- show the status of redis server

Fig.01: redis-server.service - Advanced key-value store status on an Ubuntu Linux
Fig.01: redis-server.service – Advanced key-value store status

Redis server configuration

You need to edit the file /etc/redis/redis.conf using a text editor such as vi command/joe command:
$ sudo vi /etc/redis/redis.conf
Set port (default 6379)
port 6379
Make sure Redis only listen on loopback IP address. This is a security feature.
bind 127.0.0.1
If you are using a VPN/VLAN and set network address such as 192.168.2.50. The following enables Apache or other members of cluster talk to Redis box:
bind 192.168.2.50
Set the max number of connected clients at the same time (default 10000):
maxclients 20000
Define how Redis will select what to remove when maxmemory is reached.
maxmemory-policy noeviction
You can select among five behaviors/policies as follows:

  1. volatile-lru – remove the key with an expire set using an LRU algorithm
  2. allkeys-lru – remove any key according to the LRU algorithm
  3. volatile-random – remove a random key with an expire set
  4. allkeys-random – remove a random key, any key
  5. volatile-ttl – remove the key with the nearest expire time (minor TTL)
  6. noeviction – don’t expire at all, just return an error on write operations

To remove the key with an expire set using an LRU algorithm when maxmemory is reached. Update it as follows:
maxmemory-policy allkeys-lru
Set max memory limit to 1gb/1024mb for this Redis server:
maxmemory 1024mb
Save and close the file. Restart the server:
$ sudo systemctl restart redis
Verify that a Redis server running with the netstat command/ss command:
$ netstat -tulpn | grep :6379
OR
$ ss -tulpn

How do I open port 6379 using ufw?

UFW is an acronym for uncomplicated firewall. It is used for managing a Linux firewall and aims to provide an easy to use interface for the user. To allow incoming Redis server connection (tcp port 6379) from specific ip/subnets, run:
$ sudo ufw allow from {IP_SUB/net} to any port 6379 proto tcp
$ sudo ufw allow from 192.168.2.0/24 to any port 6379 \
proto tcp comment 'Open Redis server tcp port'

Say hello to redis-cli

The redis-cli provides a simple command-line interface to a Redis server:
$ redis-cli
127.0.0.1:6379>

Find out server info

Type the following command at the redis-cli prompt:
127.0.0.1:6379> INFO
Sample outputs:

Fig.02: The INFO command returns information and statistics about the server in a format that is simple to parse by computers and easy to read by humans
Fig.02: The INFO command returns information and statistics about the server in a format that is simple to parse by computers and easy to read by humans

Please note that you can type command as follows too:
$ redis-cli INFO

Testing redis server connectivity

Type the following command:
$ redis-cli PING
PONG

How do I store key-values using Redis server?

The syntax is as follows to set a key named domain with value named “www.cyberciti.biz:
$ redis-cli set domain "www.cyberciti.biz"
OK

To retrieve the value for key named domain, run:

$ redis-cli get domain
"www.cyberciti.biz"

If you restarted the bare metal/VM or redis service, the value of your key should still be available:
$ sudo systemctl restart redis
$ redis-cli get domain

How do I delete a key named domain?

$ redis-cli del domain
(integer) 1

See full list of redis command here. Next time I will write about security, user authentication, SSL and more. You can always grab the latest version of Redis from the official project site.

This entry is 1 of 2 in the Redis Server Tutorial series. Keep reading the rest of the series:
  1. How to install Redis server on Ubuntu Linux 16.04
  2. How to flush Redis cache and delete everything using the CLI

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.