Introduction
Redis is opensource, an in-memory key-value store known for its flexibility, performance, and broad language support. It is commonly used as a database, cache, and message broker, and supports a wide range of data structures.
In this tutorial we will explain how to install and configure redis from source on ubuntu 18.04 AWS EC2 server.
You can install redis from official ubuntu repositories using apt command with few steps,but the version will not be the latest one.Here we will install the latest version Redis 6.0.6 from the source on ubuntu 18.04.To know about redis versions got to Redis version
Before We Begin
- Ubuntu 18.04 server /desktop with root or user with root privileges
Step 1 : Installing the Build and Test Dependencies
To get the latest version of Redis, we have to compile and install the software from the source. So before we are going to download the source code, you must satisfy the build dependencies so that you can compile the software
For this Redis server we need to install below packages from the Ubuntu repositories
-
build-essential
meta-package tcl
package to test the binaries
Update your local apt
package cache and install the dependencies by typing:
sudo apt update sudo apt install build-essential tcl
Now we have all the build and test dependencies are installed on our server and we can begin the process of installing Redis.
Step 2 — Downloading, Compiling, and Installing Redis
Now it is time to install Redis by downloading, compiling, and then building the source code. Since we won’t need to keep the Redis source code for the long term in our machine (you can always re-download it), it is best practice to download the source code to our /tmp
directory.
Got to tmp directory from the terminal:
cd /tmp
Next, use curl or wget to download the latest stable version of Redis. The latest version can always be found at a stable download URL: At the time of writing this article the stable version is 6.0.6
curl -O http://download.redis.io/redis-stable.tar.gz
Unpack the downloaded tarball by running :
tar xzvf redis-stable.tar.gz
Then move into the Redis source directory where we was just extracted:
cd /redis-stable
Then Compile the Redis binaries by typing:
make
This command will take seconds to finish.It will end like below :
After the binaries have finished compiling, run the test command to make sure everything was built correctly. The server itself gives that as a Hint.
make test
Sometimes this command fails with some warning as below
!!! WARNING The following tests failed:
*** [err]: pending querybuf: check size of pending_querybuf after set a big valu e in tests/unit/pendingquerybuf.tcl the used_memory of replica is much larger than master. Master:43876800 Replica:8 5820872 Cleanup: may take some time… OK Makefile:330: recipe for target 'test' failed make[1]: *** [test] Error 1 make[1]: Leaving directory '/tmp/redis-stable/src' Makefile:6: recipe for target 'test' failed make: *** [test] Error 2
In that case you can run this with sudo or re-run the make test.In my case it solved after rerunning the make command.For more info check https://github.com/redis/redis/issues/5463#issuecomment-453971420
Even this test fails if the redis is running & active you can neglect this error.
Normally it will take some time to complete the command to finish.
Once the test completes, install the binaries onto the system by typing:
sudo make install
That is it for installing Redis.Now we need to create create a configuration directory.
The Redis configuration directory is conventionally located within the /etc/
directory, and you can create it there by typing:
sudo mkdir /etc/redis
Next, copy over the sample Redis configuration file that came included with our downloaded Redis source archive file :
sudo cp /tmp/redis-stable/redis.conf /etc/redis
Now Open the file with your preferred text editor to make a few changes to the configuration file :
sudo nano /etc/redis/redis.conf
Inside the file, find the supervised
directive. This directive will help us to run Redis as a service . By default, it is the value set as no. We need to change as systemd since we are running it on ubuntu, which uses systemd init system
Next, find the dir
directive. This option specifies the directory which Redis will use to dump persistent data. You need to change this to a location where Redis will have write permissions and which isn’t viewable by normal users.
Use the /var/lib/redis
directory for this; you will create this directory and adjust its permissions later in Step 4:
Save and close the file when you are finished.
Step 3 — Creating a Redis systemd Unit File
In order to get more control to manage Redis, we can create a systemd unit file that will allow it to function as a systemd service. This will also have the benefit of making it easy to enable Redis to start up whenever our server boots.
Create and open the /etc/systemd/system/redis.service
file to get started:
sudo nano /etc/systemd/system/redis.service
Add the below contents to this 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
- [
Unit]
section by adding a description of the service and defining a requirement that networking must be available before it is started - The
[Service]
section is where you specify the service’s behavior. For security purposes, you should not run this service as root. You should instead use a dedicated user and group and, for simplicity, you can call both of these redis.To start the service, you just need to call theredis-server
binary and point it at your configuration. To stop it, use the Redisshutdown
command, which you can execute with theredis-cli
binary. Also, since it’s desirable to have Redis recover from failures whenever possible, set theRestart
directive toalways
[Install]
section is where we define the systemd target that the service should attach to if it’s enabled (meaning that it’s configured to start at boot)
Save and close the file when you are finished.
The Redis systemd unit file is all set. Before using this file, we must create the dedicated user and group we referenced in the [Service] section and grant them the permission they need to function.
Step 4 — Creating the Redis User, Group, and Directories
The last things we need to do before starting and testing Redis are to create the user, group, and directory that we referenced in the previous two files.
Begin by creating the redis user and group. We can do this in a single command by typing:
sudo adduser --system --group --no-create-home redis
Next, create the /var/lib/redis
directory (which is referenced in the redis.conf
file we created in Step 2) by typing:
sudo mkdir /var/lib/redis
Give the redis
user and group ownership over this directory:
sudo chown redis:redis /var/lib/redis
Finally, adjust the permissions so that regular users cannot access this location:
sudo chmod 770 /var/lib/redis
Now we have put all setup Redis needs to function in place. We are now ready to start the Redis service and test its working
Step 5 — Starting and Testing Redis
Start the systemd service by typing:
sudo systemctl start redis
Check that the service has no errors by running:
sudo systemctl status redis
This will produce output similar to the following:
● redis.service – Redis In-Memory Data Store
Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2020-07-24 13:57:37 UTC; 7s ago
Main PID: 23149 (redis-server)
Tasks: 5 (limit: 2329)
CGroup: /system.slice/redis.service
└─23149 /usr/local/bin/redis-server 127.0.0.1:6379
Jul 24 13:57:37 ip-10-0-6-159 redis-server[23149]: 23149:M 24 Jul 2020 13:57:37.201 # Server initialized
Now test our redis service is working properly,connect to the Redis server with the command-line client:
redis-cli
You will get a prompt like:
127.0.0.1:6379
test connectivity by typing ping
127.0.0.1:6379 ping
This should return:
PONG
Next, check that you can set keys by typing:
127.0.0.1:6379 set test "It's working!"
Output
ok
Retrieve the test
value by typing:
127.0.0.1:6379 get test
You should be able to retrieve the value you stored:
Output :
"It's working!"
Now we confirmed that Redis is working.Exit from the Redis prompt to our shell gain by running:
127.0.0.1:6379 exit
Now we would like to start Redis automatically on server boot up,so enable the systemd service :
sudo systemctl enable redis
With that, our Redis installation is fully operational.
Conclusion
In this tutorial, we installed, compiled, and built Redis latest stable version from its source code, configured it to run as a systemd service, and we validated that our Redis installation is functioning correctly