Edited 1 week ago by ExtremeHow Editorial Team
RedisLinuxInstallationSetupSoftwareConfigurationGetting StartedDevelopmentDatabaseServer
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.
Before starting the installation, make sure that your system meets the following minimum requirements:
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
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.
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
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.
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.
To use Redis in a production environment, configure it as a background service. This involves several steps, as described below:
Create a directory to hold the configuration file and another directory for the Redis data:
sudo mkdir /etc/redis
sudo mkdir /var/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.
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
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.
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
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