How To Install Redis On Ubuntu 16.04

In this tutorial we learn how to install Redis on Ubuntu 16.04. Redis is an open source (BSD licensed), in-memory data structure store.

Objective

In this tutorial we’ll learn how to install Redis on Ubuntu 16.04 Xenial Xerus. We will learn how to install Redis from Ubuntu repository, from PPA repository and latest version from source

What is Redis

Redis is :

an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker.

Introduction to Redis

Redis support multiple types of data structures. You can find data structures that Redis support below :

  • Binary-safe strings
  • Lists
  • Sets
  • Sorted Sets
  • Hashes
  • Bit arrays (or bitmaps)
  • Hyperloglogs

Requirements

This tutorial assumes you have clean install of Ubuntu Server 16.04. If you are using server that already running production workload, you might want to skip some steps like upgrading all packages.

Install Redis on Ubuntu 16.04

Install Redis From Ubuntu Repository

Now, let’s install Redis from Ubuntu repository. Before installing Redis, update the system to latest update.

$ sudo apt-get update
$ sudo apt-get upgrade

After updating the system, it’s time to install Redis from repository.

$ sudo apt-get -y install redis-server    

By default, redis-server is started after installation. You can check using service command :


$ sudo service redis-server status
● redis-server.service - Advanced key-value store
    Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
    Active: active (running) since Mon 2017-10-02 13:25:52 UTC; 2min 44s ago
      Docs: http://redis.io/documentation,
            man:redis-server(1)
  Main PID: 2610 (redis-server)
    CGroup: /system.slice/redis-server.service
            └─2610 /usr/bin/redis-server 127.0.0.1:6379       

Oct 02 13:25:52 ubuntu-xenial systemd[1]: Starting Advanced key-value store...
Oct 02 13:25:52 ubuntu-xenial run-parts[2600]: run-parts: executing /etc/redis/redis-server.pre-up.d/00_example
Oct 02 13:25:52 ubuntu-xenial run-parts[2611]: run-parts: executing /etc/redis/redis-server.post-up.d/00_example
Oct 02 13:25:52 ubuntu-xenial systemd[1]: Started Advanced key-value store.

ubuntu@ubuntu-xenial:~$ systemctl status redis-server
● redis-server.service - Advanced key-value store
    Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
    Active: active (running) since Mon 2017-10-02 13:25:52 UTC; 2min 58s ago
      Docs: http://redis.io/documentation,
            man:redis-server(1)
  Main PID: 2610 (redis-server)
    CGroup: /system.slice/redis-server.service
            └─2610 /usr/bin/redis-server 127.0.0.1:6379       

Oct 02 13:25:52 ubuntu-xenial systemd[1]: Starting Advanced key-value store...
Oct 02 13:25:52 ubuntu-xenial run-parts[2600]: run-parts: executing /etc/redis/redis-server.pre-up.d/00_example
Oct 02 13:25:52 ubuntu-xenial run-parts[2611]: run-parts: executing /etc/redis/redis-server.post-up.d/00_example
Oct 02 13:25:52 ubuntu-xenial systemd[1]: Started Advanced key-value store.      

We can also check using netstat command whether redis-server already listen on a port or not.


$ sudo netstat -naptu | grep LISTEN
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      2610/redis-server 1
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1310/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      1310/sshd    

From output above we learned that redis server is already listening on port 6379 and bind to localhost or 127.0.0.1.

To check redis version you can run command below


$ redis-server --version
Redis server v=3.0.6 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=687a2a319020fa42        

Install Redis From PPA Repository

As you can see above that Redis server shipped with Xenial is still version 3.0.6.

To install the latest version of Redis we can install it from Chris Lea Redis PPA

Let’s add Chris Lea PPA repository


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

Press ENTER to continue adding this PPA repository.


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    

Redis PPA repository added successfully.


gpg: keyring `/tmp/tmp0yd1pd9h/secring.gpg' created
gpg: keyring `/tmp/tmp0yd1pd9h/pubring.gpg' created
gpg: requesting key C7917B12 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmp0yd1pd9h/trustdb.gpg: trustdb created
gpg: key C7917B12: public key "Launchpad chrislea" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK    

Update apt metadata database by running command below:


$ sudo apt-get update    

Install latest version of Redis server using command below.


$ sudo apt-get install redis-server    

apt will automatically uninstall Redis server that we already installed from Ubuntu repositroy

Configuring Redis

Redis configuration is located on /etc/redis/redis.conf file. In this tutorial we’ll change one Redis configuration directive so that it will listen to all network interfaces instead of only on localhost.

Open /etc/redis/redis.conf. Find line below:


bind 127.0.0.1

Change the line above with


bind 0.0.0.0

Restart Redis service


$ sudo service redis-server restart

Now check where does Redis listening.


$ sudo netstat -naptu | grep LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 906/sshd
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 7650/redis-server 0
tcp6 0 0 :::22 :::* LISTEN 906/sshd

We see above that redis is listening on all interfaces on port 6379 (0.0.0.0:6379).

There are a lot more configuration directive on redis.conf file. You can read the comment above each directive to see how you can customize Redis configuration.

Redis Learning Resources

If you want to learn more about how-to use Redis, you can follow some tutorial below :

Summary

In this tutorial we learned how-to install Redis on Ubuntu 16.04 from Ubuntu repository.

We also learned managing Redis service, and do basic configuration change by changing Redis port.

I hope this tutorial will help you getting started with Redis.