Scott Hanselman

Ruby on Rails on Windows is not just possible, it's fabulous using WSL2 and VS Code

July 26, 2019 Comment on this post [12] Posted in Linux | Open Source | Ruby | Win10
Sponsored By

I've been trying on and off to enjoy Ruby on Rails development on Windows for many years. I was doing Ruby on Windows as long as 13 years ago. There's been many valiant efforts to make Rails on Windows a good experience. However, given that Windows 10 can run Linux with WSL (Windows Subsystem for Linux) and now Windows runs Linux at near-native speeds with an actual shipping Linux Kernel using WSL2, Ruby on Rails folks using Windows should do their work in WSL2.

Running Ruby on Rails on Windows

Get a recent Windows 10

WSL2 will be released later this year but for now you can easily get it by signing up for Windows Insiders Fast and making sure your version of Windows is 18945 or greater. Just run "winver" to see your build number. Run Windows Update and get the latest.

Enable WSL2

You'll want the newest Windows Subsystem for Linux. From a PowerShell admin prompt run this:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

and head over to the Windows Store and search for "Linux" or get Ubuntu 18.04 LTS directly. Download it, run it, make your sudo user.

Make sure your distro is running at max speed with WSL2. That earlier PowerShell prompt run wsl --list -v to see your distros and their WSL versions.

C:\Users\Scott\Desktop> wsl --list -v
NAME STATE VERSION
* Ubuntu-18.04 Running 2
Ubuntu Stopped 1
WLinux Stopped 1

You can upgrade any WSL1 distro like this, and once it's done, it's done.

wsl --set-version "Ubuntu-18.04" 2

And certainly feel free to get cool fonts and styles and make yourself a nice shiny Linux experience...maybe with the Windows Terminal.

Get the Windows Terminal

Bonus points, get the new open source Windows Terminal for a better experience at the command line. Install it AFTER you've set up Ubuntu or a Linux and it'll auto-populate its menu for you. Otherwise, edit your profiles.json and make a profile with a commandLine like this:

"commandline" : "wsl.exe -d Ubuntu-18.04"

See how I'm calling wsl -d (for distro) with the short name of the distro?

Ubuntu in the Terminal Menu

Since I have a real Ubuntu environment on Windows I can just follow these instructions to set up Rails!

Set up Ruby on Rails

Ubuntu instructions work because it is Ubuntu! https://gorails.com/setup/ubuntu/18.04

Additionally, I can install as as many Linuxes as I want, even a Dev vs. Prod environment if I like. WSL2 is much lighter weight than a full Virtual Machine.

Once Rails is set up, I'll try making a new hello world:

rails new myapp

and here's the result!

Ruby on Rails in the new Windows Terminal

I can also run "explorer.exe ." and launch Windows Explorer and see and manage my Linux files. That's allowed now in WSL2 because it's running a Plan9 server for file access.

Ubuntu files inside Explorer on Windows 10

Install VS Code and the VS Code Remote Extension Pack

I'm going to install the VSCode Remote Extension pack so I can develop from Windows on remote machines OR in WSL or  Container directly. I can click the lower level corner of VS Code or check the Command Palette for this list of menu items. Here I can "Reopen Folder in WSL" and pick the distro I want to use.

Remote options in VS Code

Now that I've opened the folder for development WSL look closely at the lower left corner. You can see I'm in a WSL development mode AND Visual Studio Code is recommending I install a Ruby VS Code extension...inside WSL! I don't even have Ruby and Rails on Windows. I'm going to have the Ruby language servers and VS Code headless parts live in WSL - in Linux - where they'll be the most useful.

Ruby inside WSL

This synergy, this balance between Windows (which I enjoy) and Linux (whose command line I enjoy) has turned out to be super productive. I'm able to do all the work I want - Go, Rust, Python, .NET, Ruby - and move smoothly between environments. There's not a clear separation like there is with the "run it in a VM" solution. I can access my Windows files from /mnt/c from within Linux, and I can always get to my Linux files at \\wsl$ from within Windows.

Note that I'm running rails server -b=0.0.0.0 to bind on all available IPs, and this makes Rails available to "localhost" so I can hit the Rails site from Windows! It's my machine, so it's my localhost (the networking complexities are handled by WSL2).

$ rails server -b=0.0.0.0
=> Booting Puma
=> Rails 6.0.0.rc2 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.2-p47), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

Here it is in new Edge (chromium). So this is Ruby on Rails running in WSL, as browsed to from Windows, using the new Edge with Chromium at its heart. Cats and dogs, living together, mass hysteria.

Ruby on Rails on Windows from WSL

Even better, I can install the ruby-debug-ide gem inside WSL and now I'm doing interactive debugging from VS Code, but again, note that the "work" is happening inside WSL.

Debugging Rails on Windows

Enjoy!


Sponsor: Get the latest JetBrains Rider with WinForms designer, Edit & Continue, and an IL (Intermediate Language) viewer. Preliminary C# 8.0 support, rename refactoring for F#-defined symbols across your entire solution, and Custom Themes are all included.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service
July 31, 2019 10:01
Holy cow, Scott! I gave Ruby on Rails on VM on Windows and RoR on Plain Old Windows a valiant try for a couple of years, back around 2007, then gave up in disgust. Amazing that it only took a decade to finally get a working environment on Windows, and it's STILL Linux! Kudos for another great, clearly written how-to, maybe I'll give it another go just for fun.
July 31, 2019 17:12
Hi Scott! Thank you for the great article! Just noticed that the link to VSCode Remote Extension pack is broken.
August 01, 2019 14:46
I always have loved your articles, they are so thorough... For instance, you actually tell us how to get WSL in the terminal by editing the JSON file... No other article has mentioned that. I am super excited to try those out. Right now we are using an old Ubuntu 32 bit VM and Rubymine for development and I loath to use VS Code natively.
August 01, 2019 21:28
Soooo, this is not Ruby on Windows, this is Ruby on Ubuntu hosted on Windows, amirite? So people, move on, nothing to see here, especially if you are already using a real operating system...
August 01, 2019 23:57
I have run large Ruby on Rails applications on WSL 1 successfully on my laptop on Ubuntu 18.04. They run a bit slow, but run fine otherwise. The reason I mention that is you may not be able to update your windows installation to Windows Insiders release like me due to company policies, but you can work with the current release of WSL today
August 02, 2019 0:20
@Bharat, have you been using RubyMine IDE together with WSL 1?

I am curious if that combo is a viable set-up to develop Ruby on Rails on Windows 10 stable.
August 02, 2019 2:39
I just made the jump to Fast to get this. Some issues though.


  1. Terminal was borked for Ubuntu until a reboot after converting to WSL 2

  2. I never did find how to connect to Windows Postgres from inside WSL 2 so I installed one in Ubuntu instead

  3. My parallel Selenium tests with headless Firefox don't seem happy - one at time seems Ok

  4. Windows Sublime with Linux files doesn't detect changes, i.e when you change Git branches



It is a whole lot faster with all the files in the WSL system and not shared from Windows.
Tom
August 02, 2019 12:44
Thanks for an interesting article. I am curious, what are the advantages of your setup compared to running Ubuntu via Virtual Box? I've been using this setup for many years now, I wonder if there are reasons to change it to WSL2.
August 02, 2019 13:22
Could you dive a little deeper into debugging? I can't get it to work.
August 03, 2019 0:13
@Joto, I use VSCode. Currently, in WSL-1 there are some restrictions in the sense that you should not write anything on the Linux side from Windows, whereas you can write anything that you want to the Windows disk. It takes a bit of getting used to. As I understand, WSL-2 has no such restrictions. Rubymine should be fine. Just install it on Windows and run your Ruby on Rails SERVER on linux :)
August 08, 2019 3:55
Hmm my wsl.exe has a different set of commands, do I need to get a newer version or something?

wsl --help
Copyright (c) Microsoft Corporation. All rights reserved.
Usage: wsl.exe [Argument] [Options...] [CommandLine]

Arguments to run Linux binaries:

If no command line is provided, wsl.exe launches the default shell.

--exec, -e <CommandLine>
Execute the specified command without using the default Linux shell.

--
Pass the remaining command line as is.

Options:
--distribution, -d <DistributionName>
Run the specified distribution.

--user, -u <UserName>
Run as the specified user.

Arguments to manage Windows Subsystem for Linux:

--export <DistributionName> <FileName>
Exports the distribution to a tar file.
The filename can be - for standard output.

--import <DistributionName> <InstallLocation> <FileName>
Imports the specified tar file as a new distribution.
The filename can be - for standard input.

--list, -l [Options]
Lists distributions.

Options:
--all
List all distributions, including distributions that are currently
being installed or uninstalled.

--running
List only distributions that are currently running.

-setdefault, -s <DistributionName>
Sets the distribution as the default.

--terminate, -t <DistributionName>
Terminates the distribution.

--unregister <DistributionName>
Unregisters the distribution.

--upgrade <DistributionName>
Upgrades the distribution to the WslFs file system format.

--help
Display usage information.
August 08, 2019 4:00
ah, from the fine print I can see that WSL2 requires 18917 and I am still on 18362 like some sort of caveman

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.