Running Redis on Ubuntu on Windows

Redis running under the Windows Subsystem for Linux on Windows 10

Whilst there's a Windows port of Redis server, the last stable release that was produced was 3.0.504 in July 2016. There's plenty of options for running later versions of Redis as a Windows developer, up to and including running a Linux virtual machine to host a copy of a more recent version of Redis. One of the options I thought worth giving a try is running it under the Windows Subsystem for Linux (installation instructions).

Having installed WSL along with Ubuntu (this was installed quite a while ago meaning these steps were tested against Ubuntu 14.04.4 LTS running on Windows 10 1809) the first thing to try is using the native Linux package management tools to install Redis. This means running:

sudo apt install redis-server

From Ubuntu. I'll confess, it's so long since I installed WSL and Ubuntu I had to check what version of Ubuntu I had (which you can do by running the command lsb_release -a).

Unfortunately, this pulled down a version of Redis even older than the one I had available to me on Windows, 2.8.4:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libfreetype6 os-prober
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  libjemalloc1 redis-tools
The following NEW packages will be installed
  libjemalloc1 redis-server redis-tools
0 to upgrade, 3 to newly install, 0 to remove and 123 not to upgrade.
Need to get 410 kB of archives.
After this operation, 1,272 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/universe libjemalloc1 amd64 3.5.1-2 [76.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/universe redis-tools amd64 2:2.8.4-2 [65.7 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty/universe redis-server amd64 2:2.8.4-2 [267 kB]

Not helpful in the grand scheme of things, so next up was trying to pull and build from source. Before that I did take a moment to run Redis and make sure it worked. This eliminates anything I do with pulling down the latest bits and trying to use them from the equation if something doesn't work. Starting Redis is as simple as running the command redis-server from the command line in Ubuntu. Immediately up-popped a dialog from the Windows firewall to ask for network access permision for Redis. 

Checking Redis is working

Being someone who writes code, the easiest way to check that Redis is up and running for me was to use a simple example to write some data to the Redis server and then pull it back out. This involved using Visual Studio to create a .NET Core console app, adding a reference to the Stackexchange.Redis NuGet package and dropping in this code:

using StackExchange.Redis;
using System;

namespace RedisClientApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var redis = ConnectionMultiplexer.Connect("localhost");
            var database = redis.GetDatabase();
            database.StringSet("Key", "12345");

            var result = database.StringGet("Key");
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

With that code present, hitting F5 started the app and wrote the text 12345 to the console, which counts as a successful test for me. That's an app written in Visual Studio in Windows, writing and reading data to Redis running on Ubuntu in the Windows Subsystem for Linux. Success there means that I'm good to try upgrading to a later version of Redis and seeing if that plays ball as well.

Pulling the bits for the latest version of Redis

This is where it turned a little bit into a "run a series of commands that look like they make sense but I'm not familiar enough with Linux to know exactly what they do" moment. I used some instructions for Ubuntu 16.04, there's a link on the page that suggests it's instructions for 14.04 but unfortunately it's not. I'll repeat the commands to run here (for posterity on the off chance that the page is removed or changed in the future):

sudo apt-get update
sudo apt-get install build-essential tcl

cd /tmp
curl -O http://download.redis.io/redis-stable.tar.gz
tar xzvf redis-stable.tar.gz
cd redis-stable

make
make test

sudo make install

There are further instructions on the referenced page that're responsible for configuring Redis, but working on the assumption that the process of downloading and installing the latest version of Redis would either (a) use the configuration from the prior installation, or (b) come with a basic configuration sufficient to validate the install, next up is running the command:

redis-server

And hey presto, Redis 4.11 running under WSL:

Redis 4.11 running under the Windows Subsystem for Linux after pulling the source, compiling and installing

A final check that this is working properly by re-running my console app came up trumps, the app connected and saved/loaded data from Redis! The third line of the output from Redis does indicate that it's not picked up any config file and is instead using its defaults, which certainly appear to suffice for a basic does it work / have I screwed anything up test.

I guess the process of pulling the source, compiling, installing and then running Redis shows just how complete (for want of another word) the Windows Subsystem for Linux is. It's not just running existing / included binaries that works, it also allows you to pull down the code for an arbitrary application, build it and run it.

Next up - I wonder if there's a way to auto-run this so it starts every time I boot my machine, like any other Windows service?

About Rob

I've been interested in computing since the day my Dad purchased his first business PC (an Amstrad PC 1640 for anyone interested) which introduced me to MS-DOS batch programming and BASIC.

My skillset has matured somewhat since then, which you'll probably see from the posts here. You can read a bit more about me on the about page of the site, or check out some of the other posts on my areas of interest.

No Comments

Add a Comment