How to setup Redis on Ubuntu!

Step by step Redis setup.

Intro

Redis is a software used for database or caching that stores a key-value running in-memory. Click for more details

Requirements

We need a machine with Ubuntu 16.04 installed a non-root user with sudo authority in order to have Redis working in your hand.

Pre Setup and Test

I prefer to set up Redis from source code. If you want to set up via OS package manager or Homebrew I'll add those later.

First of all we need to install some packages.

  • build-essential
  • tcl

As we always do, we get updates first.

sudo apt-get update
$ sudo apt-get install build-essential tcl

Download

I prefer to download source code to /tmp.

cd /tmp
$ curl -O http://download.redis.io/redis-stable.tar.gz
$ tar xzvf redis-stable.tar.gz

Compile and Setup

While into /tmp directory, executing commands below

make
$ make test

And;

sudo make install

Configuration

After the setup, we need to configure Redis. To save the configuration files, we need to create /etc/redisdirectory. To do this;

sudo mkdir /etc/redis

Copy the sample configuration files to the directory we just created.

sudo cp /tmp/redis-stable/redis.conf /etc/redis

Open and start editing the file.

sudo nano /etc/redis/redis.conf

In Configuration File;

We need to changecode supervised no to supervised systemd Because Ubuntu using systemd init.

Also change dir ./ todir /var/lib/redis. This line indicates where permanent data is stored. Save and close the configuration file.

Creating Systemd Unit File

We need to edit redis.service file like below and than save it. (Typical service definition)

sudo nano /etc/systemd/system/redis.service
[Unit]
Description=Redis standalone
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

Redis User/Group/Path Definitions

To create a user;

sudo adduser --system --group --no-create-home redis

To create a directory

sudo mkdir /var/lib/redis

To create ownership;

sudo chown redis:redis /var/lib/redis

To change write permission;

sudo chmod 770 /var/lib/redis

Starting Redis and Checking the Status

To start;

sudo systemctl start redis

To status check;

sudo systemctl status redis

When you execute the last command, you need to see green "Active: active (running)" message.

Test with CLI

Redis CLI is a client tool to communicate with the Redis server. To execute;

redis-cli

lets ping it;

 127.0.0.1:6379> ping

response;

 PONG

Let's continue with the data test.

127.0.0.1:6379> set test "Habil"

Output is;

OK

Give it to me back;

127.0.0.1:6379> get test

Response

 "Habil"

Starting Redis Service on Boot-up

$ sudo systemctl enable redis

The End

We currently have a fully working Redis installation in our operating system. If you wish, you can take a look at official documents.

Comments