The Gift and Curse of WSL


Legend says if you use WSL you automatically become a hacker. Graphics courtesy of undraw.co

I’ve been using the Windows Subsystem for Linux (WSL) for a while now and let me tell ya, for Ruby development, it’s a life-saver. 

The amount of time that it saves in making sure that your Ruby or Rails application works on Windows is just amazing! Gone are the days of work-around software like PuTTy and the Cygwin terminal to make things happen. 

But (cue the shadows and torch light under my face) there is a dark side too. There’s the cryptic errors and frustrating troubleshooting. The long, sleepless nights getting WSL itself to work. The … You get the idea.

Why am I talking about this now? My PC was recently wiped and I needed to set everything up again. So, in this journey post I want to go through all the issues I had setting up WSL and Rails again.


First things first


If you’re interested in setting up Rails on your PC, you can follow this amazing guide from GoRails - that being said, I have found a few things to be missing on it and I’ll be filling in the gaps here.

First, when installing WSL, you need to run a few powershell scripts:

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

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

This didn’t actually work for me. I ended up needing to do the following to get things working:

  1. Open your start menu and search for “Turn Windows Features On or Off”

  2. Scroll down that list and find where it says “Windows Subsystem for Linux”

  3. If you’ve already run the powershell scripts, that option should already be checked. In which case uncheck it and restart your PC

  4. Then do steps 1, 2 and 3 again, but this time, make sure the option for WSL is checked and then restart your PC.

Then you can proceed to the next step - which is updating it to WSL 2 (The GoRails guide seems to be installing WSL 1). 

As per this guide from Microsoft, you first need to download the “Linux kernel update package” (See step 4 of the above guide). After you download the package, you can run it. If you haven’t already done the whole uncheck-restart-check-restart process I outlined above, this will fail - so make sure to do that before you run this. 

After running that package, you can proceed to Step 5 of the guide and open a Powershell window (with admin privileges) and run:

wsl --set-default-version 2

To make sure that you set the default version of WSL to WSL 2. 

From this point onwards, you can go back to following the GoRails guide and install Rails. 


But what if it fails to launch again?


That’s ridiculous! Why would it fail to run if it works the first time

And to that I say, “🤷🏽‍♂️”, but it did. I shut down my PC, content after a long day of installing WSL and getting it to work, only to be greeted the next day with this error:

Bad error is bad


“But I thought I fixed this yesterday with the whole ‘uncheck-restart-check-restart’ thing 😫” - yeah, turns out no. So, I did some more digging and found this solution on the WSL GitHub issues page and updating to Windows version 2004 fixed it for me - why? 🤷🏽‍♂️


Temporary failure resolving <insert endpoint here>


But wait! There’s more!

The first step to install Rails is to run “sudo apt-get update” to make sure your Linux distribution is up to date and is grabbing the up to date files. However, I ran into the following error with that command:

Temporary failure resolving 'security.ubuntu.com'

This took me a while to fix, but apparently stemmed from some bad DNS on my WSL. Using the advice from this GitHub issue comment, I finally managed to get it working by running: 

echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf


Operation not permitted


🙅🏽‍♂️


The next issue cropped up when running “rails new” to create a new rails app - it fails noting that the operation is not permitted. 

A quick google search brought me to this SO Answer and helped me fix it, by running:

sudo umount /mnt/c 

sudo mount -t drvfs C: /mnt/c -o metadata


Finally, PostgreSQL errors


The last thing I ran into was PostgreSQL errors. As per the GoRails guide, I installed postgres on Windows and added the details of my Postgres user to the “database.yml” file on my rails project - be sure to set your default db host as “localhost”, like so:

default: &default
  adapter: postgresql
  encoding: unicode

  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: localhost


But running “rake db:create” fails - first with the error that it cannot recognize the psql command. I managed to fix this by running this (from this SO Answer):

sudo apt install postgresql-contrib libpq-dev

This fixes the inability to recognize psql. But then it fails to connect to the server. I’ve solved this before, with the help of this Medium article, where we need to run:

psql -p 5432 -h localhost -U postgres

To launch the psql instance. This will ask you for your password, so make sure you remember what you entered when installing postgres - if not, follow this guide to reset the password.

If running the above command (with the right password) gives you an error regarding the TCP connection on port 5432 or that the server is not started, then make sure that:

  1. You have opened port 5432 on your firewall - follow this guide for that.


  2. Run “sudo /etc/init.d/postgresql start” to make sure that the server is running (Thanks @benvc 🙏🏽). In my experience, this is only needed the first time, but your mileage may vary.

...

And you’re done! Now, you can use the power of WSL to more easily build your Rails apps!

On a final note, do remember that to access the files you have on your Windows system, you need to go through the “/mnt/c” directory - I always found it annoying to have to go that way to get to any files, so instead I created an alias to quickly “cd” to my code directory. You can find a guide to that here.

And with that, happy coding!