How-to Install Redis From Source on Ubuntu 14.04
Overview
The latest version of Ubuntu at the time of this writing is 15.10. Ubuntu 15.10 already shipped with Redis 3 but if you need LTS (Long Term Support) version of Ubuntu (Latest LTS version is Ubuntu 14.04) you can only install Redis 2.8 from the repository.
Previously we already learned how-to install Redis from Ubuntu Repository. In this tutorial we’ll learn how-to install the latest stable version of Redis, or Redis 3 from source.
Prerequisite
We need make to compile Redis from source. You can check whether you already have make installed or not using which
command.
$ which make
/usr/bin/make
If the command above does not give output like the above it means that you haven’t got make installed . You can install make from the repository.
$ sudo apt-get -y install cmake
All the prerequisites are now ready, the first thing to do is download Redis source. At the time of this writing the latest stable version of Redis is 3.0.5. You might want to check Redis Download Page for the latest stable version of Redis.
You can also easily access the latest stable version of redis using this URL : http://download.redis.io/redis-stable.tar.gz.
Now let’s download the Redis source archive using wget
$ wget -c http://download.redis.io/releases/redis-3.0.5.tar.gz
After downloading the Redis source code you might want to verify the download to make sure that the source is not tampered with in the network.
You can check check the SHA1 hash of each package released on Redis Hashes Repository.
To check hash of package that you downloaded you can use sha1sum
application. The command is below :
$ sha1sum redis-3.0.5.tar.gz
ad3ee178c42bfcfd310c72bbddffbbe35db9b4a6 redis-3.0.5.tar.gz
After running the command above please check with the page on the redis-hashes repo above and make sure the output of the command above is correct.
We can see the the sha1 hash of the package that was downloaded and the one hosted on github is the same.
Installing Redis
After checking the SHA1 hash of the package, now extract the redis source archive and rename the extracted directory to redis
$ tar xzf redis-3.0.5.tar.gz
$ mv redis-3.0.5 redis
Go to the new directory that contains the redis source code and run make
to compile the Redis source code. This process might take some time depending on your machine specification.
$ cd redis
$ make
...
``` language-bash
LINK redis-server
INSTALL redis-sentinel
CC redis-cli.o
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
CC redis-check-dump.o
LINK redis-check-dump
CC redis-check-aof.o
LINK redis-check-aof
Hint: It’s a good idea to run ‘make test’ 😉
make[1]: Leaving directory `/home/sumodirjo/redis/src’
The above output has been truncated. If your compilation of Redis was successful you should get output similar to the above.
After successful compilation, let's install the redis binary files. The binary will be installed to the ```/usr/local/bin``` folder. We need to use ```sudo``` to copy these binary files to the directory.
$ sudo make install
cd src && make install
make[1]: Entering directory `/home/sumodirjo/redis/src’
Hint: It’s a good idea to run ‘make test’ 😉
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/home/sumodirjo/redis/src’
## Trying Redis
Now, the redis-server binary is ready, we can test using it by running ```/usr/local/bin/redis-server```. You will see output similar to text below :
$ /usr/local/bin/redis-server
21242:C 03 Nov 20:44:36.309 # Warning: no config file specified, using the default config. In order to specify a config file use /usr/local/bin/redis-server /path/to/redis.conf
21242:M 03 Nov 20:44:36.310 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
21242:M 03 Nov 20:44:36.310 # Redis can’t set maximum open files to 10032 because of OS error: Operation not permitted.
21242:M 03 Nov 20:44:36.311 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase ‘ulimit -n’.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.5 (00000000/0) 64 bit
.-.-```. ```/ _.,_ ''-._
-.|’
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-._.-'| Port: 6379
-.
|._ / _.-' | PID: 21242
-._ -._
-./ .-‘ _.-‘
|-._
-. -.__.-' _.-'_.-'|
-.
|-._ _.-'_.-' | http://redis.io
-. -._
-..-‘.-‘ _.-‘
|-._
-. -.__.-' _.-'_.-'|
-.
|-._ _.-'_.-' |
-. -._
-..-‘_.-‘ _.-‘
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
21242:M 03 Nov 20:44:36.316 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
21242:M 03 Nov 20:44:36.317 # Server started, Redis version 3.0.5
21242:M 03 Nov 20:44:36.317 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.
21242:M 03 Nov 20:44:36.317 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled’ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
21242:M 03 Nov 20:44:36.317 * DB loaded from disk: 0.000 seconds
21242:M 03 Nov 20:44:36.317 * The server is now ready to accept connections on port 6379
The process is still running on foreground, to exit you can press ```CTRL+C```.
## Configuring Redis
Even though we're already able to run Redis server but it is not properly setup yet. In this section we'll configure Redis so that it become a properly setup service on Ubuntu 14.04.
### Creating the Redis User
We need to create a user to run redis server. You can run command below to create new user named ```redis```.
$ sudo useradd redis -d /var/lib/redis -s /bin/false
Let's check the new user we just created :
$ grep redis /etc/passwd
redis:x:1001:1001::/var/lib/redis:/bin/false
And ```redis``` group for Redis
$ grep redis /etc/group
redis:x:1001:
### Creating Directories for Redis
We need to create some directories to store the redis configuration files, pidfile and also data.
Create the Redis configuration directory
$ sudo mkdir /etc/redis
Create a directory to store the Redis pidfile
$ sudo mkdir /var/run/redis
$ sudo chown -R /var/run/redis
Create a directory to store Redis data
$ sudo mkdir /var/lib/redis
$ sudo chown -R /var/lib/redis
### Create a Redis Default Configuration
The Redis init script will read the default configuration when starting redis-server. Create a new file ```/etc/default/redis-server``` with the content below. All content in this file is commented so nothing is being applied from this file.
redis-server configure options
ULIMIT: Call ulimit -n with this argument prior to invoking Redis itself.
This may be required for high-concurrency environments. Redis itself cannot
alter its limits as it is not being run as root. (default: do not call
ulimit)
#
ULIMIT=65536
### Customizing the Redis Configuration
We need to change some configurations inside the redis.conf file before copying the file to redis.
We'll make Redis server work as a daemon. Find the line below:
daemonize no
Change it to
daemonize yes
Change the pidfile location. Find the line below
pidfile /var/run/redis.pid
Change it to
pidfile /var/run/redis/redis-server.pid
This configuration change will make Redis server listen to all network interfaces. If you don't need the Redis service to be accessible from another server you can leave this configuration default. Hoever, if you need to make Redis accessible from another, find line below
bind 127.0.0.1
Change it to
bind 0.0.0.0
Now, configuring the logfile location. By default redis does not do logging. Find the line below :
logfile “”
Change it to
logfile /var/log/redis/redis-server.log
The last one is redis data directory. Find the line below
dir ./
Change it to
dir /var/lib/redis
### Copying Configuration
Copy the Redis main configuration file. Make sure you already changed the values on the ```redis.conf``` file specified above.
$ sudo cp redis.conf /etc/redis
Copy the Redis Sentinel configuration
$ sudo cp sentinel.conf /etc/redis
### Logrotate
Log rotation is a must have activity for a server application. By rotating the log, we make sure that the main log file size is not growing huge but we will still have a history of log files because the log is rotated.
For example if the redis-server log named redis-server.log when rotated it will be copied to redis-server.log.1 and then compressed as redis-server.log.1.gz. The logfile redis-server.log will be emptied.
On the next log rotation the redis-server.log.1.gz will be moved to redis-server.log.2.gz and again redis-server.log copied and compressed as redis-server.log.1.gz and so on.
Create new file ```/etc/logrotate.d/redis-server``` and add logrotate configuration below on the file.
/var/log/redis/*.log {
weekly
missingok
copytruncate
rotate 12
compress
notifempty
}
The logrotate configuration above will rotate the log on a weekly basis and keep the previous log for 12 times, in this case 12 weeks.
You can change the ```weekly``` line above with daily, monthly, or yearly. If your redis has heavy load and writes a lot of log you might want to consider rotating the log daily. How long you want to keep the logs can be configured with ```rotate``` directive above.
### Creating Init Script for Redis
Redis server needs an init script so that we can easily manage the Redis service (start, stop, restart the service).
Create new file ```/etc/init.d/redis-server``` with the content below.
#! /bin/sh
BEGIN INIT INFO
Provides: redis-server
Required-Start: $syslog $remote_fs
Required-Stop: $syslog $remote_fs
Should-Start: $local_fs
Should-Stop: $local_fs
Default-Start: 2 3 4 5
Default-Stop: 0 1 6
Short-Description: redis-server – Persistent key-value db
Description: redis-server – Persistent key-value db
END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/redis-server
DAEMON_ARGS=/etc/redis/redis.conf
NAME=redis-server
DESC=redis-server
RUNDIR=/var/run/redis
PIDFILE=$RUNDIR/redis-server.pid
test -x $DAEMON || exit 0
if [ -r /etc/default/$NAME ]
then
. /etc/default/$NAME
fi
. /lib/lsb/init-functions
set -e
case “$1” in
start)
echo -n "Starting $DESC: "
mkdir -p $RUNDIR
touch $PIDFILE
chown redis:redis $RUNDIR $PIDFILE
chmod 755 $RUNDIR
if [ -n "$ULIMIT" ]
then
ulimit -n $ULIMIT
fi
if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid redis:redis --exec $DAEMON -- $DAEMON_ARGS
then
echo "$NAME."
else
echo "failed"
fi
;;
stop)
echo -n "Stopping $DESC: "
if start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
then
echo "$NAME."
else
echo "failed"
fi
rm -f $PIDFILE
sleep 1
;;
restart|force-reload)
${0} stop
${0} start
;;
status)
echo -n "$DESC is "
if start-stop-daemon --stop --quiet --signal 0 --name ${NAME} --pidfile ${PIDFILE}
then
echo "running"
else
echo "not running"
exit 1
fi
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac
exit 0
You can also download the init script above from a github gist
$ wget -c https://gist.githubusercontent.com/sumodirjo/6a957a0dec0a11cd36e6/raw/572b2a8e7ddf2acdfa1fac002a102eb52ebab4bd/redis-server
$ sudo mv redis-server /etc/init.d
We also need to change the init script permissions so that it can be executed.
$ sudo chmod 755 /etc/init.d/redis-server
### Make Redis Automatically Start on Boot
We'll also make redis-server automatically start on boot using ```update-rc.d```. The complete command below :
$ sudo update-rc.d redis-server defaults 20 10
## Running Redis Server
We already configured Redis and also setup service for Redis server. Now, let's start the redis service
$ sudo service redis-server start
Starting redis-server: redis-server.
You can change the option ```start``` above with stop, restart, force-reload and status.
To check the Redis server status you can run.
$ sudo service redis-server status
redis-server is running
Another method that you can use to check redis service is by checking redis process using ```ps```.
$ ps aux | grep redis
redis 22467 0.4 0.7 46296 15648 ? Ssl 03:06 0:00 /usr/local/bin/redis-server *:6379
sumodir+ 22477 0.0 0.0 11712 672 pts/0 S+ 03:06 0:00 grep –color=auto redis
And the last method to check redis server status is using netstat to check whether redis is already listening to the correct port or not.
$ sudo netstat -naptu | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 22467/redis-server
tcp6 0 0 :::6379 :::* LISTEN 22467/redis-server
## Benchmarking Redis
Redis also comes with a benchmarking tool. To benchmark your redis server you can run command below
$ redis-benchmark
## Running Tests
Redis comes with a testing script, but the script needs tcl 8.5 as prerequisite. To install tcl 8.5 you can run command below
$ sudo apt-get -y install tcl8.5
### Redis Tests
Now you 're ready to run a test, make sure that redis-server is already running. If you want to run a redis test you can run the script with the command below (don't forget the ```./``` to run the script).
$ ./runtest
…
The End
Execution time of different units:
1 seconds – unit/printver
1 seconds – unit/auth
1 seconds – unit/quit
2 seconds – unit/scan
3 seconds – unit/multi
6 seconds – unit/protocol
11 seconds – unit/expire
20 seconds – unit/type/list
8 seconds – integration/aof
4 seconds – integration/rdb
32 seconds – unit/dump
33 seconds – unit/type/list-2
2 seconds – integration/logging
2 seconds – unit/pubsub
35 seconds – unit/other
4 seconds – integration/convert-zipmap-hash-on-load
2 seconds – unit/slowlog
1 seconds – unit/introspection
2 seconds – unit/limits
39 seconds – unit/type/hash
11 seconds – unit/scripting
54 seconds – unit/type/set
23 seconds – unit/bitops
63 seconds – unit/aofrw
65 seconds – unit/type/zset
31 seconds – unit/maxmemory
65 seconds – integration/replication-2
73 seconds – unit/sort
37 seconds – unit/memefficiency
79 seconds – unit/type/list-3
79 seconds – unit/basic
55 seconds – unit/hyperloglog
102 seconds – integration/replication-4
107 seconds – integration/replication-3
112 seconds – integration/replication-psync
121 seconds – integration/replication
154 seconds – unit/obuf-limits
o/ All tests passed without errors!
### Sentinel test
Sentinel is a feature of Redis where multiple instance of Redis can work together as cluster. We're not going to discuss about Sentinel in this tutorial but we'll only run the test. To run sentinel the test, you can run command below
$ ./runtest-sentinel
…
Cleaning up…
GOOD! No errors.
“`
Summary
In this tutorial we learned how-to install latest stable version of Redis from source by compiling the source code and configuring redis to run as a service.
It’s always easier and recommended to use Redis from your the Linux distribution repository but if you need features that are not available on the Redis version provided by the Linux distribution, you might have to compile and install the latest stable version of Redis yourself.