Install Redis in Ubuntu

Posted on May 29, 20189 min read — in development

Redis is an open source, in-memory data structure store, used as a database, cache and message broker. It is known for its flexibility, performance, and wide language support. That is why it is commonly used as a message broker for celery. Here, I will show how to install Redis on an Ubuntu.

Install Redis

Open the terminal and install build-essential and tcl packages using apt:

sudo apt-get install build-essential tcl

Download the redis.tar.gz file from https://redis.io/download and extract it in any directory. Open the terminal in that directory and run the following commands to build and install Redis:

make
make test
sudo make install

Configure Redis

Create a Redis configuration directory:

sudo mkdir /etc/redis
sudo cp <redis_directory>/redis.conf /etc/redis
sudo vim /etc/redis/redis.conf

Find the "supervised" directive which is currently set to "no". Change this to "systemd":

.....
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous liveness pings back to your supervisor.
supervised systemd
.....

Find the dir directory and set this to "/var/lib/redis". Redis will use this directory to dump data.

.....
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis
.....

Create Redis Systemd File

Create a systemd unit file so that the init system can manage the Redis process.

sudo touch /etc/systemd/system/redis.service
sudo nano /etc/systemd/system/redis.service

Write the following lines in redis.service file:

[Unit]
Description=Redis In-Memory Data Store
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

Create Redis User, Group and Directories

Create a Redis user and group:

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

Create the /var/lib/redis directory by typing:

sudo mkdir /var/lib/redis

Change ownership and permissions of this directory:

sudo chown redis:redis /var/lib/redis
sudo chmod 770 /var/lib/redis

Start Redis Service

Start up the Redis systemd service by typing:

sudo systemctl start redis

Redis service will start by now. Check Redis status using the following command:

sudo systemctl status redis

Now, we would like to start Redis automatically when the server boots. To do so, type:

sudo systemctl enable redis

To learn more about Redis, visit Redis Documentation.
To know about the details step by step guide visit DigitalOcean Tutorials.

« PREVIOUS
Celery Integration with Flask and Redis
NEXT »
A Quick Guide to Django Logging