K Cartlidge

C#/DotNet. Go. Node. Python/Flask. Elixir.

Ruby on Rails on Windows, using Windows Subsystem for Linux 2

Most advice out there around Ruby, Rails, and Windows, says to use WSL 2 for running Ruby or Ruby on Rails on Windows 10.

That's good advice, as it results in a complete Linux installation within Windows. Ruby is easier to install on Linux and Mac than Windows, as the options on the latter usually reduce down to a global install of a single version.

Pre-requisites

Make sure your system supports Hyper-V, which may need switching on at a BIOS level.

Installing WSL and upgrading to WSL 2

Installing WSL 2 requires doing WSL 1 first then upgrading it.

The first command (below) shows your current Windows 10 version. Ensure you have done enough Windows Updates to reach at least Version 1903, which is OS Build 18362.

The next two commands install WSL 1 and enable the Virtual Machine Platform. These take virtually no time at all to run. You can do these in either a Command Prompt or Powershell.

winver
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Now restart your system, and it will install the necessary stuff.

WSL wizard Download and run the WSL2 Linux kernel update package for x64 machines. This is only about 15MB, so quick to download, and you'll get to see a Microsoft installation window with a penguin!

Set the default WSL to WSL 2, either in a Command Prompt or using Powershell.

wsl --set-default-version 2
wsl -l -v

The command above will announce that you have zero distros installed. It will also offer a link (usually this) for finding one.

WSL Ubuntu Install Ubuntu 20+ or similar and configure it using the instructions it comes with. Click the Launch button when it is installed.

Subsequent launches depend upon the distro. For Ubuntu, just run this from any command prompt or Powershell:

ubuntu

Your available distros can be listed again, including their run status, by repeating the wsl -l -v command. You can do this in either a Command Prompt or Powershell.

wsl -l -v

Installing Ruby into WSL 2

Launch your distro. I'm assuming Ubuntu 20 from here on in.

ubuntu

To do the install you first need to add the Ruby Version Manager.

sudo apt install software-properties-common # may already be there
sudo apt-add-repository -y ppa:rael-gc/rvm
sudo apt update
sudo apt install rvm
echo 'source "/etc/profile.d/rvm.sh"' >> ~/.bashrc

Drop out of your WSL Ubuntu and go back in again. Then use rvm to list available Ruby versions.

rvm list known

If you look at the start of the list you'll see the 'typical' Ruby versions. Pick a version and install it like the following.

rvm install 2.7
rvm list
ruby --version

If you have a Ruby script with a shebang/hashbang, it will probably expect to find /usr/bin/ruby. You can arrange that via symbolic linking, for example:

sudo ln -s /usr/share/rvm/rubies/ruby-2.7.0/bin/ruby /usr/bin/ruby

If you switch Ruby versions using rvm remember to update this link too. Otherwise, your script and your prompt/shell will be using different versions.

You can open Windows Explorer to a WSL folder with explorer.exe . where the .exe extention is required and the trailing . can be replaced with any folder path.

Setting up Visual Studio Code for using across Windows and WSL 2

If you use VS Code for your development, launch it from the command line inside your Ubuntu.

cd <your-code-folder>
code .

It will download VS Code server (again, within your Ubuntu), then launch VS Code under Windows 10.

VS Code will now prompt you to install the WSL extention which will allow it to run in Windows against your Ubuntu source. Accept it, and make sure you read the extention details for further information.

Install the Ruby extention from Leng Lv (rebornix.ruby).

VSCode Ruby Language Server Enable the language server option in the VS Code preferences.

Then add a sample Ruby script.

print("Hello, cruel world.")

Back in your original prompt, a quick ls will show your new script.

If you open up a terminal pane in VS Code (Terminal, New Terminal) you'll see it opens up in the same folder as your file. As that file is in your Ubuntu, you've got an Ubuntu shell prompt ready to go.

If you close down running stuff, starting VS Code from scratch will start everything else as needed.

Install Node

You also need to install Node for the front-end stuff. To do that we use nvm in a similar fashion to rvm. That link has the latest installation details; currently it reduces to this:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.0/install.sh | bash

To finalise the nvm install, drop out of Ubuntu and re-enter it. You can now install and use a version of Node using the following commands.

The first line shows available versions. The second can be changed to use a different version.

nvm ls-remote
nvm install 12.19.0
node --version

Finally install yarn, a package manager that sits above the default npm.

npm i -g yarn
yarn --version

To be blunt (and opinions vary) yarn doesn't really offer enough to be worth the switch from npm, but it did when it first arrived and Rails expects it. So whatever.

Installing Rails

This is the easy bit, though it can take a while on a slow machine as native extentions are built.

gem install rails
rails -v

Now you can create an example app.

rails new example
cd example
rails server

Visit it at localhost:3000.