Would you like to learn how to install Redis server on Ubuntu Linux? In this tutorial, we are going to show you all the steps required to install and configure Redis server on Ubuntu Linux.

• Ubuntu Linux version 18.04

Related Tutorial:

On this page, we offer quick access to a list of tutorials related to Ubuntu Linux.

1. Configure Date and Time using NTP

Configure the system to use the correct date and time using NTP.

On the Linux console, use the following commands to set the correct timezone.

# dpkg-reconfigure tzdata

Install the Ntpdate package and set the correct date and time immediately.

# apt-get update
# apt-get install ntpdate
# ntpdate pool.ntp.br

The Ntpdate command was used to set the correct date and time using the server: pool.ntp.br

Let's install the NTP service.

# timedatectl set-ntp 0
# apt-get install ntp

NTP is the service that will keep our server updated.

Use the command date to check the date and time configured on your Ubuntu Linux.

# date

2. Install Redis Server

On the Linux console, use the following commands to install the Redis server package.

# apt-get update
# apt-get install redis-server

Now, you need to edit the sysctl.conf file.

# vi /etc/sysctl.conf

At the end of the file, add the following lines:

vm.overcommit_memory=1
net.core.somaxconn=65535
fs.file-max=100000

Enter the following command to reload the configuration file.

# sysctl -p
# echo never > /sys/kernel/mm/transparent_hugepage/enabled

Now, you should find the location of the redis.conf file on your system.

After finding, you need to edit the redis.conf file.

# updatedb
# locate redis.conf

# vi /etc/redis/redis.conf

Here is the original file, before our configuration and without the comments.

bind 127.0.0.1 ::1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis/redis-server.pid
loglevel notice
logfile /var/log/redis/redis-server.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

Add the following lines to the configuration file:

maxmemory 1024mb
maxmemory-policy allkeys-lru

In our example, we are going to reserve 1GB of RAM to the Redis service.

Change the following existing configuration lines:

supervised systemd
bind 127.0.0.1 ::1

Here is the new file with our configuration.

bind 127.0.0.1 ::1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised systemd
pidfile /var/run/redis/redis-server.pid
loglevel notice
logfile /var/log/redis/redis-server.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 1024mb
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

Restart the Redis service.

Enable the Redis service during boot.

Reboot the computer to test the automatic startup.

The Redis service is offered on the TCP port 6379.

# service redis-server status
# systemctl enable redis-server
# reboot

If you have Apache and PHP locally, use the following command to install the required PHP module.

# apt-get install php-redis
# service apache2 restart
# /etc/init.d/php7.2-fpm restart

3. Test the Redis-server

On the Linux console, use the following commands to test the Redis service.

# redis-cli
> ping
> set MYSITE "Techexpert.Tips"
> get MYSITE
> exit

Test the Redis service peformance.

# redis-benchmark -q -n 1000 -c 10 -P 5

PING_INLINE: 142857.14 requests per second
PING_BULK: 142857.14 requests per second
SET: 142857.14 requests per second
GET: 124999.99 requests per second
INCR: 124999.99 requests per second
LPUSH: 142857.14 requests per second
RPUSH: 142857.14 requests per second
LPOP: 124999.99 requests per second
RPOP: 124999.99 requests per second
SADD: 124999.99 requests per second
HSET: 124999.99 requests per second
SPOP: 142857.14 requests per second
LPUSH (needed to benchmark LRANGE): 124999.99 requests per second
LRANGE_100 (first 100 elements): 47619.05 requests per second
LRANGE_300 (first 300 elements): 14925.37 requests per second
LRANGE_500 (first 450 elements): 9433.96 requests per second
LRANGE_600 (first 600 elements): 6578.95 requests per second
MSET (10 keys): 100000.00 requests per second

Congratulations! you finished the Redis server installation.