WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Install Redis on Linux

Edited 1 week ago by ExtremeHow Editorial Team

RedisLinuxInstallationSetupSoftwareConfigurationGetting StartedDevelopmentDatabaseServer

How to Install Redis on Linux

This content is available in 7 different language

Redis is an open-source, in-memory key-value data store that is widely used for caching, session management, real-time analytics, and more. It supports complex data structures like strings, hashes, lists, sets, and more. Redis is designed for high performance and can be deployed on various platforms. This guide will walk you through the installation of Redis on a Linux system. We will cover all the necessary steps from downloading the software to configuring it for use.

1. System requirements

Before starting the installation, make sure that your system meets the following minimum requirements:

2. Update your system

It is good practice to update your system's package index before installing new software. This ensures that you have access to the latest software versions. Run the following command to update your package index.

sudo apt-get update

If you are using CentOS, the command is:

sudo yum update

3. Install the required packages

To build Redis from source, you need to install some build tools on your system. Execute the following command to install them on Ubuntu system:

sudo apt-get install build-essential tcl

For CentOS, the equivalent command is:

sudo yum groupinstall "Development Tools"

Make sure all required packages for setting up Redis are installed correctly.

4. Download and extract Redis

We will download the latest stable version of Redis from their official website. Use the wget command to download Redis.

wget http://download.redis.io/redis-stable.tar.gz

Once downloaded, extract the tar.gz file using the following command:

tar xzf redis-stable.tar.gz

Navigate to the extracted directory:

cd redis-stable

5. Compile Redis

In this step, compile the source files of Redis.

make

This command will compile the Redis binary. This may take a few minutes depending on your system capabilities. Once the compilation is complete, test the build using the command below:

make test

Testing makes sure that everything is compiled correctly.

6. Install Redis

Once the build and testing is successful, install Redis using the following command:

sudo make install

This command will install the Redis binary to /usr/local/bin. Redis is now installed on your system.

7. Configure Redis as a background service

To use Redis in a production environment, configure it as a background service. This involves several steps, as described below:

7.1 Create directories

Create a directory to hold the configuration file and another directory for the Redis data:

sudo mkdir /etc/redis
sudo mkdir /var/redis

7.2 Configure Redis

Copy the default configuration file to the /etc directory we are using:

sudo cp redis.conf /etc/redis

Edit the configuration file using a text editor such as nano:

sudo nano /etc/redis/redis.conf

Inside, make the following changes:

Save the file and exit the editor.

7.3 Setup Redis as a Systemd service

Create a new systemd service file for Redis:

sudo nano /etc/systemd/system/redis.service

Add the following ingredients:

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=root
Group=root
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/bin/kill -s TERM $MAINPID
Restart=always

[Install]
WantedBy=multi-user.target

Save the file and exit. Start the Redis service with the following command:

sudo systemctl start redis

Enable Redis to start on boot:

sudo systemctl enable redis

Check the status to make sure it's running:

sudo systemctl status redis

8. Testing Redis

Once Redis is running as a service, you can test it using the Redis command-line tool. Open the terminal and type:

redis-cli

This command opens the Redis command-line interface. To test, set up a key-value pair:

SET test "Hello World!"

Get the value using the following:

GET test

If Redis returns "Hello World!", it means that Redis is working correctly.

9. Securing Redis

For production use, make sure Redis is secure. Edit the Redis configuration file to connect Redis only to localhost:

# bind 127.0.0.1

Set a password for added security:

requirepass yourpasswordhere

Restart Redis for these changes to take effect:

sudo systemctl restart redis

Conclusion

Installing Redis on a Linux system is a straightforward process, involving downloading, building, and configuring the software. Upon following the steps outlined in this guide, you should have a fully functional, secure Redis installation ready for use. Remember to update Redis regularly and configure it properly for a production environment to ensure optimal performance and security.

If you find anything wrong with the article content, you can


Comments