How to Install Redis on Ubuntu 16.04

Redis is an in-memory data structure storage that is used for many purposes like storing key-value pairs, message queue handling and caching. Redis is widely used for cache storage and executing task queues in background on servers.

In this guide, I am going to show you how you can install and configure Redis on Ubuntu 16.04 machine. We will also set a secure password to access Redis server.

To make sure that you can follow this guide without facing any issues on the way, here are some prerequisites!

  • VPS/VM/Dedicated Server running on Ubuntu 16.04
  • Root access to the server (so that we can install Redis)

If you have a Server running on Ubuntu 16.04 as well as root access on it, you are good to go.

Install Redis on Ubuntu 16.04

It’s good that we can now install Redis directly. It means, we don’t have to download the source and then compile it in order to install Redis. To install Redis server, execute the following command in terminal.

sudo apt-get install redis-server -y

It will take few seconds to install Redis server on your Ubuntu 16.04 machine. The -y flag we used is to avoid getting a confirmation message to install Redis server.

Once the Redis server is installed on your machine, execute the following command to check if Redis is working properly or not.

redis-cli

If Redis server is installed successfully, the redis-cli command will open a command line interface for Redis. To test if Redis is working correctly or not, Execute the following command.

127.0.0.1:6379> PING

In response, you should get “PONG”. If you get the message, Redis server is successfully installed and running on the server. To exit, execute “exit” command in the Redis server’s command line interface.

The next thing we have to do is to configure Memory usage limit and password in Redis configuration file.

Configure Redis Server

Redis configuration file is located at “/etc/redis/redis.conf”. To edit the configuration file using nano, execute the following command in terminal.

sudo nano /etc/redis/redis.conf

Now, you will see a redis configuration file with lots of directives and information. Press CTRL+W to enable search interface and type¬†“# maxmemory” and then hit the Enter key to find the line containing “# maxmemory”.

Once you find the line containing following content:

# maxmemory <bytes>

Replace it with:

maxmemory 256mb

You can replace “256mb” with any number you want based on the resources available on your server. Now, we also have to set the password for Redis server. To set the password, Press CTRL+W and type “# requirepass” and hit Enter key to navigate to the line.

Once you find the line containing following content:

# requirepass foobared

Replace it with:

requirepass YOUR_DESIRED_REDIS_PASSWORD

After editing the line, Press CTRL+X and then type Y and then hit the Enter key to save the configuration. Note that the configuration will update only after restarting or reloading the service.

To restart the Redis server, execute the following command.

sudo service redis restart

And then check the status of the Redis service by executing the command given below. It should show “Running”.

sudo service redis status

So, our Redis Server Installation and configuration is done and the Redis server is ready to use. If you still want to test it before using it on any application in production environment, continue following the article.

Testing Redis Server

There are few things that we have changed in our configuration. Mainly, the memory limit for Redis and password required for authentication. We can test if Redis allows us to create and store key-value pairs without authentication.

Once again, Execute the following command to spin up the Redis server’s command line interface.

redis-cli

Now, try to create and store a key-value pair in Redis:

127.0.0.1:6379> set test working

It should not work and you will see the the error message stating “NOAUTH Authentication required.”. Go ahead and authenticate using the following command.

127.0.0.1:6379> auth YOUR_DESIRED_REDIS_PASSWORD

It will return “OK” as response if the password is correct. It means, you are now authenticated and can create and store key-value pairs in Redis. Now, execute the test command again to store the key-value pair. Then execute the following command to get a value based on a key.

127.0.0.1:6379> get test

It should return “working” if you are successfully authenticated. If it is working as expected, Congratulations! Our Redis server is ready to handle the requests!

Conclusion: So, this is how you can install and configure Redis on Ubuntu. As you have successfully installed Redis on Ubuntu right now, you can also follow our guide to set up Redis Object Cache for WordPress. If you are facing any issue installing Redis server, please contact our support staff.

Leave a Reply