Installation & Initial Setup
Introduction
Before you can build powerful applications with Redis, you need a solid, reliable installation and initial server setup. This lesson guides you through the process of installing Redis on various platforms, configuring basic options, running your first Redis server, and verifying your environment. We’ll focus on practical, step-by-step instructions, code examples, and best practices to ensure a smooth start.
Installing Redis
Redis is supported on Linux, macOS, and Windows (unofficially). Let’s cover the most common installation methods.
Installing on Linux
Option 1: Using Package Manager (Recommended for Ease)
For Ubuntu/Debian:
sudo apt update
sudo apt install redis-server
For CentOS/RHEL (with EPEL repository enabled):
sudo yum install epel-release
sudo yum install redis
Option 2: Building from Source (Latest Features & Customization)
sudo apt update
sudo apt install build-essential tcl # Prerequisites
wget http://download.redis.io/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable
make
sudo make install
Then run the built server:
src/redis-server
Note: Building from source is recommended if you need the latest version or want to customize the build.
Installing on macOS
Using Homebrew:
brew update
brew install redis
Installing on Windows
Redis doesn't officially support Windows, but you can use Memurai (a Redis-compatible Windows solution) or unofficial builds, or run Redis in WSL2 (Windows Subsystem for Linux).
WSL2 Example:
- Install WSL2 and Ubuntu from Microsoft Store.
- Use the Linux installation steps inside WSL2.
Verifying the Installation
Check the installed Redis version:
redis-server --version
Start the Redis server:
redis-server
In another terminal, test the CLI:
redis-cli
127.0.0.1:6379> PING
Output should be:
PONG
Running Redis as a Service
On most Linux distributions, Redis can run as a managed system service.
Start Redis:
sudo systemctl start redis
Enable Redis to start at boot:
sudo systemctl enable redis
Check status:
sudo systemctl status redis
Stop Redis:
sudo systemctl stop redis
Basic Configuration
On first install, Redis uses a default configuration file, usually at /etc/redis/redis.conf or /usr/local/etc/redis/redis.conf.
Common Configuration Options
- Port: Default is
6379. Change withport 6380. - Bind Address: Default is
127.0.0.1. To listen on all interfaces:bind 0.0.0.0 - supervised: Set to
systemdif using systemd-based Linux distributions.
Example (in redis.conf):
port 6380
bind 127.0.0.1
supervised systemd
Restart Redis after changes:
sudo systemctl restart redis
Connecting to Redis
Using Redis CLI
redis-cli -p 6379
127.0.0.1:6379> SET mykey "Hello, Redis!"
OK
127.0.0.1:6379> GET mykey
"Hello, Redis!"
Using Python (redis-py)
Install the client:
pip install redis
Connect and test:
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
r.set('language', 'Python')
print(r.get('language')) # Output: b'Python'
Initial Security Considerations
- Do not bind to 0.0.0.0 in production unless protected by a firewall.
- Set a password in
redis.conf:requirepass your-strong-password - Never run Redis as root.
Common Mistakes and Pitfalls
- Forgetting to restart Redis after configuration changes.
- Opening Redis to the internet without protection: Never bind to public IPs without authentication and firewall.
- Running multiple instances on the same port: Each instance must have a unique port.
- Assuming Redis persists data by default: By default, Redis uses
RDBsnapshots, but improper shutdowns may cause data loss.
Summary
- Redis can be installed via package manager, Homebrew, or from source.
- Always verify your installation and connectivity.
- Use the configuration file for basic options (port, bind address, supervision).
- For production, secure your instance and never expose Redis directly to the internet.
- Always restart Redis after changing configuration.
Quiz
1. What is the default port Redis listens on?
A) 8080
B) 6379
C) 11211
D) 3306
Answer: B) 6379
2. Which command verifies if your Redis CLI can communicate with the server?
A) CHECK
B) CONNECT
C) PING
D) STATUS
Answer: C) PING
3. On Linux, which command starts Redis as a service?
A) redis-cli start
B) redis-server --daemonize yes
C) sudo systemctl start redis
D) start redis
Answer: C) sudo systemctl start redis
4. What is a common security mistake during initial setup?
A) Setting a strong password
B) Binding Redis to 0.0.0.0 without a firewall
C) Running Redis on a non-default port
D) Running Redis as a non-root user
Answer: B) Binding Redis to 0.0.0.0 without a firewall
5. Where is the main Redis configuration file usually located on Ubuntu?
A) /usr/local/redis.conf
B) /etc/redis/redis.conf
C) /var/redis/redis.conf
D) /opt/redis/redis.conf
Answer: B) /etc/redis/redis.conf
Congratulations! You’re now ready to move beyond installation and start using Redis in more advanced operational scenarios.