Installing Redis (Simple)
Before we start learning about Redis and how we can leverage on its fast performance, we need to install and have a working server. The installation of Redis is easy and straightforward.
Getting ready
In order to install, we need a computer system with a working Linux environment or a Mac OS X. If using Linux, I assume you have installed the Debian or Ubuntu operating system for this book. For the other operating system, you can refer to the There's more section of this recipe. As the Debian or Ubuntu packages are usually outdated, we will see how to install the Redis server from the source. Make sure you are using the latest stable version, as Redis is under active development and new commands and improvements pop out in every version. There are no dependencies for the Redis server except libc
, which will be available in your system by default. But to build the server from the source, you need the compiler.
How to do it...
We will start with installing the build tools which are necessary to build Redis from the source. Open the terminal program and execute the following command:
# sudo apt-get update
Once the command executes, download the compiler with build essentials, which is a package of all the common build tools to build the Redis server from the source.
# sudo apt-get install build-essential
Now we have all the components to build the source. The following URL always has the latest stable build (2.6.13), which eliminates the confusion of version numbers:
# wget http://download.redis.io/redis-stable.tar.gz # tar xvzf redis-stable.tar.gz # cd redis-stable # make # sudo make install
How it works...
Building a Redis server from the source is easy and it gets the latest version. The preceding steps will download the latest version of Redis from the repository, compile the C source, and install them in the machine. In this section, we will understand about various executables generated in this process.
You can install any unstable builds using the same procedure.
Tip
Caution
Be aware that the unstable builds usually have bugs and are not for production environments, so do not use them unless you are sure about what you are doing.
On successful execution of the preceding procedure, the following binaries are built from the source:
redis-server: The server executable
redis-cli: The command-line interface for working with the Redis server
redis-benchmark: For performance benchmarking and to test your setup
redis-check-aof: For debugging corrupt data files
redis-check-dump: For debugging corrupt data files
There's more...
Now that we have built the Redis binaries from the source, we will see how to set up the Redis server with configuration files and test the installation.
Setting up and testing
The make install
command copies all the executables to the/usr/local/bin
folder. The configuration files and init scripts are not yet in place to run Redis.
You can run the
install_server.sh
script, which can be found inside theutils
folder of the Redis source. In the terminal, go to theutils
folder insideredis-stable
and execute the following command:# ./install_server.sh
The script will produce a series of questions about the path to where the configuration files are to be kept. If you are not sure, just press the Enter key (Return) to keep the default value. Once the installation is completed, the Redis server will be started by the script.
Now the server is running with the configuration available in the file
/etc/redis/6379.conf
. Runredis-cli
to test the server in another terminal window.# redis-cli
If the Redis server is installed and is running, the prompt will look something like this:
redis 127.0.0.1:6379>
You can issue a couple of commands to get the feel of working with the Redis server:
SET Android Google SET Windows Microsoft SET iOS Apple
The preceding commands will create keys of type
string
(Android, Windows, and iOS) and set their values to their respective company names. To check the server, issue theGET
command using the keys created to retrieve their values.GET Android Google GET iOS Apple QUIT
The
QUIT
command closes the connection from the server. To stop the server, issue the following command. Usingshutdown
makes sure that Redis saves the data to the disk before quitting gracefully.# redis-cli shutdown
Now we have a working Redis server with the basic configuration.
Redis in Mac OS X
If you are using Mac OS X, you can install Redis from the source or by using Homebrew. The installation process from the source is the same as we did for Debian. So we will see how to install Redis in Mac using Homebrew. You should have OS X command-line tools installed in the system for Homebrew to work. If you do not have the command-line tools installed, you can download them from the Apple Developer website (http://connect.apple.com/).
To install Homebrew, execute the following command in the terminal:
ruby <(curl -fsSk https://raw.github.com/mxcl/homebrew/go)
To check the successful installation of Homebrew, run the following command:
brew doctor
You are one step away from installing Redis. Execute the following command:
brew install redis
Homebrew installs and configures the server. You can test the installation by running redis-cli
and issuing commands to the server.
Redis in Windows
If you are a loyal user of the Windows operating system, you have hit a hurdle. Unfortunately, Redis is not officially supported in Windows 32- or 64-bit systems. But there are various ports available that are not of production quality and can be used only for development purposes. As Redis is not natively supported in Windows, the performance of ported builds is not even close to its performance in Linux. But if you do not have access to Linux systems, the compiled packages can be downloaded from the GitHub page of the officially recommended Windows port (https://github.com/dmajkic/redis/downloads).
As it is not officially supported for Windows, the book will focus on the Linux installation, and using a Linux system is strongly recommended.