Install Redis on Ubuntu
Learn to install Redis on Ubuntu step by step — from prerequisites to running the server, with troubleshooting tips and next steps in the Redis track.
Focus: install redis on ubuntu
You need a fast, in-memory data store for caching, real-time analytics, or session management, but you're staring at a fresh Ubuntu server wondering where to start. Without a clear installation path, you risk broken dependencies, misconfigured services, or a security nightmare. This lesson walks you through the official, production-ready way to install Redis on Ubuntu — so you can move from zero to redis-cli ping → PONG in under 10 minutes.
The problem this lesson solves
Redis is the go‑to cache and message broker for modern applications — but getting it onto Ubuntu cleanly isn't always obvious. Package managers may deliver an ancient version, manual compilation can lead to scattered binaries, and skipping basic security leaves your instance open to remote attacks. This lesson addresses all three: you'll learn how to install a recent, stable Redis release via apt, verify it works, and configure it for safe use. Without this step, every subsequent lesson on caching, TTLs, and pub/sub is built on a shaky foundation.
Core concept / mental model
Think of Redis as a lightning‑fast dictionary living in RAM. Installing Redis on Ubuntu is like placing that dictionary on a shelf where both your application and command line can reach it. The installation process:
- Adds the official Redis repository — ensures you get the latest stable version, not the OS's outdated one.
- Installs the
redis-serverpackage — this includes the server binary, the CLI client (redis-cli), and systemd service files. - Starts the service — brings the in‑memory store to life and makes it persist across reboots.
- Verifies connectivity — you
pingthe server to confirm it's alive.
💡 Mental shortcut: Redis on Ubuntu === one
sudo aptcommand away, plus two minor configuration tweaks. The rest is just verifying it works.
How it works step by step
Step 1: Update package list and install dependencies
Before adding any external repo, refresh your local package index. Python‑like idiom: apt update is like calling pip install -r requirements.txt — it prepares your environment.
sudo apt update
Then install lsb-release and curl — these let you add the official Redis GPG key and repository.
sudo apt install -y lsb-release curl
Step 2: Add the official Redis APT repository
Redis maintains its own repo for Ubuntu. You'll download their GPG key and add it to your trusted keys, then add the repository to your sources list.
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /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
Step 3: Install Redis
Now that the repo is configured, update your package list and install redis-server.
sudo apt update
sudo apt install -y redis-server
Step 4: Start and enable the service
Ubuntu uses systemd. Enable Redis to start automatically on boot, then start it immediately.
sudo systemctl enable redis-server
sudo systemctl start redis-server
Step 5: Verify installation
Use redis-cli to ping the server. A PONG response confirms everything is running.
redis-cli ping
# Output: PONG
Hands-on walkthrough
Let's put this all together into one clean script you can run on a fresh Ubuntu 22.04 or 24.04 LTS machine.
Complete one‑shot example
Create a file install-redis.sh with:
#!/usr/bin/env bash
set -euo pipefail
# Update packages
sudo apt update
sudo apt install -y lsb-release curl
# Add Redis repo
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /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
# Install Redis
sudo apt update
sudo apt install -y redis-server
# Enable and start
sudo systemctl enable redis-server
sudo systemctl start redis-server
# Verify
redis-cli ping
Run it:
chmod +x install-redis.sh
./install-redis.sh
Expected output (after script completes):
PONG
Python example to confirm connectivity
If you want to test from Python (after installing redis-py):
import redis
# Connect to local Redis (default host/port)
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
# Ping the server
print(r.ping()) # Output: True
# Set and get a key
r.set('lesson', 'install-redis-on-ubuntu')
print(r.get('lesson')) # Output: install-redis-on-ubuntu
Compare options / when to choose what
| Installation method | Pros | Cons | Best for |
|---|---|---|---|
| Official APT repo (this lesson) | Latest stable, easy updates, systemd integration | Requires adding a repo | Production & most use cases |
| Ubuntu's default APT repo | No extra repo, simple sudo apt install redis-server |
Often outdated (e.g., Redis 6.x when 7.x is available) | Quick dev environments, testing |
| Compile from source | Absolute latest features, custom compile flags | No package manager integration, manual service setup | Edge cases, custom builds |
| Docker image | Isolated, repeatable, no host pollution | Requires Docker knowledge, network overhead | Containerized deployments, microservices |
💡 Recommendation: For this track, use the official APT repo. It balances freshness with stability and integrates cleanly with systemd.
Troubleshooting & edge cases
Problem: "Failed to start redis-server.service: Unit not found"
Cause: The apt install didn't complete successfully, or you're on an unsupported Ubuntu version (e.g., older than 18.04).
Fix:
# Verify Redis is installed
which redis-server
# If missing, re-run install
sudo apt install --reinstall redis-server
Problem: redis-cli ping hangs or times out
Cause: Redis is bound to 127.0.0.1 only by default, but your CLI might be trying IPv6. Or the service didn't start.
Fix:
# Check service status
sudo systemctl status redis-server
# Test with explicit host
redis-cli -h 127.0.0.1 ping
Problem: "W: GPG error: ... NO_PUBKEY"
Cause: The GPG key wasn't imported correctly.
Fix:
# Re-import the key
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
sudo apt update
Edge case: Already installed a different version from Ubuntu's repo
If you previously ran sudo apt install redis-server without the official repo, you'll have an older version. Remove it first:
sudo apt purge redis-server
sudo apt autoremove
# Then follow the official repo steps above
What you learned & what's next
You now know how to install Redis on Ubuntu using the official repository — adding the GPG key, installing redis-server, enabling the systemd service, and verifying with redis-cli ping. You also learned the difference between official and default repo versions and how to troubleshoot common installation hiccups.
This is the foundation for the entire Redis track. In the next lesson, you'll configure Redis basics: setting a password, binding to a specific interface, and tuning the maxmemory policy to prevent your server from swapping. These next steps are essential before you start building caches or pub/sub pipelines.
🧠 Key insight: A correctly installed Redis is the springboard for everything else — from TTL management to Streams. Take 5 minutes to run the one-liner script above; it'll save you hours of debugging later.
Practice recap
Now that Redis is installed, test your understanding: open a terminal and run redis-cli to enter interactive mode. Type SET mykey "hello" then GET mykey to confirm writes work. Then run INFO server to see version, uptime, and memory stats — this mimics what you'd check in a real deployment.
Common mistakes
- Using
sudo apt install redis-serverwithout adding the official repo — you'll get an old Redis 5 or 6 instead of the latest stable 7.x. - Forgetting to
sudo apt updateafter adding the new repository — you'll install the stale Ubuntu version even though you added the official repo. - Not enabling the service (
sudo systemctl enable redis-server) — Redis won't start automatically after a reboot, causing surprise downtime in production. - Running
redis-cli pingbefore the service is started — you'll get a connection refused error and think the install failed.
Variations
- Use Docker:
docker run --name redis -p 6379:6379 -d redis:7for a containerized, isolated instance ideal for CI/CD or microservices. - Compile from source by cloning the Redis repo and running
make— gives you bleeding-edge features but adds manual service management. - Use Snap:
sudo snap install redis— simpler for development but may have permission quirks and less community support.
Real-world use cases
- Startups using Redis as a session store across multiple web servers with sticky sessions disabled.
- E‑commerce platforms caching product catalog data in Redis to reduce database load during flash sales.
- Real‑time analytics pipelines where Redis streams buffer events before batch processing by Spark or Flink.
Key takeaways
- Use the official APT repository to install the latest stable Redis on Ubuntu — avoid the outdated default package.
- Always enable the systemd service so Redis restarts automatically after a reboot.
- Verify installation with
redis-cli ping— aPONGresponse confirms the server is running. - Check service status with
sudo systemctl status redis-serverto diagnose startup issues. - The official repo installation is the baseline for all future lessons in this Redis track.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.