Quantcast

How to Install Redis on Ubuntu 22.04 LTS

Redis is an open-source (BSD licensed), in-memory key-value data structure store used as a database, cache, and message broker. Redis supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyper log logs, geospatial indexes, and streams. Redis also provides high availability with Redis Sentinel software logic, creating automatic partitioning across Redis nodes with Redis Cluster.

In the following tutorial, you will learn how to install Redis on Ubuntu 22.04 LTS Jammy Jellyfish using the terminal command line and some basic setup instructions to get you started.

Update Ubuntu

Before installing Redis, start by making sure your packages on your system are up-to-date to avoid any conflicts during the installation using the following command.

sudo apt update && sudo apt upgrade

Install Redis

Ubuntu Repository Method

By default, Redis 5 series comes included in the Ubuntu 22.04 repository, which can be installed with the following command:

sudo apt install redis-server

RedisLabs PPA Method

The second method is to import the “Redis Labs” team, which boasts the latest stable version of Redis. As Ubuntu 22.04 LTS ages, the standard Ubuntu repository version can often be left far behind, ensuring you have the latest version.

First, import the PPA using the following command.

sudo add-apt-repository ppa:redislabs/redis -y

Update your sources list to reflect the new PPA.

sudo apt-get update

Now install Redis; you may see an upgrade if you have Redis installed. I would still advise using the install command.

sudo apt install redis

Next, verify the status and make sure Redis is running and, more importantly, with no errors:

systemctl status redis-server

Example output:

How to Install Redis on Ubuntu 22.04 LTS

Note that Reddis actively listens to localhost on the default port 6379. To confirm this type, the following:

ps -ef | grep redis

Example output:

How to Install Redis on Ubuntu 22.04 LTS

While testing everything is working and operational, it is good to connect to your Redis service and then perform a ping test.

To perform the test, enter the following command:

redis-cli

Once connected, your terminal will display (127.0.0.1:6379). Now ping the Redis service as follows:

ping

Example output:

How to Install Redis on Ubuntu 22.04 LTS

Type the following to exit the Redis instance:

exit

Congratulations, you have installed Redis on your Ubuntu operating system and verified it is operational.

Next, you can configure Redis.

How to Configure Redis

Redis can be configured in a few ways. The most notable action of why people use Redis is for caching purposes. To do this, you need to open the “/etc/redis/redis.conf” file using nano editor.

sudo nano /etc/redis.conf

Configure Max Memory

Now, add the following to the end of the file. Note that you can change the memory value to whatever you like or, more importantly, optimal for your web application and server hardware.

maxmemory 500mb 
maxmemory-policy allkeys-lru

As you can see, the setting in the guide has 500MB dedicated to Redis as it is on a dedicated host with lots of RAM to spare. Once the 500MB is exhausted, Redis removes any key as per the LRU algorithm.

Configure Network Access

Another option is to listen to all services or set an IP address/subnet if you like your Redis service.

First, find line 69 in the configuration file.

First, to listen to all network interfaces, Comment “#” the line bind to IP.

Example:

# bind 127.0.0.1 ::1

Alternative Method:

bind 0.0.0.0/0

Note, make sure your internal network is trustworthy and has appropriate security controls.

To bind to an IP address, make sure it is a static IP address.

Example:

bind 192.150.5.2

To bind a network subnet.

Example:

bind 192.150.5.0/24

Note, it is highly suggested to set a password when using subnet or access to all interfaces to listen to.

Configure Password

Another security feature to further harden Redis is to set a password on the Redis instance.

Navigate to line 507, uncomment the “# requiredpass” line, and set a password.

Example:

requiredpass APASSWORD

Make sure this password is robust, numbers, letters, special symbols, and capitals randomized as Redis servers can be bruted forced on a decent box very well.

Next, when invoking the Redis-CLI, use the following command with the password that was set for the user.

Example:

auth THEPASSWORDSET

“THEPASSWORDSET” is the password that was created.

When users fail to log in, they will see the following error message.

(error) NOAUTH Authentication required.

When a user successfully logs in, they will see the following message.

OK

Once done, save your changes CTRL+O then exit CTRL+X. Now restart the Redis service by typing:

sudo systemctl restart redis-server

Configure UFW Rules

If you use the UFW firewall installed by default on Ubuntu distributions, you will need to create allow rules on the TCP port 6379. Depending on your installation and requirements if using singular or in a cluster network, some examples are below:

Additional network IP server instance:

sudo ufw allow proto tcp from <ip address> to any port 6379

Cluster network with many instances:

sudo ufw allow proto tcp from <ip address>/24 to any port 6379

Note that the second UFW rule is a subnet rule; ensure the internal network is secure and trustworthy before allowing it.

Please see our tutorial for more information about installing and configuring UFW on Ubuntu 22.04 LTS Jammy Jellyfish.

Now, as you tested at the start of the guide by pinging your Redis service to make sure it was operational, you can try the firewall rules and changes in IP by using the “redis-cli” command:

redis-cli -h  <ip address> ping

If setup correctly, the output should be:

pong

Comments and Conclusion

In the guide, you have learned how to install Redis on Ubuntu 22.04 Jammy Jellyfish by using its default repositories through the apt package manager. You have learned how to test the service by pinging it and changing the memory limit and network interface to suit various setups.

To find more information about managing your Redis installation, visit the Redis documentation page.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!