Featured image of post How to Install Redis: A Step-by-Step Guide

How to Install Redis: A Step-by-Step Guide

Learn how to install Redis on Ubuntu, macOS, and Windows with our detailed step-by-step guide. Follow these Redis setup instructions for optimal performance and configuration.

Redis is a robust, open-source, in-memory data structure store used extensively for caching, real-time analytics, and as a message broker. Whether you’re new to Redis or looking to refine your current setup, this guide will provide you with clear and practical instructions for installing Redis on various platforms like Linux, MacOS, and Windows.

What is Redis?

Redis is an open source, in-memory data structure store used as a distributed database, cache, and message broker. It supports various data structures such as strings, lists, sets, sorted sets, hashes, bitmaps, hyperloglogs, geospatial indexes, and streams.

Redis offers low-latency reads and writes by holding all data in memory. This makes it particularly suitable for use cases requiring a cache. Redis is the most popular NoSQL database and one of the most popular databases overall

Key Features of Redis

  • In-memory data structure store: Redis stores data in memory, providing extremely fast access times.
  • Supports multiple data types: Redis supports strings, lists, sets, sorted sets, hashes, bitmaps, hyperloglogs, streams, and more.
  • Atomic operations: All operations in Redis are atomic, meaning they cannot be interrupted by other clients.
  • Durability: Redis supports optional durability through persistence mechanisms like RDB and AOF.
  • Replication: Redis supports master-slave replication to improve performance and reliability.
  • Clustering: Redis introduced clustering in 2015 to distribute data across multiple nodes.

Redis is a powerful, open source, in-memory data structure store that provides extremely fast access to data. Its support for various data types, durability, replication, and clustering make it a popular choice for building high-performance, scalable applications that require real-time access to data.

Use Cases

Redis is commonly used for:

  • Caching: Caching frequently accessed data to improve application performance
  • Session management: Storing session data for high-concurrency applications
  • Real-time applications: Providing real-time access to data due to its high performance
  • Leaderboards and rankings: Storing and updating real-time leaderboards
  • Pub/Sub messaging: Implementing a message broker for pub/sub use cases

Redis Installation Guide

Here’s a detailed Redis installation guide covering different operating systems:

1. Installing Redis on Ubuntu

Step 1: Update Your System

Ensure your package list is up to date with the following command:

1
sudo apt update

Step 2: Install Redis

Execute the command to install Redis:

1
sudo apt install redis-server

Step 3: Start and Enable Redis

Initialize the Redis service and configure it to start on boot:

1
2
sudo systemctl start redis-server
sudo systemctl enable redis-server

Step 4: Verify Installation

Check that Redis is working properly with:

1
redis-cli ping

You should receive PONG in response.

2. Redis Setup on macOS

Step 1: Install Homebrew

If Homebrew is not installed, you can install it with:

1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 2: Install Redis

Use Homebrew to install Redis:

1
brew install redis

Step 3: Start Redis

Start Redis using Homebrew services:

1
brew services start redis

Step 4: Verify Installation

Confirm Redis is running with:

1
redis-cli ping

3. Installing Redis on Windows

Windows users can either use Windows Subsystem for Linux (WSL) or a native installation method.

a. Using Windows Subsystem for Linux (WSL)

To install Redis on Windows, you’ll first need to enable WSL2 (Windows Subsystem for Linux). WSL2 lets you run Linux binaries natively on Windows. For this method to work, you’ll need to be running Windows 10 version 2004 and higher or Windows 11.

1
wsl --install

Once you’re running Ubuntu on Windows, you can follow the steps detailed at Install on Ubuntu/Debian to install recent stable versions of Redis from the official packages.redis.io APT repository. Add the repository to the apt index, update it, and then install:

1
2
3
4
5
6
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

sudo apt-get update
sudo apt-get install redis

Lastly, start the Redis server like so:

1
sudo service redis-server start

b. Native Installation

Download Redis for Windows from trusted sources like Memurai or the Microsoft Store. After that, extract the downloaded archive and navigate to the extracted folder and run redis-server.exe. Open a command prompt and run:

1
redis-cli ping

You should see PONG if Redis is working.

Setting Up Redis

After installation, it’s important to configure Redis to fit your needs.

Locate and Open the Configuration File

Redis configuration is typically managed through the redis.conf file:

  • Linux/macOS: /etc/redis/redis.conf
  • Windows: In the installation directory.

Edit the configuration file with a text editor. For Linux/macOS, use:

1
sudo nano /etc/redis/redis.conf

Set a Password

To secure your Redis instance, set a password:

1
requirepass yourpasswordhere

Configure Persistent Storage

Redis offers several persistence options. To configure snapshotting, find the save directive in redis.conf:

1
2
3
save 900 1
save 300 10
save 60 10000

These settings dictate when Redis saves snapshots of your data to disk.

Limit Memory Usage

Set a memory limit to prevent Redis from using too much system memory:

1
maxmemory 256mb

You can also configure Redis to use different eviction policies:

1
maxmemory-policy allkeys-lru

Restart Redis

After making changes to the configuration file, restart Redis to apply them:

1
2
sudo systemctl restart redis-server   # Linux
brew services restart redis           # macOS

Verify the Configuration

Check the new settings by running:

1
redis-cli config get *

Conclusion

This guide covers the installation and setup of Redis on various operating systems, ensuring you can get Redis up and running with the configuration tailored to your requirements. For further customization and advanced configuration, refer to the official Redis documentation.

For more insights and tutorials, visit CodeNoun.

Related Article