Appendix A. Quick and dirty setup

Depending on your platform, setting up Redis can range from easy to difficult. I’ve broken down installation instructions into sections for the three major platforms. Feel free to skip ahead to your platform, which will also include instructions for installing and configuring Python and the Redis client libraries for Python on your system.

A.1. Installation on Debian or Ubuntu Linux

If you’re using a Debian-derived Linux, your first instinct will be to apt-get install redis-server, but this is probably the wrong thing to do. Depending on your version of Debian or Ubuntu, you could be installing an old version of Redis. As an example, if you’re using Ubuntu 10.4, you’d be downloading Redis 1.2.6, which was released in March 2010 and doesn’t support many of the commands that we use.

In this section, you’ll first install the build tools because you’ll compile Redis from scratch. Then you’ll download, compile, and install Redis. After Redis is running, you’ll download the Redis client libraries for Python.

To get started, make sure that you have all of the standard required build tools installed by fetching and downloading make, as can be seen in the following listing.

Listing A.1. Installing build tools on Debian Linux
~$ sudo apt-get update
~$ sudo apt-get install make gcc python-dev

When your build tools are installed (they were probably installed before; this was a verification step), you’ll take these steps:

1.  Download the most recent stable Redis source code from http://redis.io/download.

2.  Extract, compile, install, and start Redis.

3.  Download and install the necessary Redis client libraries for Python.

The first two steps in this process are shown next.

Listing A.2. Installing Redis on Linux

After you have Redis installed and running, you need to install the Redis client libraries for Python. You won’t bother to install Python, because Python 2.6 or 2.7 should already be installed by default on Ubuntu or Debian releases in the last few years. But you’ll download and install a simple helper package called setuptools, which will help you download and install the Redis client libraries.[1] This last step of installing the Redis client libraries for Python is shown next.

1 Experienced Python users will ask “Why not pip?” which is another package for installing Python libraries. This is because virtualenv, which is necessary for the easy download of pip, is out of the scope of these instructions.

Listing A.3. Installing the Redis client libraries for Python on Linux

Now that you have the Python libraries installed, you can skip ahead to section A.4 to test Redis from Python, which should prepare you for all of the other chapters.

A.2. Installing on OS X

As is the case with other platforms, there are a few ways to download and install Redis and the Python Redis client library. In this section, we’ll discuss the following:

1.  Downloading, installing, and running Redis on OS X.

2.  Installing the Redis client library for Python.

If you read the Linux section, you’ll know that we made sure that the necessary tools for building Redis from scratch were available, because it was easy to do so. Though the installation of Xcode for OS X is a bit more difficult, the fact that the build tools download is 10 times larger makes following along without a long break much more difficult. As such, you’ll use a method to install Redis that doesn’t require a compiler.

To install Redis in OS X without using a compiler, you’ll use a Python utility called Rudix, which installs precompiled binaries for a variety of software. Conveniently, as of this writing it includes an installer for the most recent version of Redis.

To download and install Rudix and Redis, you should open a Terminal. The Terminal application can be found in the Utilities group inside of Applications. After you’ve started the terminal, please follow along with the next listing to install Redis using Rudix.

Listing A.4. Installing Redis on OS X

Now that you’ve installed Redis, it’s time to install the Redis client library for Python. You don’t need to install Python, because OS X versions 10.6 and 10.7 come with either Python 2.6 or 2.7 preinstalled and available via python by default. While Redis is running in one terminal, open up a new tab (command + T), and follow along with the next listing to install the Python Redis library.

Listing A.5. Installing the Redis client library for Python on OS X

If you read either of the Linux or Windows install instructions, you may have noticed that we used setuptools’s easy_install method to install the Redis client library, but here you use pip. This is because Rudix offers a pip package, but doesn’t have a setuptools package, so it was easier to install pip, and then use pip to install the Redis client library for Python instead of manually downloading and installing setuptools.

Also, if you read the installation instructions for Linux, you may have noticed that we installed the hiredis helper library there, but you don’t install it on OS X. This is because, like before, you can’t guarantee that users will have Xcode installed, so you’ll use what you have available.

Now that you have the Redis Python library installed, you should skip ahead to section A.4 and follow along to use Redis from Python for the first time.

A.3. Installing on Windows

Before we get into how to install Redis on Windows, I’d like to point out that running Redis on Windows isn’t recommended, for a variety of reasons. In this section we’ll cover these points:

  • Reasons why you shouldn’t be running Redis on Windows.
  • How to download, install, and run a precompiled Windows binary.
  • How to download and install Python for Windows.
  • How to install the Redis client library.

Our first step is to discuss why you shouldn’t be running Redis on Windows.

A.3.1. Drawbacks of Redis on Windows

Windows doesn’t support the fork system call, which Redis uses in a variety of situations to dump its database to disk. Without the ability to fork, Redis is unable to perform some of its necessary database-saving methods without blocking clients until the dump has completed.

Recently, Microsoft has contributed engineering time helping to address background saving issues, using threads to write to disk instead of a forked child process. As of this writing, Microsoft does have an alpha-stage branch of Redis 2.6, but it’s only available as source, and Microsoft makes no guarantees as to its worthiness in production scenarios.

At least for the short term, there’s an unofficial port of Redis by Dusan Majkic that offers precompiled binaries for Redis 2.4.5, but it has the previously mentioned issue that Redis blocks when dumping the database to disk.

Compiling Redis in Windows yourself

If you find yourself in the position of needing the most up-to-date version of Redis on Windows as possible, you’ll need to compile Redis yourself. Your best option is to use Microsoft’s official port (https://github.com/MSOpenTech/redis/), which requires Microsoft Visual Studio, though the free Express 2010 works just fine. If you choose to go this route, be warned that Microsoft makes no guarantees as to the fitness of their ports to Windows for anything except development and testing.

Now that you know the state of Redis on Windows, if you still want to run Redis on Windows, let’s install it.

A.3.2. Installing Redis on Windows

You can download a moderately out-of-date precompiled version of Redis for 32-bit and 64-bit Windows thanks to Dusan Majkic from his GitHub page: https://github.com/dmajkic/redis/downloads. Go ahead and do that now.

After you download Redis, you’ll need to extract the executables from the zip file. As long as you’re using a version of Windows more recent than Windows XP, you should be able to extract Redis without any additional software. Do that now.

After you’ve extracted either the 32- or 64-bit version of Redis to a location of your choice (depending on your platform and preferences; remember that 64-bit Windows can run 32- or 64-bit Redis, but 32-bit Windows can only run 32-bit Redis), you can start Redis by double-clicking on the redis-server executable. After Redis has started, you should see a window similar to figure A.1.

Figure A.1. Redis running in Windows

Now that Redis is up and running, it’s time to download and install Python.

A.3.3. Installing Python on Windows

If you already have Python 2.6 or 2.7 installed, you’re fine. If not, you’ll want to download the latest version of Python 2.7, because that’s the most recent version of Python that has support for the Redis library. Go to http://www.python.org/download/ and select the most recent version of the 2.7 series that’s available for Windows in either the 32- or 64-bit version (again, depending on your platform). When Python is done downloading, you can install it by double-clicking on the downloaded .msi file.

Assuming that you accepted all of the default options for installing Python 2.7, Python should be installed in C:Python27. From here, you only need to install the Python Redis library to be ready to use Redis with Python. If you’re using Python 2.6, any time the book refers to Python27, you can instead use Python26.

To help you to install the Redis client library, you’ll use the easy_install utility from the setuptools package. This is because you can easily download setuptools from the command line. To get started, open a command prompt by going into the Accessories program group in the Start menu and clicking on Command Prompt. After you have a command prompt open, follow along with the next listing; it shows how to download and install setuptools and the Redis client library.

Listing A.6. Installing the Redis client library for Python on Windows

Now that you have Python and the Redis client library installed, you should continue with section A.4 to use Redis from Python for the first time.

A.4. Hello Redis

After Redis itself is installed, you need to ensure that Python has the proper libraries for accessing Redis. If you followed the earlier instructions, you may still have a command prompt open. In this command prompt (you can open a new one if you closed the old one), you’ll run Python. (Windows users can refer to how you ran Python during the setup procedure.) Inside of the Python console, you’ll try connecting to Redis and issue a couple commands, as shown in the next listing.

Listing A.7. Testing Redis from Python

Running Python in other ways

Though you can run Python in a standard terminal, there are a variety of other more “fully featured” ways to have a Python console available. A basic editor and console called Idle ships with Python on Windows and OS X; you can also install it on Linux (install the idle-python2.6 or idle-python2.7 package, as relevant). In a console, you can run python -m idlelib.idle from the command line, and Idle should load. Idle is a fairly basic editor and Python console itself, so if you’re new to programming, it should be a gentle introduction. Many other people have found IPython to be the Python console of choice, offering a list of amazing features too long to mention here. Whether you go basic or fully featured, you can’t go wrong.

Redis on OS X and Windows

Right now, precompiled versions of Redis for Windows and OS X are from the 2.4 series. In some chapters, we use features that are only available in the Redis 2.6 and later series. If you find that something we do doesn’t work, and you’re using Redis 2.4, it’s probably because the feature or usage was added in Redis 2.6. See the notes in chapter 3 for specific examples.

Configuring Redis

By default, Redis should be configured to keep your data using either snapshots or append-only files, and as long as you execute shutdown on a client, Redis should keep your data around. Depending on how you started it, Redis may be keeping the on-disk version of data in the same path as the path you’re running it from. To update that, you’ll want to edit redis.conf and use system startup scripts appropriate for your platform (remember to move your data to the newly configured path). More information about configuring Redis is available in chapter 4.

Is hiredis available on non-Linux platforms?

For those who are using Windows or OS X and peeked at the Debian/Ubuntu install instructions, you’ll have noticed that we installed a library called hiredis to be used with Python. This library is an accelerator that passes protocol processing to a C library. Though this library can be compiled for OS X and Windows, binaries for them aren’t readily downloadable on the internet. Also, because I didn’t have you install a compiler, if you’re interested in compiling and using hiredis on your platform, you’re on your own.

Periodically in other chapters, we’ll use the Python console to show interactions with Redis. In other cases, we’ll show function definitions and executable statements outside of a console. In those cases where we’re not using the console, it’s assumed that those functions are defined in a Python module. If you haven’t used Python before, you should read Python’s tutorial on modules and running modules as programs in the first part of http://docs.python.org/tutorial/modules.html, up to and including the section “Executing modules as scripts.”

If you’re the kind of person who can read through language documentation and tutorials and just “get it,” and you haven’t spent a lot of time with Python in the past, you may want to consider going through the Python language tutorial at http://docs.python.org/tutorial/. If you’re only interested in the important stuff where you’ll learn the most about Python’s syntax and semantics, read sections 3-7, and then 9.10 and 9.11 (which are about generators, which we use a couple of times).

By now you’ll have Redis and a Python interpreter running. If you got here via a reference from chapter 1, go back to really start using Redis from Python.

If you’re having difficulty installing Redis or Python, please post your questions or read others’ answers on the Redis in Action Manning forum: http://mng.bz/vB6c.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.122.11