How to Install Redis on Ubuntu 18.04

How to Install Redis on Ubuntu 18.04
How to Install Redis on Ubuntu 18.04

Install Redis on Ubuntu 18.04

Redis is a in memory key-value data structure store mainly used as a database, message broker or as a cache. Redis supports wide languages with flexibility and high performance. It supports different data structures like strings, lists, sets, maps, spatial indexes, and bitmaps. In this tutorial, you are going to learn How to Install Redis on Ubuntu 18.04.

Prerequisites

Before you start to install Redis on Ubuntu 18.04. You must have a non-root user account on your server with sudo privileges and IPv6 should be enabled on your server otherwise Redis service won’t start.

Install Redis

First, update the apt package manager index and upgrade all packages running below command:

sudo apt-get update && sudo apt-get upgrade

Redis package is available in default repository in Ubuntu 18.04. So to install Redis run below command:

sudo apt install redis-server

If you are using Laravel PHP framework or WordPress then you can install php-redis package running below command:

sudo apt install php-redis

After installing Redis it will start automatically so to confirm installation and check Redis status run below command:

sudo systemctl status redis-server

The output should look like:

● redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-03-13 16:03:57 PDT; 2h ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
  Process: 2197 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 2201 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
 Main PID: 2226 (redis-server)
    Tasks: 4 (limit: 2319)
   CGroup: /system.slice/redis-server.service
           `-2226 /usr/bin/redis-server 0.0.0.0:6379

Binding Redis

By default, you can not access redis from another host because its by default bound to localhost only. To bind it to localhost(127.0.0.1) this follow below instructions:

First, run below command and open Redis configuration file:

sudo nano /etc/redis/redis.conf

Now uncomment bind 127.0.0.1 ::1 line by removing # from the beginning. Now it’s bound to localhost only.

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 ::1

Optional: If you want to access Redis from remote host then you can replace bind 127.0.0.1 with bind 0.0.0.0 like given below:

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0 ::1

Save and exit file pressing Ctrl+x.

To take changes effect, restart Redis server by running below command:

sudo systemctl restart redis-server

now run below command to confirm above changes:

tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      14222/redis-server  
tcp6       0      0 ::1:6379                :::*                    LISTEN      14222/redis-server  

Now to check everything is ok, start redis-cli using below command:

redis-cli

Above command will start redis-cli at 127.0.0.1:6379. So to check everything works fine run below command:

ping

You should see exact below output:

PONG

Exit the redis-cli shell using below command:

exit

Testing Redis Using Redis Client

To start testing Redis, run redis0-cli using below command:

redis-cli

It will open redis-cli shell, Now store a value with the key myname with value John run below command inside it:

set myname "John"

The output should be:

OK

Now run below command to check the value of myname:

get myname

You should see the following output:

John

Exit the redis-cli shell using below command:

exit

Conclusion

You have successfully learned how to install Redis on Ubuntu 18.04. If you have any queries please don’t forget to comment out.

LEAVE A REPLY

Please enter your comment!
Please enter your name here