Install Redis from Source Code on Raspberry Pi

Install Redis from Source Code on Raspberry Pi

Redis is an in-memory key-value data structure store that often used as a database, cache, and message broker. Redis is an open-source project that available under the 3-clause BSD license.

This tutorial explains how to install Redis from source code on Raspberry Pi.

Prepare environment

In order to compile Redis from source code we need to install build-essential package that contains a list of build tools. Additionally, we can install checkinstall package that allows to install the software and later easily to remove it.

sudo apt update
sudo apt install -y build-essential checkinstall

Compile and install Redis

Move to your home directory and download the latest stable version of Redis. Extract downloaded tar.gz file and navigate to the Redis source directory.

cd ~
curl -O http://download.redis.io/redis-stable.tar.gz
tar xf redis-stable.tar.gz
cd redis-stable

Compile Redis using make command. The -j option defines the number of jobs to run in parallel. The nproc command returns the number of CPU cores available.

make -j$(nproc)

Compilation took about 50 seconds on Raspberry Pi 4 (8GB).

We can install Redis with make install command but later we cannot use package manager to uninstall Redis. To install it we use checkinstall command.

sudo checkinstall

The checkinstall creates Debian package (.deb file) and installs it. You will be asked to answer questions. Use default answers, except when answering question about version of the package. The stable is illegal version. So you need to provide number instead, for example 1.

...
This package will be built according to these values:

0 -  Maintainer: [ root@raspberrypi ]
1 -  Summary: [ Package created with checkinstall 1.6.2 ]
2 -  Name:    [ redis ]
3 -  Version: [ stable ]
4 -  Release: [ 1 ]
5 -  License: [ GPL ]
6 -  Group:   [ checkinstall ]
7 -  Architecture: [ armhf ]
8 -  Source location: [ redis-stable ]
9 -  Alternate source location: [  ]
10 - Requires: [  ]
11 - Provides: [ redis ]
12 - Conflicts: [  ]
13 - Replaces: [  ]

Enter a number to change any of them or press ENTER to continue: 3
Enter new version:
>> 1
...
Building Debian package...OK

Installing Debian package...OK
...

After the installation is finished, we can check version of the Redis.

redis-server --version

Configure Redis

Create a configuration directory for Redis and copy default configuration file to it.

cd ~
sudo mkdir /etc/redis
sudo cp redis-stable/redis.conf /etc/redis

Open configuration file because we need to make few changes on it.

sudo nano /etc/redis/redis.conf

Search for supervised directive. Change value no to systemd. We will use systemd init system to run Redis as a service. Next, search for dir directive. It used to specify the working directory for Redis to store persistent data.

Change from:

...
supervised no
...
dir ./
...

to:

...
supervised systemd
...
dir /var/lib/redis
...

The tar.gz file and source code are no longer needed and we can remove them.

sudo rm -rf redis-stable.tar.gz
sudo rm -rf redis-stable

Create user for Redis

Execute the following command to create redis user and group:

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

Create working directory for Redis and make redis user as owner. Modify the permissions of directory to restrict access for other users.

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

Run Redis as a service

Create and open a systemd unit file:

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

Copy the following content to the file:

[Unit]
Description=In-memory data structure 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

Save and close file. Now we can start Redis service:

sudo service redis start

To make sure that Redis service is running, we can use this command:

sudo service redis status

Also we can stop or restart the Redis service:

sudo service redis stop
sudo service redis restart

To enable Redis to start on boot, execute the following command:

sudo systemctl enable redis

Testing Redis

We can connect to the Redis server using client via command line.

redis-cli

To test connectivity use PING command. Then server returns PONG.

127.0.0.1:6379> PING
PONG

Use SET and GET commands to store and retrieve value by key.

127.0.0.1:6379> SET message "Hello"
OK
127.0.0.1:6379> GET message
"Hello"

To exit the Redis prompt, type QUIT command.

127.0.0.1:6379> QUIT

Uninstall Redis

If you decided to completely remove the Redis, stop the service and remove a systemd unit file.

sudo service redis stop
sudo systemctl disable redis
sudo rm -rf /etc/systemd/system/redis.service
sudo systemctl daemon-reload
sudo systemctl reset-failed

Then uninstall Redis, remove related directories and redis user:

sudo dpkg -r redis
sudo rm -rf /etc/redis
sudo rm -rf /var/lib/redis
sudo deluser redis

Also you can remove build-essential, checkinstall and related dependencies which used for building and installing the Redis.

sudo apt purge --autoremove -y build-essential gcc g++ make checkinstall

The 2 Comments Found

Leave a Comment

Cancel reply

Your email address will not be published.