Before you can start harnessing the power of Redis, you need to set up your development environment. This involves installing the Redis server, and then configuring the necessary client libraries for Node.js and Python.
Prerequisites
Make sure you have the following installed on your system:
- Operating System: Linux (Ubuntu, Debian, CentOS, Rocky Linux, AlmaLinux), macOS, or Windows (using WSL2 or Docker is recommended for Windows).
- Node.js: Version 18.x or later. You can download it from nodejs.org.
- Python: Version 3.8 or later. You can download it from python.org.
npmoryarn: Package manager for Node.js.pip: Package installer for Python.- Docker (Optional but Recommended for Windows/macOS): Simplifies Redis installation. Download from docker.com.
1. Installing Redis Server
There are several ways to install Redis, depending on your operating system.
Option 1: Using Docker (Recommended for cross-platform consistency)
Docker provides the easiest and most consistent way to run Redis, especially on Windows or macOS.
Install Docker Desktop: If you haven’t already, install Docker Desktop for your operating system.
Pull the Redis Image: Open your terminal or command prompt and run:
docker pull redis/redis-stack-server:latestWe’re pulling
redis/redis-stack-serverbecause Redis 8.x has integrated many previously modular features directly. This image provides the most comprehensive set of features.Run Redis Container:
docker run -d --name my-redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack-server:latest-d: Runs the container in detached mode (background).--name my-redis-stack: Assigns a name to your container for easy reference.-p 6379:6379: Maps port 6379 (default Redis port) from your host to the container.-p 8001:8001: Maps port 8001, used by RedisInsight (a GUI tool, covered later).redis/redis-stack-server:latest: Specifies the image to use.
Verify Installation:
docker psYou should see
my-redis-stacklisted as running.To connect to the Redis CLI inside the container:
docker exec -it my-redis-stack redis-cliThen, type
PING. If it responds withPONG, Redis is running successfully. Typeexitto leave the CLI.
Option 2: Native Installation (Linux/macOS)
For macOS (using Homebrew):
- Install Homebrew: If you don’t have it, install Homebrew by following instructions on brew.sh.
- Install Redis:
brew install redis/redis-stack/redis-stack - Start Redis Server:
brew services start redis-stack - Verify Installation:You should get
redis-cli PINGPONG.
For Ubuntu/Debian (using apt):
Update package index:
sudo apt updateInstall Redis:
sudo apt install redis-serverNote: This might install an older version. For the latest, consider compiling from source or using Docker.
Start and Enable Redis:
sudo systemctl enable redis-server sudo systemctl start redis-serverVerify Installation:
redis-cli PINGYou should get
PONG.
For CentOS/Rocky Linux/AlmaLinux (using dnf/yum):
Install EPEL repository (if not already installed):
sudo dnf install epel-releaseInstall Redis:
sudo dnf install redisNote: This might install an older version. For the latest, consider compiling from source or using Docker.
Start and Enable Redis:
sudo systemctl enable redis sudo systemctl start redisVerify Installation:
redis-cli PINGYou should get
PONG.
2. Setting up Node.js Client
For Node.js, the most popular and robust client library is ioredis.
- Create a New Project Directory:
mkdir redis-nodejs-app cd redis-nodejs-app npm init -y - Install
ioredis:npm install ioredis # or yarn add ioredis - Test Connection (Node.js):
Create a file named
app.jsand add the following code:// redis-nodejs-app/app.js const Redis = require('ioredis'); // Create a new Redis client instance // By default, it connects to localhost:6379 const redis = new Redis({ host: '127.0.0.1', // Or 'localhost' if running natively port: 6379, }); redis.on('connect', () => { console.log('Connected to Redis server (Node.js)!'); // Perform a simple Redis command redis.set('mykey', 'Hello from Node.js!') .then(() => redis.get('mykey')) .then((value) => { console.log(`Value for 'mykey': ${value}`); return redis.del('mykey'); // Clean up }) .then(() => { console.log('Key "mykey" deleted.'); redis.quit(); // Close the connection }) .catch((err) => { console.error('Redis operation error:', err); redis.quit(); }); }); redis.on('error', (err) => { console.error('Redis connection error (Node.js):', err); }); - Run the Node.js Script:You should see output similar to:
node app.jsConnected to Redis server (Node.js)! Value for 'mykey': Hello from Node.js! Key "mykey" deleted.
3. Setting up Python Client
For Python, the redis-py library is the standard.
- Create a New Project Directory:
mkdir redis-python-app cd redis-python-app - Create a Virtual Environment (Recommended):
python3 -m venv venv source venv/bin/activate # On Windows: .\venv\Scripts\activate - Install
redis-py:pip install redis - Test Connection (Python):
Create a file named
app.pyand add the following code:# redis-python-app/app.py import redis try: # Connect to Redis server # By default, it connects to localhost:6379 r = redis.Redis(host='127.0.0.1', port=6379, db=0) # Or 'localhost' # Ping the server to check connection r.ping() print("Connected to Redis server (Python)!") # Perform a simple Redis command r.set('mykey', 'Hello from Python!') value = r.get('mykey') print(f"Value for 'mykey': {value.decode('utf-8')}") # Decode bytes to string r.delete('mykey') # Clean up print("Key 'mykey' deleted.") except redis.exceptions.ConnectionError as e: print(f"Could not connect to Redis: {e}") except Exception as e: print(f"An error occurred: {e}") - Run the Python Script:You should see output similar to:
python app.py
Deactivate the virtual environment when done:Connected to Redis server (Python)! Value for 'mykey': Hello from Python! Key 'mykey' deleted.deactivate.
4. Introducing RedisInsight (Optional but Recommended GUI Tool)
RedisInsight is a powerful GUI tool that allows you to visually inspect your Redis data, monitor performance, and interact with your Redis server without using the command line.
- Download RedisInsight: You can download it from the RedisInsight official page.
- Connect to your Redis instance:
- If you’re running Redis via Docker as shown in Option 1, RedisInsight will usually detect it automatically on
localhost:6379. - If you installed natively, you might need to manually add a new connection, specifying
localhostand port6379.
- If you’re running Redis via Docker as shown in Option 1, RedisInsight will usually detect it automatically on
- Explore: Once connected, you can browse keys, view their data types, run commands, and monitor metrics.
This concludes the environment setup. You now have a running Redis server and basic client connections configured for both Node.js and Python. In the next chapter, we’ll dive into the first core concept: Redis Strings and key management.