How to Install Redis Server on Ubuntu Linux 16.04/18.04
This post will guide you how to install and configure redius on Ubuntu Linux 16.04 or 18.04. How do I install Redis Server on Ubuntu Linux.
What is Redis?
Redis is an open-source in-memory data structure project implementing a distributed, in-memory key-value database with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, hyperloglogs, bitmaps, streams and spatial indexes.
Installing Redis
You need to ssh to your remote Ubuntu Linux server via SSH, and install the newest versions of all packages that are currently installed on your server, type the following command:
$ sudo apt-get update $ sudo apt-get upgrade
Once the update is completed and you can try to install Redis package. you can use the apt-get to install redis package with the default apt repository. Type the following command from the terminal:
$ sudo apt-get install redis-server
Outputs:
devops@devops-osetc:~$ sudo apt-get install redis-server sudo: unable to resolve host devops-osetc [sudo] password for devops: Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libjemalloc1 redis-tools Suggested packages: ruby-redis The following NEW packages will be installed: libjemalloc1 redis-server redis-tools 0 upgraded, 3 newly installed, 0 to remove and 294 not upgraded. Need to get 518 kB of archives. After this operation, 1,505 kB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 libjemalloc1 amd64 3.6.0-9ubuntu1 [78.9 kB] Get:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 redis-tools amd64 2:3.0.6-1ubuntu0.2 [95.6 kB] Get:3 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 redis-server amd64 2:3.0.6-1ubuntu0.2 [344 kB] Fetched 518 kB in 2s (201 kB/s) Selecting previously unselected package libjemalloc1. (Reading database ... 179830 files and directories currently installed.) Preparing to unpack .../libjemalloc1_3.6.0-9ubuntu1_amd64.deb ... Unpacking libjemalloc1 (3.6.0-9ubuntu1) ... Selecting previously unselected package redis-tools. Preparing to unpack .../redis-tools_2%3a3.0.6-1ubuntu0.2_amd64.deb ... Unpacking redis-tools (2:3.0.6-1ubuntu0.2) ... Selecting previously unselected package redis-server. Preparing to unpack .../redis-server_2%3a3.0.6-1ubuntu0.2_amd64.deb ... Unpacking redis-server (2:3.0.6-1ubuntu0.2) ... Processing triggers for libc-bin (2.23-0ubuntu10) ... Processing triggers for man-db (2.7.5-1) ... Processing triggers for systemd (229-4ubuntu21.10) ... Processing triggers for ureadahead (0.100.0-19) ... Setting up libjemalloc1 (3.6.0-9ubuntu1) ... Setting up redis-tools (2:3.0.6-1ubuntu0.2) ... Setting up redis-server (2:3.0.6-1ubuntu0.2) ... Processing triggers for libc-bin (2.23-0ubuntu10) ... Processing triggers for systemd (229-4ubuntu21.10) ... Processing triggers for ureadahead (0.100.0-19) ...
Enable Redis Service
You need to enable redis service to start it on system boot. type the following command:
$ sudo systemctl enable redis-server.service
Start Reids Service
You need to use the systemctl command to start/stop/restart redis service in your Ubuntu server. To start redis service, just type the following command:
$ sudo systemctl start redis
If you want to restart/stop redis service, just type the following commands:
#Restart redis service $ sudo systemctl restart redis #Stop Redis Service $ sudo systemctl stop redis
Check the Current Status of Redis service, type the following command:
$ sudo systemctl status redis
Outputs:
devops@devops-osetc:~$ sudo systemctl status redis sudo: unable to resolve host devops-osetc ● redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2017-08-03 03:41:24 EDT; 4min 40s ago Docs: http://redis.io/documentation, man:redis-server(1) Main PID: 14414 (redis-server) CGroup: /system.slice/redis-server.service └─14414 /usr/bin/redis-server 127.0.0.1:6379 Aug 03 03:41:24 devops-osetc systemd[1]: Starting Advanced key-value store... Aug 03 03:41:24 devops-osetc run-parts[14408]: run-parts: executing /etc/redis/redis-server.pre-up.d/00_example Aug 03 03:41:24 devops-osetc run-parts[14415]: run-parts: executing /etc/redis/redis-server.post-up.d/00_example Aug 03 03:41:24 devops-osetc systemd[1]: Started Advanced key-value store.
Configuring Redis
You can edit the configure file /etc/redis/redis.conf to configure your redis server. you can use nano or vim as a text editor to edit the configuration file, type:
$ sudo nano /etc/redis/redis.conf
You can add the following lines to configure the max memory limit as per available on your Ubuntu server, and also set the maxmemory policy to define how redis will select what to remove when maxmeory is readhed.
maxclients 10000 maxmemory 128mb maxmemory-policy allkeys-lru
Note: You can select among six policies:
volatile-lru -> remove the key with an expire set using an LRU algorithm
allkeys-lru -> remove any key according to the LRU algorithm
volatile-random -> remove a random key with an expire set
allkeys-random -> remove a random key, any key
volatile-ttl -> remove the key with the nearest expire time (minor TTL)
noeviction -> don’t expire at all, just return an error on write operations
Save and close the file. Then, restart the Redis service to reflect the changes you made to the configuration file, type:
$ sudo systemctl restart redis.service
Testing Redis
If you want to test that Redis is functioning correctly, you just type the following command to connect the redis server, type:
$ redis-cli
Then you can type ping command to test connectivity:
127.0.0.1:6379> ping PONG 127.0.0.1:6379>
You can also run info command in the redis-cli prompt to get out redis server information,type:
127.0.0.1:6379> info
Or you can directly run the command redis-cli info in your shell terminal.
$ redis-cli info
Outputs:
# Server redis_version:3.0.6 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:99784f518ea46c6e redis_mode:standalone os:Linux 4.8.0-36-generic x86_64 arch_bits:64 multiplexing_api:epoll gcc_version:5.4.0 process_id:14414 run_id:2fa1b6042808f9890ad0bf101530a02f46b9fb8b tcp_port:6379 uptime_in_seconds:9484 uptime_in_days:0 hz:10 lru_clock:8583344 config_file:/etc/redis/redis.conf # Clients connected_clients:1 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0 # Memory used_memory:508976 used_memory_human:497.05K used_memory_rss:6885376 used_memory_peak:508976 used_memory_peak_human:497.05K used_memory_lua:36864 mem_fragmentation_ratio:13.53 mem_allocator:jemalloc-3.6.0 # Persistence loading:0 rdb_changes_since_last_save:0 rdb_bgsave_in_progress:0 rdb_last_save_time:1501746084 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:-1 rdb_current_bgsave_time_sec:-1 aof_enabled:0 aof_rewrite_in_progress:0 aof_rewrite_scheduled:0 aof_last_rewrite_time_sec:-1 aof_current_rewrite_time_sec:-1 aof_last_bgrewrite_status:ok aof_last_write_status:ok # Stats total_connections_received:1 total_commands_processed:1 instantaneous_ops_per_sec:0 total_net_input_bytes:28 total_net_output_bytes:7 instantaneous_input_kbps:0.00 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:0 migrate_cached_sockets:0 # Replication role:master connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 # CPU used_cpu_sys:11.05 used_cpu_user:4.34 used_cpu_sys_children:0.00 used_cpu_user_children:0.00 # Cluster cluster_enabled:0 # Keyspace
See Also: