Developer

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.

By Admin User
18 views
How to Set Up Redis in Node.js on Ubuntu 20.04/22.04/24.04 LTS

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.1 
  • Require 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.

#Node.js#redis#redis-setup

Share this article

Admin User

Admin User

Admin User

Related Posts

The Enduring Importance of Data Structures & Algorithms
Developer
5 min read

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.

Guest User

Comments