Redis is a popular, open-source, in-memory data structure store used as a database, cache, and message broker.

Installing Redis on Ubuntu is straightforward, thanks to the Advanced Package Tool (APT), which simplifies installing software on Debian-based Linux systems like Ubuntu. Here’s a step-by-step guide on how to install Redis using APT.

Step 1: Update Your System

Before installing any new software, it’s good practice to update your package lists to ensure you have the latest versions available. Open your terminal and type:

1
sudo apt update

This command refreshes the list of available packages, which helps avoid potential issues during the installation.

Step 2: Install Redis

Now, you can install Redis with the following command:

1
sudo apt install redis-server -y

The -y flag automatically confirms the installation, so you won’t need to approve it manually. This command installs the Redis server and other dependencies.

Step 3: Verify the Installation

Once the installation is complete, you can verify it by checking the Redis version:

1
redis-server --version

This command should return the version number of Redis installed on your system, confirming that the installation was successful.

Step 4: Start and Enable the Redis Service

To ensure Redis starts automatically with your system, enable the Redis service:

1
sudo systemctl enable redis-server

Then, start the Redis service:

1
sudo systemctl start redis-server

You can check the status of the Redis service to confirm it’s running:

1
sudo systemctl status redis-server

If Redis is running, you’ll see an “active (running)” status.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ sudo systemctl status redis-server
● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2024-11-07 15:36:06 CST; 1min 31s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Main PID: 3982 (redis-server)
Status: "Ready to accept connections"
Tasks: 5 (limit: 4551)
Memory: 2.8M
CPU: 226ms
CGroup: /system.slice/redis-server.service
└─3982 "/usr/bin/redis-server 127.0.0.1:6379" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >

11月 07 15:36:05 john-vm-095ziwmi systemd[1]: Starting Advanced key-value store...
11月 07 15:36:06 john-vm-095ziwmi systemd[1]: Started Advanced key-value store.

Step 5: Test the Redis Installation

To confirm that Redis is functioning as expected, you can use the redis-cli (Redis Command Line Interface) to connect to the Redis server and run a simple command. Type:

1
redis-cli ping

If Redis is working correctly, you’ll see a response of PONG, indicating a successful connection to the Redis server.

1
2
$ redis-cli ping
PONG

Optional: Configure Redis

For production environments, it’s essential to configure Redis securely. By default, Redis is configured to run in an open state, which is suitable for development but may require additional configuration for production. For example, you can modify the Redis configuration file located at /etc/redis/redis.conf to set up a password, configure persistent storage, and more. After making any changes to this file, restart the Redis service:

1
sudo systemctl restart redis-server

Conclusion

With these steps, you’ve successfully installed Redis on your Ubuntu system using the APT package manager. Redis is now ready for use as an in-memory cache, database, or message broker on your server. For additional security and performance tuning, consider exploring the Redis configuration file and adjusting settings based on your specific needs.