How to Install and Secure Redis on Ubuntu
To install Redis on Ubuntu, there are three different sources to choose from, and because the license of Redis has changed, the choice matters. This tutorial helps you understand all three, install the one you pick, and then move on to securing it, a step many tutorials skip.
Before you install Redis on Ubuntu: which one?
| Source | Ubuntu 24.04 | Ubuntu 26.04 | License |
|---|---|---|---|
Ubuntu's redis-server package |
Redis 7.0.15 (universe) | Redis 8.0.5 (universe) | 7.0: BSD-3-Clause; 8.0: RSALv2, SSPLv1 or AGPLv3 |
| Official packages.redis.io repository | Latest Redis 8 release | Latest Redis 8 release | Redis 8: RSALv2, SSPLv1 or AGPLv3, your choice |
Valkey (valkey-server) |
Valkey 7.2 (universe) | Valkey 9.0 (main) | BSD-3-Clause |
Background information: Redis abandoned the BSD license with version 7.4. The option of using it under AGPLv3 appeared in Redis 8. Valkey, the community fork of Redis 7.2, operates under the Linux Foundation and retains the BSD license. Also, it has the same protocol and can work with the current Redis clients. On Ubuntu 26.04, it is the version in the main repository.
In case there are no requirements for any licenses, one can choose the package provided by Ubuntu itself. Otherwise, for the latest version and for new functionality, it is better to install the package via the official repository. If you want a permissive license, choose Valkey.
Option 1: Ubuntu's package
sudo apt update
sudo apt install redis-server
redis-cli ping
“PONG” indicates that the server is active. Ubuntu runs the server as a redis-server systemd service.
Option 2: the official Redis repository
sudo apt-get install lsb-release curl gpg
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
sudo chmod 644 /usr/share/keyrings/redis-archive-keyring.gpg
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
sudo apt-get update
sudo apt-get install redis
The lsb_release -cs part fills in your release's code name, so everything works on 24.04 as well as 26.04. The updates will come with apt upgrade; you can learn more about this from our article on what apt is in Linux.
Option 3: Valkey
sudo apt update
sudo apt install valkey-server
valkey-cli ping
Your client applications will interact with it exactly like they did with Redis, at the same port 6379.
Lock Redis down
Redis is engineered for secure networks. A wide-open Redis without a password is one of the most common security issues that will compromise servers; therefore, please look at the following settings in /etc/redis/redis.conf (/etc/valkey/valkey.conf for Valkey) before you put any data in it.
Listen on localhost only. The default is:
bind 127.0.0.1 -::1
protected-mode yes
Leave this unless any other server needs access. In that case, bind to a private IP address and accept just the connecting machine in the firewall configuration. Under no circumstances, expose port 6379 to the internet. Identify the listeners using sudo ss -tlnp | grep 6379; our Linux network commands cheat sheet interprets this output.
Set up a password. Define one in the configuration file:
requirepass a-long-random-password
Afterward, clients authenticate with AUTH. For several applications, create a separate ACL user for each one, with only the commands and keys it needs.
Cap memory. Redis keeps all its contents in RAM. Without a cap, it keeps growing until the system starts swapping or kills processes:
maxmemory 2gb
maxmemory-policy allkeys-lru
allkeys-lru should be the choice for a cache. noeviction will have to be used if your data should never be lost, and the server sized properly.
Fix the overcommit warning. An entry in the log about vm.overcommit_memory means background saves can fail under memory pressure. Set it permanently:
echo 'vm.overcommit_memory = 1' | sudo tee /etc/sysctl.d/99-redis.conf
sudo sysctl --system
Apply config updates with sudo systemctl restart redis-server (or valkey-server).
Persistence in one paragraph
As configured by default, Redis will snapshot (RDB) every so often to the disk, so that a crash might have you losing some writes made in the last couple of minutes. If the goal is to be able to recover from nearly every write even in case of crashes, consider enabling Append Only mode with appendonly yes. For a pure cache, you can turn persistence off entirely.
Redis' speed heavily relies on the server's available memory. The plans in our OrbitServers VPS range run from 8GB on Starter to 48GB on Mega, so you can size the server to your dataset.
Was this article helpful?
Written by Julius · Product Manager