Redis is an in-memory key-value store database. There are a few different ways to install redis-server in a Linux based system. To fit our purpose we are going to use the current (latest) stable release from REDIS.IO This process of installation is a bit complex because it requires you to build, compile and install the service manually. So hold on tight…
Install Build Dependencies
Before we download the source code we need to install the build dependencies meta package from ubuntu repositories.
sudo apt-get update
sudo apt-get install build-essential tcl
We are installing
tcl package also to test the binaries after compilation
Download, Compile, Install Redis
After we are done with the compilation of the source code we can purge the files, so we can use
/tmp folder to download and compile the source code.
Let’s change the directory to
Now we can download the
tarball directly in this folder.
curl -O http://download.redis.io/redis-stable.tar.gz
Extract the tarball using
tar -xzvf redis-stable.tar.gz
Change the directory to
Now we can compile the source code
After the compilation is complete, run the test suit to make sure that everything was built correctly
Once the test is finished and everything is okay, it’s time to install the binaries into the system
sudo make install
Once the installation is successful, we can move on to configuring the
To check the installation process was successful or not, use the following command
It should show something like below
Redis server v=5.0.5 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=2f03bf541f259179
Configure Redis Server
To start the configuration process we need to create a configuration directory in
sudo mkdir /etc/redis
The next step is to copy the sample configuration file that comes with the source code to the recently created
/etc/redis directory, using
sudo cp ./redis.conf /etc/redis
Now it’s time to tweak a few configurations, open the file and edit like below
sudo nano /etc/redis/redis.conf
In the file search for
supervised directive and change it to
systemd, default is set to
supervised directive will look like following
Now, we can configure the persistent data storage location by finding the
dir directive. Change it to
Now, find the
logfile directive and change it to
Now, it’s time to find the
pidfile directive, change it to
redis must have access and write permission to these
directories in order to be able to dump data
Save the file and exit.
Create Redis User, Group and Data Directory
Now we can create
redis user and group and also the directory we mentioned in the configuration file. We start by adding a user and group in the system
sudo adduser --system --group --no-create-home redis
Now once the user and group is created we can move on to creating the directory, we can create the directory by using
sudo mkdir /var/lib/redis /var/log/redis /var/run/redis
redis user and group permission to the newly-created directory. To do that use the following
sudo chown redis:redis /var/lib/redis /var/log/redis /var/run/redis
Now, we can tweak the permission of this folder so that other regular users can not access this directory. To achieve that, use the following command
sudo chmod 770 /var/lib/redis /var/log/redis /var/run/redis
Once user, group, and directory permissions are in place, let’s create the
systemd unit file to run redis as a service.
Create Systemd Unit File
Create a file named
/lib/systemd/system using the following command
sudo nano /lib/systemd/system/redis.service
Add the following content to the file, save it, and exit.
[Unit] Description=Advanced key-value store After=network.target [Service] ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf ExecStop=/bin/kill -s TERM $MAINPID PIDFile=/var/run/redis/redis-server.pid TimeoutStopSec=0 Restart=always User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=2755 UMask=007 PrivateTmp=yes LimitNOFILE=65535 PrivateDevices=yes ProtectHome=yes ReadOnlyDirectories=/ ReadWriteDirectories=-/var/lib/redis ReadWriteDirectories=-/var/log/redis ReadWriteDirectories=-/var/run/redis NoNewPrivileges=true CapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE MemoryDenyWriteExecute=true ProtectKernelModules=true ProtectKernelTunables=true ProtectControlGroups=true RestrictRealtime=true RestrictNamespaces=true RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX # redis-server can write to its own config file when in cluster mode so we # permit writing there by default. If you are not using this feature, it is # recommended that you replace the following lines with "ProtectSystem=full". ProtectSystem=true ReadWriteDirectories=-/etc/redis [Install] WantedBy=multi-user.target Alias=redis.service
Once this file is created in
/lib/systemd/system/ create a symlink to it from
sudo ln -s /lib/systemd/system/redis-server.service /etc/systemd/system/redis.service
Please note that two directories are different, one is
/lib another is
/etc, although the child directory names are the same
Start Redis Server
Star the redis systemd process by using the following command
sudo systemctl start redis.service
The current status of
redis-server can be checked with
sudo systemctl status redis
If everything went Okay, this should produce an output similar to following
● redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2019-08-16 19:15:34 CEST; 11s ago Main PID: 30540 (redis-server) Tasks: 4 (limit: 4915) CGroup: /system.slice/redis-server.service └─30540 /usr/local/bin/redis-server 127.0.0.1:6379 Aug 16 19:15:34 isemg-p-ntr001 systemd: Started Advanced key-value store.
To test that the newly installed
redis-server instance is functioning properly, connect to redis server using the command line client for redis.
In the prompt that follows the above command, try
ping command to test the connectivity with the server
It should return something like below
This action confirms the connection to
redis server, now let’s try setting a key-value pair and retrieving it.
127.0.0.1:6379> set test "this is a test string from the blog"
The output should be similar as below
Now let’s retrive the saved value
127.0.0.1:6379> get test
In the output it should return the text we set in the previous step
"this is a test string from the blog"
Once it’s confirmed, let’s restart the redis server and confirm that it’s persistent with data even after restart
sudo systemcrl restart redis
Now let’s try to retrive the stored value for key
redis-cli 127.0.0.1:6379> get test "this is a test string from lego python blog"
This confirms that redis installation is fully operational and ready for use.