Redis Installation

Redis supports multiple operating systems. This lesson covers detailed installation steps on Linux, Windows, and Mac.

Installing Redis on Linux

Linux is the best platform for running Redis. For production environments, Linux is recommended.

Ubuntu/Debian Installation

Method 1: Using apt (Recommended for Beginners)

BASH
# Update package list
sudo apt update

# Install Redis server
sudo apt install redis-server -y

# Start Redis service
sudo systemctl start redis-server

# Enable auto-start on boot
sudo systemctl enable redis-server

Method 2: Compile from Source (Recommended for Production)

BASH
# Install build dependencies
sudo apt update
sudo apt install build-essential tcl -y

# Download Redis source code (using 7.0 as example)
cd /tmp
wget https://download.redis.io/redis-7.0.11.tar.gz

# Extract
tar xzf redis-7.0.11.tar.gz
cd redis-7.0.11

# Compile
make

# Test compilation (optional, takes a few minutes)
make test

# Install to system directories
sudo make install
💡 Benefits of source installation: You can specify the install path, enable specific modules, and optimize compilation parameters.

CentOS/RHEL Installation

Method 1: Using yum

BASH
# Install EPEL repository
sudo yum install epel-release -y

# Install Redis
sudo yum install redis -y

# Start Redis
sudo systemctl start redis

# Enable auto-start on boot
sudo systemctl enable redis

Method 2: Compile from Source

BASH
# Install build dependencies
sudo yum groupinstall "Development Tools" -y
sudo yum install tcl -y

# Download, compile, install (same as Ubuntu)
cd /tmp
wget https://download.redis.io/redis-7.0.11.tar.gz
tar xzf redis-7.0.11.tar.gz
cd redis-7.0.11
make
sudo make install

Manually Starting Redis (After Source Installation)

BASH
# Start Redis server (foreground)
redis-server

# Run in background (recommended)
redis-server --daemonize yes

# Start with a config file
redis-server /etc/redis/redis.conf
⚠️ Caution: After source installation, you need to manually create a systemd service file or use supervisor to manage Redis.

Installing Redis on Windows

Redis is not officially supported on Windows, but Microsoft maintains a Windows port.

Step 1: Download Redis

Visit GitHub to download the latest version:

TEXT
https://github.com/microsoftarchive/redis/releases

Download Redis-x64-3.2.100.msi (or a newer version)

Step 2: Install

  1. Double-click the msi file
  2. Choose an installation path (e.g., C:\Program Files\Redis)
  3. Check "Add Redis to PATH"
  4. Check "Install as Windows Service"
  5. Click Install to complete

Step 3: Verify

Open Command Prompt:

CMD
redis-cli ping

If it returns PONG, the installation was successful.

WSL (Windows Subsystem for Linux) can run the native Linux version of Redis.

POWERSHELL
# Install WSL (PowerShell as Administrator)
wsl --install

# After reboot, install Redis in WSL
sudo apt update
sudo apt install redis-server -y

# Start Redis
sudo service redis-server start
💡 Benefits of WSL: Runs the native Linux version of Redis for better performance and full feature support.

Method 3: Using Docker

POWERSHELL
# Pull Redis image
docker pull redis

# Run Redis container
docker run -d -p 6379:6379 --name redis redis

# Connect to Redis
docker exec -it redis redis-cli

Installing Redis on Mac

BASH
# Install Homebrew (if not already installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install Redis
brew install redis

# Start Redis service
brew services start redis

# Or start in foreground
redis-server

Method 2: Compile from Source

BASH
# Install build tools (if not already installed)
xcode-select --install

# Download, compile, install
cd /tmp
wget https://download.redis.io/redis-7.0.11.tar.gz
tar xzf redis-7.0.11.tar.gz
cd redis-7.0.11
make
sudo make install

Installing Redis with Docker (Cross-platform)

Docker is the easiest way to install Redis, suitable for development and testing.

Pulling the Image

BASH
# Pull the latest version
docker pull redis

# Pull a specific version
docker pull redis:7.0
docker pull redis:6.2

Running the Container

BASH
# Basic run
docker run -d -p 6379:6379 --name redis redis

# Run with password
docker run -d -p 6379:6379 --name redis redis --requirepass yourpassword

# Persistent storage
docker run -d -p 6379:6379 -v /data/redis:/data --name redis redis --appendonly yes

# Use a config file
docker run -d -p 6379:6379 -v /path/to/redis.conf:/etc/redis/redis.conf --name redis redis /etc/redis/redis.conf

Connecting to Redis

BASH
# Enter Redis CLI
docker exec -it redis redis-cli

# Connect with password
docker exec -it redis redis-cli -a yourpassword
💡 Benefits of Docker: Isolated environment, quick deployment, easy to test different versions.

Verifying the Installation

No matter which installation method you used, verify that Redis is properly installed.

1. Check Redis Service Status

Linux (systemd):

BASH
sudo systemctl status redis
# or
sudo systemctl status redis-server

Mac (Homebrew):

BASH
brew services list

Windows:

POWERSHELL
# Check service status
Get-Service Redis

2. Connect with redis-cli

BASH
# Connect to local Redis
redis-cli

# Test after connecting
 ping
PONG

 set test "Hello Redis"
OK

 get test
"Hello Redis"

 exit

3. Check Redis Version

BASH
redis-server --version
# Output: Redis server v=7.0.11 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64

redis-cli --version
# Output: redis-cli 7.0.11

4. Check Redis Info

BASH
redis-cli info server

Key information:

TEXT
redis_version:7.0.11
redis_mode:standalone
os:Linux 5.15.0 x86_64
tcp_port:6379
uptime_in_seconds:3600

Redis Directory Structure

Directory structure after source installation:

TEXT
/usr/local/bin/
├── redis-server      # Redis server
├── redis-cli         # Redis command-line client
├── redis-benchmark   # Redis performance benchmark tool
├── redis-check-rdb   # RDB file checker
└── redis-check-aof   # AOF file checker

Config file locations:

Common Installation Issues

Issue 1: Port Already in Use

Error message:

TEXT
Creating Server TCP listening socket *:6379: bind: Address already in use

Solution:

BASH
# Check port usage
sudo lsof -i :6379
# or
sudo netstat -nltp | grep 6379

# Stop the process using the port
sudo kill -9 <PID>

# Or start with a different port
redis-server --port 6380

Issue 2: Insufficient Permissions

Error message:

TEXT
Can't open the log file: Permission denied

Solution:

BASH
# Create Redis user
sudo useradd -r -s /bin/false redis

# Change data directory permissions
sudo chown -R redis:redis /var/lib/redis
sudo chown -R redis:redis /var/log/redis

Issue 3: Insufficient Memory

Error message:

TEXT
OOM command not allowed when used memory > 'maxmemory'.

Solution:

BASH
# Modify config file to set max memory
maxmemory 256mb

# Or specify at startup
redis-server --maxmemory 256mb

Issue 4: Windows Firewall

Solution:

POWERSHELL
# Add firewall rule (PowerShell as Administrator)
New-NetFirewallRule -DisplayName "Redis" -Direction Inbound -LocalPort 6379 -Protocol TCP -Action Allow

Startup Parameters

BASH
redis-server [config file] [options]

Common options:
--port 6379              # Specify port
--bind 0.0.0.0          # Bind IP address
--daemonize yes         # Run in background
--logfile /var/log/redis.log  # Log file
--dir /var/lib/redis    # Data directory
--maxmemory 256mb       # Max memory
--requirepass password  # Set password
--appendonly yes        # Enable AOF persistence

❓ FAQ

Q Which installation method should I use for production?
A Compiling from source on Linux is recommended so you can customize compilation parameters and optimize performance. Package manager versions may be outdated.
Q Can Windows be used for production?
A Not recommended. Redis is not officially supported on Windows, and the Microsoft-maintained version has limited features. Use Linux or Docker instead.
Q How do I install multiple Redis instances?
A Use different ports and config files: redis-server --port 6380 --config-file /etc/redis/redis-6380.conf

Q: How do I uninstall Redis? A:

  • apt: sudo apt remove redis-server
  • yum: sudo yum remove redis
  • Source: sudo make uninstall (in the source directory)
  • Homebrew: brew uninstall redis

📖 Summary

📝 Exercises

  1. Installation practice: Install Redis on your system and verify it works
  2. Version check: Check your Redis version and learn its key features
  3. Multi-instance practice: Try starting two Redis instances on ports 6379 and 6380

Next Lesson

In the next lesson, we will learn Redis Quick Start — starting Redis and running your first commands.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏