How to Set Up Redis in Node.js on Ubuntu 20.04/22.04/24.04 LTS
Learn how to install and configure Redis with Node.js on Ubuntu 20.04, 22.04, or 24.04 LTS. This step-by-step guide ensures high-performance caching and data storage for your Node.js apps.

Redis is a powerful in-memory data store, widely used for caching, pub/sub messaging, and real-time analytics. In this tutorial, you’ll learn how to install Redis and integrate it with Node.js on Ubuntu 20.04, 22.04, or 24.04 LTS.
Step 1: Update Your Ubuntu System
sudo apt update && sudo apt upgrade -y Step 2: Install Redis Server
sudo apt install redis-server -y Step 3: Verify Redis Installation
redis-cli ping Expected output: PONG
Step 4: Configure Redis to Start on Boot
Open the configuration file:
sudo nano /etc/redis/redis.conf Find and set:
supervised systemd Then restart Redis:
sudo systemctl restart redis.service sudo systemctl enable redis Step 5: Secure Redis (Optional but Recommended)
Bind Redis to localhost:
In/etc/redis/redis.conf:bind 127.0.0.1Require a password:
requirepass your_strong_password
Restart Redis again:
sudo systemctl restart redis Step 6: Install Node.js and NPM
For Ubuntu 20/22/24 LTS:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt install -y nodejs Verify:
node -v npm -v Step 7: Create Node.js Project and Install Redis Client
mkdir redis-node && cd redis-node npm init -y npm install redis Step 8: Connect Node.js to Redis
Index.js
const redis = require('redis');
const client = redis.createClient({ url: 'redis://:your_strong_password@127.0.0.1:6379' });
client.connect();
client.on('connect', () => {
console.log('Connected to Redis');
}); client.set('message', 'Hello Redis!'); client.get('message').then(console.log); Run:
node index.js Output:
Connected to Redis Hello Redis! Step 9: Test Redis Persistence
Restart Redis:
sudo systemctl restart redis Run your Node.js script again. The data should persist depending on your Redis persistence settings (RDB or AOF).
Conclusion
You’ve successfully set up Redis with Node.js on Ubuntu 20.04, 22.04, or 24.04 LTS. This setup allows your applications to perform faster with in-memory caching and real-time data handling.
Share this article
Admin User
Admin User
Related Posts

The Enduring Importance of Data Structures & Algorithms
In an age where AI seems to handle everything from chatbots to complex simulations, the fundamentals of Data Structures & Algorithms (DSA) remain the cornerstone of every efficient, scalable, and innovative software solution. Here’s why, even in 2025, DSA skills are non-negotiable for any developer aiming to thrive.

Average Salaries in India (2025) for Developers & Designers Based on Experience
Planning a career in tech or design? This detailed guide breaks down the average salary in India for developers (Node.js, Java, Python, Django) and designers based on experience—from fresher to 4+ years.

Ubuntu 24.04 LTS Installation Guide for Any PC – From BIOS to Developer Setup
Want to install Ubuntu 24.04 LTS on your PC? This universal guide covers BIOS setup, swap memory recommendations, partitioning, and essential post-install commands for all systems.

React.js & Next.js Explained: Frameworks, Libraries, Hooks & Expert Roadmap (2025 Guide)
Learn why Next.js is the go-to React framework, explore React libraries, deep-dive into hooks, and discover how to build your career in React—including real developer salaries in the U.S. and India.
