How to Install Redis on Ubuntu

Step 1: Update Your System

🔄 First, make sure all your packages are up-to-date:

sudo apt update && sudo apt upgrade -y

📋 Example Logs:

Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Reading package lists... Done
Calculating upgrade... Done
        

Step 2: Install Redis

📥 Install Redis from the official repository:

sudo apt install redis-server -y

📋 Example Logs:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  redis-server redis-tools
...
Setting up redis-server (5:5.0.7-2ubuntu0.1) ...
        

Step 3: Verify Redis Installation

🧐 Check the installed version:

redis-server --version

📋 Example Output:

Redis server v=5.0.7 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=00000000

Step 4: Start and Enable Redis Service

🛠️ Start the Redis service and enable it to run at boot:


sudo systemctl start redis
sudo systemctl enable redis
    

📋 Example Logs:

Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /lib/systemd/system/redis.service.

Step 5: Verify Redis is Running

✅ Ensure the Redis service is active:

sudo systemctl status redis

📋 Example Output:

● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2024-11-15 10:30:00 UTC; 1min 10s ago
        

Step 6: Configure Redis

🛡️ Customize Redis settings by editing its configuration file:

sudo nano /etc/redis/redis.conf

Examples of configurations:

# Enable background mode:
daemonize yes

# Set a password:
requirepass yourpassword
    

Save your changes and restart Redis:

sudo systemctl restart redis

Step 7: Test Redis

🔍 Use the Redis CLI to verify everything is working:


redis-cli
ping
AUTH yourpassword
SET mykey "Hello, Redis"
GET mykey
    

📋 Example Output:

PONG
OK
"Hello, Redis"
        

Step 8: Optional – Install Redis from Source

💡 For the latest version, build Redis from source:

  1. 🔧Install dependencies:
    sudo apt install build-essential tcl -y
  2. 📥Download and extract Redis:
    
    curl -O http://download.redis.io/redis-stable.tar.gz
    tar xzvf redis-stable.tar.gz
    cd redis-stable
    make
                
  3. 🧪Test and install:
    make test && sudo make install
  4. ⚙️Set up Redis:
    
    sudo mkdir /etc/redis
    sudo cp redis.conf /etc/redis
    redis-server /etc/redis/redis.conf
                

Step 9: Secure Redis

🔒 Bind Redis to localhost in /etc/redis/redis.conf:

bind 127.0.0.1

Use a firewall to restrict access:

sudo ufw allow from <your-ip> to any port 6379

Step 10: Automate Redis Commands

🤖 Automate Redis tasks with a script:


#!/bin/bash
redis-cli -a yourpassword <

 

Related articles

Setting Up Virtual Hosts in Nginx for Java, HTML, and PHP Applications

Setting Up Virtual Hosts for Java, HTML, and PHP Applications with Nginx Introduction This guide will walk you through configuring...

Aws s3 Interview Questions​

Aws s3 Interview Questions​ Basic Aws s3 Interview Questions What is Amazon S3? Amazon Simple Storage Service (S3) is an...

An Ultimate Guide of How to Manage Linux Systemd Services With Systemctl Command

An Ultimate Guide of How to Manage Linux Systemd Services With Systemctl Command Systemd is the default service manager...

How To Publish Branch in Git

How To Publish Branch in Git Introduction Git is a powerful distributed version control system that enables developers to collaborate...