How to install Redis on Ubuntu 22.04

Welcome to this tutorial where we will learn how to install the Redis server o Ubuntu 22.04.

Redis server is an open source in-memory data store used by many developers as a database, cache, streaming engine, and message broker.

Redis as a real-time data store means Redis’ versatile in-memory data structures let you build data infrastructure for real-time applications requiring low latency and high throughput

Caching and session storage. Redis speed is ideal for caching database queries, complex computations, API calls, and session states.

Streaming and messaging. Streamed data type enables high-speed data ingestion, messaging, event sourcing, and notifications.

Install Redis on Ubuntu 22.04

1. Update system repositories

Before we begin the installation, we need to update our system repositories in order to make system repositories up to date.

$ sudo apt update && apt upgrade -y

2. Add Redis to the apt repository

To install the stable version of Redis we need to install it from the official Redis packages and to do so we need to add an apt repository .

$ curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

Add the downloaded package to the repository by using echo.

$ echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

What follows is to update the system repositories once more for the changes to take effect.

sudo apt update

Lastly is to install Redis with the following command.

sudo apt install redis 

You should get the output something like this.

The following additional packages will be installed:
  redis-server redis-tools
Suggested packages:
  ruby-redis
The following NEW packages will be installed:
  redis redis-server redis-tools
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 1642 kB of archives.
After this operation, 9437 kB of additional disk space will be used.
Do you want to continue? [Y/n] 

3. Testing Redis

If your installation was successful, you can test Redis with the following command.

sudo systemctl status redis-server 

You must see the following output.

● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; disabled; vendor>
     Active: active (running) since Tue 2022-07-26 07:41:00 UTC; 7min ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 24426 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 1033)
     Memory: 2.8M
        CPU: 385ms
     CGroup: /system.slice/redis-server.service
             └─24426 "/usr/bin/redis-server 127.0.0.1:6379" "" "" "" "" "" "" ""

Jul 26 07:41:00 localhost systemd[1]: Starting Advanced key-value store...
Jul 26 07:41:00 localhost systemd[1]: Started Advanced key-value store.
lines 1-15/15 (END)

We can also leverage Redis-cli whenever we want to test whether Redis is working correctly. To use Redis-cli use the following command to ping the connection.

$ redis-cli

root@localhost:~# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

To know that Redis is establishing communication, you will get an output as pong

4. Configuring Redis

By default Redis is accessible from the localhost. it doesn’t allow remote connections. So to enable Redis to accept remote connections, we need to edit the following configuration file.

$ sudo vi /etc/redis/redis.conf

Check the line that begins with bind 127.0.0.1 : : 1 and uncomment it.

bind 127.0.0.1 ::1

Save and exit and make sure you restart the Redis server again for the changes to take effect.

sudo systemctl restart redis-server

To check that the service has actually taken effect, let’s test it with the netstat command

$ sudo netstat -lnp | grep redis

If you find an error that netstat is not installed you can do an install with the following command.

sudo apt install net-tools 

You will see the output like this.

tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      24735/redis-server  
tcp6       0      0 ::1:6379                :::*                    LISTEN      24735/redis-server 

5. Securing Redis

By default, Redis binds to all interfaces and has no authentication systems in place. To make Redis more secure, use the following steps to harden your Redis.

  • Make sure the port Redis uses to listen for connections (by default 6379 and additionally 16379 if you run Redis in cluster mode, plus 26379 for Sentinel) is firewalled so that it is not possible to contact Redis from the outside world.
  • Use the requirepass option in order to add an additional layer of security so that clients will require to authenticate using the AUTH command.
  • Use a configuration file where the bind directive is set in order to guarantee that Redis listens on only the network interfaces you are using.

In Redis password is configured directly by changing the following configuration file.

$ sudo vi /etc/redis/redis.conf

Scroll down to the security section and change the following and uncomment it and place a very strong password

# requirepass foobared

Save the file and exit and make sure you restart the Redis server again.

sudo systemctl restart redis-server

Conclusion

Congratulations, you have installed and configured the Redis server accordingly. For more information, check its rich documentation.

About Kipkoech Sang

I am a technology enthusiast who loves to share gained knowledge through offering daily tips as a way of empowering others. I am fan of Linux and all other things open source.
View all posts by Kipkoech Sang →

Leave a Reply

Your email address will not be published.