In this article , We have explained how to Install Redis on Ubuntu.

We have also published article which covers Installation and Configuration of Database engines on Ubuntu operating systems.Check it out.

Install MySQL & Configure on Ubuntu

Install MongoDB on Ubuntu

Install PostgreSQL on Ubuntu

Okay , Lets get started with the installation of Redis on Ubuntu operating system.

What is Redis?

Redis is an open source , in-memory data structure store, used as a database, cache and message broker.

It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams.

In addition to being fully in-memory, Redis enables data persistence and high availability through replication and backups.

Prerequisites

Make sure you have root or sudo privileges on the system where you want to install redis.

Redis packages comes with the default Ubuntu software repository , In order to install the latest version redis ,

Update the Ubuntu APT package software repository to the latest available versions using the below command.

sudo apt-get update

Lets go ahead and install Redis.

Installing Redis on Ubuntu

As the redis packages are available on the default apt repository , Run the below command to install the redis packages which will install all of its dependencies.

sudo apt-get install redis-server
Install redis on Ubuntu
Install redis on Ubuntu

During installation If you face the below the error as shown below.

Errors were encountered while processing:
redis-server

Due to the above error , The redis-server packages will be installed but it will fail to start the service.

To fix this issue , Go to /etc/redis folder and then open redis.conf file

We need to alter redis.conf to force it to use IPv4 only.

sudo nano /etc/redis/redis.conf

Search for bind and then remove ::1 IPv6 loopback address from the bind option.

Install redis on Ubuntu

Save and close the file.

Now if you re-run the below command , You shouldn’t be facing any issue.

sudo apt-get install redis-server
Install redis on Ubuntu

By default , While installing redis It will automatically configure systemd service file for us to easily manage the redis service.

You can find the redis.service file under /etc/systemd/system/ folder.

cat /etc/systemd/system/redis.service
Install redis on Ubuntu

To check the status of redis service , You can any one of the below commands

sudo systemctl status redis
sudo systemctl status redis-server.service
sudo systemctl status redis.service

You will get the below response.

Install redis on Ubuntu

To enable the redis service to automatically start on system boot up , use the below command.

sudo systemctl enable redis-server.service
Install redis on Ubuntu

Using the below command , You can restart the redis service.

sudo systemctl restart redis-server

To check the version of Redis installed on the ubuntu system.

sudo redis-server --version
sudo redis-cli --version
Install redis on Ubuntu

By default , Redis will be running on localhost and listening on port 6379.

Using the below command , You can check the same.

sudo netstat -nltp |grep redis
Install redis on Ubuntu

To allow connections from the remote hosts , We need to edit redis.conf file and configure as shown below.

sudo vi /etc/redis/redis.conf

Replace bind 127.0.0.1 to 0.0.0.0 , Save and close the file.

Install redis on Ubuntu

For the changes the take effect , We need to restart the redis service.

sudo systemctl restart redis-server

Now If you run the below command , You can see that redis will accept connections from any host.

sudo netstat -nltp |grep redis
Install redis on Ubuntu

Connecting Redis

We use redis-cli , the Redis command line interface , using which we can send command to the redis server and we can read the replies sent by the server from the terminal.

Run the below command to test the connectivity with the Redis server.

redis-cli

And run ping to test the connectivity from the redis shell.You should get a response as PONG.

Install redis on Ubuntu

From this , We can confirm that the redis server is working as expected.

The more useful redis-cli command.

From the redis shell , run info to get the overall status of the redis server.

127.0.0.1:6379> info
Install redis on Ubuntu

You can check the details such as Number of clients connected with the redis server , Replication status , Memory & CPU used , Cluster status and much more.

To exit from the redis-cli shell , Execute the command.

127.0.0.1:6379> exit

Running Redis on Custom Port

As we know that Redis by default runs on the port 6379.For security reasons , If you wish to run redis server on different port.

Open the redis.conf file.

sudo nano /etc/redis/redis.conf

and change the port number.

Install redis on Ubuntu

After changing the port , Save and close the file and restart the redis service for the changes to take effect.

Now If you run the below command , You can see that the redis server is running on the port that you have configured.

sudo netstat -nltp |grep redis
Install redis on Ubuntu

Removing Redis from Ubuntu

If you wish to remove redis from the ubuntu operating system after testing , You can run the below command.

sudo apt-get purge redis-* -y

This will remove all the files and folders used by the redis server.

We have successfully installed and configured Redis on Ubuntu.

That’s all for this tutorial.Thanks for reading.

Hope you find it helpful.