This is Part 2 in the WSL 2 Rails Series
Windows 10 And Ruby On Rails - What Gives?
Today we’re going to take a look at installing Ruby On Rails in Windows 10, but first, it’s important to understand why we’re using WSL to run Ubuntu. Let’s start with why we don’t just do things with a Windows installer? After all, installing Ruby on Windows 10 is as easy as navigating to RubyInstaller.org, so why can’t we do something similar when we try to install Rails on Windows 10?
Well, it’s complicated. You see, in the past that’s largely what we had to do, and wow was it a headache to deal with. That’s because the gems (Ruby On Rails plugins are called Gems) would run into build issues. These issues would result in some severe head-scratching and Stack Overflow diving. Debugging Nokogiri not working was actually my first Rails 4 experience!
These days, however, Windows developers by far prefer the route of using the Windows Subsystem for Linux (Version 2). That’s because it effectively gives Windows developers the Ubuntu experience inside their Windows 10 OS, without needing to dual boot. It’s as simple as opening up a terminal and getting started! So assuming you followed along with part 1 then you can follow these Ubuntu commands as if you’re working on a real Linux distribution!
Looking to learn more about WSL 2? Check out this wonderful textbook full of tips, tricks, and configuration options for WSL 2. The textbook is available via my affiliate link to the book’s store page on Amazon: WSL 2 Tips and Trips
Originally Posted on YouTube
This is part two of my three part video series covering installing Linux and Ruby on Rails in Windows 10, and then setting up the development environment with Visual Studio Code plugins.
Part 1 covered the WSL 2 setup in under 5 minutes to run Linux on Windows 10. A large portion of the time spend was dedicated to downloading and installing Windows updates. Which is a fancy way of saying you’ll be forced to stare at the “Windows Is Updating – 0%” screen.
This part covers installing Ruby on Rails in the Linux Subsystem for Windows 2 installation that we set up in the previous part. WSL 2 will effectively give you the Linux Rails experience on a Windows PC.
The final part will cover installing plugins in Visual Studio Code for Rails. These extensions will handle code formatting, syntax highlighting, code auto-completion, and more.
Step 1: Installing The Ruby Version Manager And Ruby
To begin, we need to add the necessary key server and perform a curl to rvm.io. Afterward, we’ll need to restart the terminal.
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 \ 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable --ruby
The next step will be to check and ensure that we have RVM installed. Then we can run the longest command of this tutorial which is the Ruby Installer command. Now, the video covered Ruby version 2.7.0, but as of this article the latest version is 2.7.1. The point is, you’ll want to use “rvm list known” to grab the latest, and not just the specific version I mention in either tutorial.
rvm version
rvm get stable --autolibs=enable
rvm list known
rvm install ruby-2.7.0
rvm --default use ruby-2.7.0
ruby -v
Step 2: Installing Node JS
Following the successful installation of the Ruby Version Manager and your desired version of Ruby, it’s time to install the latest version of Node JS. The reason is that Ruby on Rails 6 relies on Webpacker and some Node goodness. So although this doesn’t seem like a required step, it will be when it comes time to running your applications. Thankfully, the commands are very simple.
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt install -y nodejs
sudo apt install gcc g++ make
And that’s all it takes to install Node JS. We can now turn our attention to the true end game: Ruby On Rails and its installation process. Spoilers: it’s going to involve Yarn.
Step 3: Installing Ruby On Rails On Ubuntu On Windows 10! Wow, that's a mouthful.
We’re going to bundle (punintended, I assure you) the Ruby on Rails steps with Yarn. Installing Rails is as easy as installing any other gems at this point, so this will save us an entire section. We’ll start by updating our system gems, and then we’ll install Rails and Yarn.
gem update --system
gem -v
gem install rails
rails -v
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn
yarn --version
Now assuming all of those commands worked as intended, you should now be the proud owner of a working Rails 6 installation on Windows. And it’s all being powered by the Windows Subsystem For Linux Version 2! Let’s take it out for a test drive, shall we?
Step 4: Creating A Basic Rails Application
Due to the nature of running two operating systems in a configuration like this, you’ll want to ensure that you create your Ruby On Rails projects inside an Ubuntu specific directory. The reason being the file system there isn’t shared between Windows and Linux. If you do pick a directory where the file systems are shared, you’re likely to experience a slowdown of anywhere from 10 to 100 times. This is showcased in the video version of this tutorial over on my YouTube channel.
For the purposes of this tutorial, we’ll create a completely open directory at the root of our installations. We’ll call it code, and create a basic Rails project with the ability to create posts. Each of these posts will have a title of type string and a body of type text. Any time that you see the command say “rails s” you’ll be able to navigate to localhost:3000 and see your Rails app. Just remember, if you want to go back to running commands, be sure to press CTRL + C in your terminal to close your server. Otherwise your commands won’t actually run and things will look weird.
cd /
sudo mkdir code
sudo chmod 777 code
cd code
rails new demoproject
cd demoproject
rails s
rails g scaffold post title body:text
rails db:migrate
rails s
Presumably at this point you should be able to open your browser of choice, navigate to localhost:3000/posts and see your new Ruby On Rails application up and running!
Conclusion
I hope that this tutorial was helpful for some of you. I’m still pretty new to writing these text versions of my video tutorials, but I’m starting to get the hang of things I believe. The final part of this mini series will cover adding syntax highlighting, code formatting, and code completion in Visual Studio Code. Just as a note, VS Code is actually capable of traversing your WSL 2 installation. Additionally, it also allows you to type “code .” in your terminal to open up the directory in VS Code!
Hopefully this helped someone out there. Thank you for making it to the end of this article, I really appreciate and value your time. See you next time, hopefully!
4 replies on “WSL 2 With Ruby On Rails in Windows 10”
Hopefully this new comment section works a bit better!
hey there is a typo in this command curl -sSL https://get.rvm.io | bash -s stable –rubyt it should be curl -sSL https://get.rvm.io | bash -s stable –ruby (no t at the end)
Oops! Sorry about that, I’ve updated the post with the correct command!
Installed RVM and Ruby On WSL2 Ubuntu
Need to run ruby commands from PowerShell like this
PS C:\>wsl ruby -v
But got this returned
/bin/bash: ruby: command not found