How to Install Redis on Ubuntu 18.04

In this tutorial, we will cover the steps needed for installing Redis on Ubuntu 18.04, as well as a couple of ways to use Redis.

Redis (short for Remote Dictionary Server), is an open-source in-memory data structure store which can be used as a database, cache, and message broker. Redis is a NoSQL Key/Value store, which offers flexibility and improves performance. One of the best features of Redis is the huge range of data types or data structures that it supports, such as lists, sets, stored sets, hashes, bitmaps, etc…Install Redis on Ubuntu 18.04

Redis has built-in master-slave replication that allows a Redis server to be an exact copy of a master server’s database. Masters can have multiple slaves, and the replication is done asynchronously, meaning that the master will continue to handle queries while slave servers are synchronizing.

Requirements:

  • For the purposes of this tutorial, we will use an Ubuntu 18.04 VPS.
  • Full SSH root access or a user with sudo privileges is also required.

1. Connect via SSH

Connect to your server via SSH as the root user using the following command:

ssh [email protected]IP_ADDRESS -p PORT_NUMBER

and replace “IP_ADDRESS” and “PORT_NUMBER” with your actual server IP address and SSH port number.

Before starting with the installation, you will need to update your system’s packages to their latest versions.

You can do this by running the following commands:

apt-get update 
apt-get upgrade

Once the upgrades are completed, we can move on to the next step.

2. Install Redis on Ubuntu 18.04

In order to get the latest stable version of Redis, we will download and install it from the official Ubuntu repositories.

Run the command below to install Redis on your machine:

sudo apt-get install redis-server

With this command, we will also download and install all the required dependencies.  Now we need to make one important configuration change in Redis.

We will open the Redis configuration file in nano with the following command. You are free to use any other text editor if you prefer:

sudo nano /etc/redis/redis.conf

We need to find supervised directive. The supervised directive allows us to declare an init system to manage Redis as a service, and we will change it to systemd.

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd

To make Redis aware of this, we need to restart it with the following command:

sudo systemctl restart redis-server

3. Configure Redis as a cache on Ubuntu 18.04

We will now configure Redis to function as a cache. We need to edit the /etc/redis/redis.conf configuration file. Open the configuration file with the following command:

sudo nano /etc/redis/redis.conf

Find the maxmemory directive and change it to 128mb. This directive will select what to remove when the max memory is reached. Also, we need to find the maxmemory-policy directive and change it to allkeys-lru.

maxmemory 128mb
maxmemory-policy allkeys-lru

With this configuration, Redis will remove any key based on the LRU algorithm when the max memory of 128 MB is reached. Save and close the file, then restart the Redis service:

sudo systemctl restart redis-server

Next, we will enable Redis on system boot:

sudo systemctl enable redis-server

We will also need to restart Apache or PHP-FPM service to get the php-redis extension enabled and ready to use on our server.

4. Using Redis on Ubuntu 18.04

We can use Redis as an object cache for a PHP-based application, like Magento or WordPress. If you would like to configure WordPress with Redis, you can simply install the Redis Object Cache plugin and adjust the connection parameters.

Once the plugin is installed and enabled, we can use the Redis command line monitor to see the real-time output with the following command:

redis-cli monitor

If you need to purge the Redis cache, you can simply do that using the Redis command line tool:

redis-cli

With this command, we will be able to execute commands in the Redis command prompt. To purge the cache, we will execute the following command:

flushall

Redis cache has been successfully installed on your Ubuntu 18.04 server, and we have also shown you a couple of ways to improve your productivity using Redis.


Installing Redis on Ubuntu 18.04Of course, you don’t have to install Redis on Ubuntu 18.04 if you use one of our Redis VPS Hosting services, in which case you can simply ask our expert Linux admins to install and configure Redis on Ubuntu 18.04 for you. They are available 24×7, and will take care of your request immediately.

PS. If you liked this post on how to install and configure Redis on Ubuntu 18.04, please share it with your friends on the social networks using the buttons below, or simply leave a comment in the comments section. Thanks.