Feb 01, 2024
How to Install Redis on Ubuntu + Configuration and Command Examples
Developers often look for systems that would increase the speed and performance of their projects. One popular system like that is Redis. It is an open-source, in-memory database used as a cache and message broker. It is also known as a data structure server.
What makes it unique compared to relational database systems is the ability to store high-level data types, such as maps, lists, and sets. It also offers an easy-to-use interface, atomic manipulation of data, and exceptional performance.
In this tutorial, we’ll go over the Redis installation process on Ubuntu 18.04, 20.04, and 22.04, as well as any configuration required to make it run properly.
How to Install Redis on Ubuntu
In order to install and configure the Redis server on Ubuntu, you will need to have an already running virtual private server (VPS) with the Ubuntu operating system installed. With all the prerequisites done, connect to SSH and start the installation.
1. Update APT Repository
Redis is already included in the official package repository of Ubuntu. Nevertheless, we recommend to frequently update the APT repository to get the latest version possible.
sudo apt-get update
2. Install Redis Server on Ubuntu Using the APT Command
Installing Redis is as simple as using the following command with the sudo privileges:
sudo apt install redis
Press “y” and then hit Enter to continue.
3. Check the Redis Version
Once Redis is installed, you can check if the installation was successful by using this command:
The output will display the version of the Redis server currently installed on your machine.
4. Start Redis Service
Once the installation is complete, we recommend checking if the Redis instance is running. In order to test connectivity, you can use the following command:
sudo systemctl status redis
In the output, locate “Active: active (running)” line.
If Redis hasn’t been started and the status is inactive, you can enable Redis client by entering the following command:
If Redis is already running and you want to stop the process, you can use the sudo systemctl stop redis command. After this, you will see “Active: inactive (dead)” in the status command output. If you need an option to restart the Redis service, use the sudo systemctl restart redis-server command.
5. Rename Dangerous Commands
A common practice for securing Redis is renaming commands or disabling any possibly unsafe ones. Such commands are hazardous because any unauthorized users can use them and manipulate or even destroy all of the database data.
Keep in mind that this process is entirely optional, and you can decide whether you want to rename, disable or leave the command active. In order to begin, open the /etc/redis/redis.conf file with your preferred editor. We will be using nano in this example:
sudo nano /etc/redis/redis.conf
Then, find the SECURITY section, where you can either rename or disable a command. In our example, we are renaming FLUSHALL, SHUTDOWN, DEL commands to CANTSEE_FLUSHALL, CANTGUESS_SHUTDOWN, CANTHEAR_DEL
We are also disabling DEBUG and CONFIG commands entirely:
Other possibly unsafe commands include RENAME, SAVE, SREM, FLUSHDB, PEXPIRE, and BGSAVE.
In order to test everything, restart the Redis service:
sudo systemctl restart redis.service
Then, log in to the Redis command-line client:
To test out a disabled command, you just need to try it. For example, testing out the DEBUG command should look like this:
An error will be shown because DEBUG command is disabled completely. Next, test out the renamed command. In our case, it’s FLUSHALL.
As you can see, FLUSHALL command doesn’t work, while our renamed one CANTSEE_FLUSHALL is working perfectly.
Learn More About Databases on Ubuntu
Check out our article to learn more about how to install PostgreSQL on Ubuntu for database management.
How to Find and Edit Redis Configuration File on Ubuntu
The default Redis configuration file is located in /etc/redis/redis.conf directory. The default configuration is when the Redis server listens for all available connections.
You can make it listen to the interfaces of your choice. This can be done by using the bind configuration directive that a single or multiple IP addresses would follow.
To instruct the Redis server to listen for a particular IP address, you need to edit the /etc/redis/redis.conf file. Open it with your preferred editor. In our case, we are using the nano editor.
sudo nano /etc/redis/redis.conf
Locate the line “bind 127.0.0.1 ::1”.
Change the IP address by entering the values of the connections you want the Redis server to listen for. Here’s an example:
In order to add multiple IP addresses, simply separate the IP addresses with a space like so:
bind 220.127.116.11 18.104.22.168
Here you need to enter the IP addresses of your network.
However, if you want the server to listen to all the interfaces on the network, you can comment out the bind line entirely:
Once you are done making changes, save and close the file. Then restart the Redis service to apply the changes:
sudo systemctl restart redis-server
Using Redis Commands
There are several different groups of commands in Redis which include:
- String commands
- List commands
- Set commands
- Hash commands
- Sorted set commands
- Pub/Sub commands
Here we listed some of the commands used in the Redis prompt:
- Redis-server /path/redis.conf – this Redis config command starts Redis with the particular configuration file.
- Redis-cli – a command to run Redis CLI client
- APPEND key value – append a value to a key.
- BITCOUNT key [start end] – sets the number of set bits in a string.
- SET key value – set a value in a key.
- EXPIRE key 120 – expire a key in 120 seconds.
- INCR key – increment the value in a key.
- KEYS pattern – finds all keys matching a particular pattern.
- DEL key – deletes a key.
- STRLEN key – get the length of a key.
- MSET key value [key value …] – set multiple keys and value pairs.
- MGET key [key …] – get values from multiple keys.
- GETSET key value – sets new value while returning the old value.
- INCRBY key increment – increases key count.
- DECRBY key increment – decrease key count
Redis database server is one of the most popular alternatives to relational databases such as MySQL. With Redis, users can expect high performance and speed, especially for high-traffic websites.
In this tutorial, we’ve covered Redis’s main aspects and why it is so valuable. We’ve also reviewed the installation process on Ubuntu 18.04, 20.04, and 22.04 systems. Lastly, we’ve shown a couple of helpful Redis commands and how to secure Redis by renaming unsafe commands.
We hope that you found this tutorial useful. If you have any questions or insights, leave them in the comments section below.