According to the 2020 Ruby on Rails Community Survey, 3% of surveyed Rails community members use Windows. The reason for this is heavily in part that Ruby was developed for Unix-based systems (Linux, macOS), with some key Ruby gems/dependencies/libraries not being optimized or even compatible with Windows out of the box.
Traditional solutions to this problem include the POSIX-providing Cygwin, RubyInstaller, which supports native Windows programs through MSYS2, and more recently, WSL (Windows Subsystem for Linux). See here for a good high-level comparison of Windows options for Ruby development. This tutorial will cover installing WSL.
Note: WSL requires Windows 10, so if you are not on it, you cannot follow this tutorial and would have to use some of the other options outlined above.
Note: More detailed instructions can be found at Microsoft’s WSL docs here.
Run the following as Administrator in PowerShell to enable WSL:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Installing Ruby on Rails
Note: The remainder of this post will follow an abridged version GoRails’ installation guide, credit to them! More detailed instructions can be found at their guide here.
Install the following Ruby dependencies in your newfound Ubuntu environment by running the following:
sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev
Run the following install commands:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELLgit clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
Using rbenv, you can now install a Ruby version, we’ll use 2.7.2:
rbenv install --verbose 2.7.2
rbenv global 2.7.2
rbenv install [your version] installs the Ruby version (--verbose helps track the installation progress), and rbenv global [your version] sets the default Ruby version used by your system.
Please verify that your Ruby is properly installed with the following command:
Install Bundler to manager our gem dependencies by running the following commands:
gem install bundler
You’ll likely want to configure GitHub in your environment, to do so:
git config --global user.name [your name]
git config --global user.email "[your email]"
ssh-keygen -t rsa -b 4096 -C "[your email]"
Paste the contents of your SSH key into your GitHub authorized keys, then check if your Git says you are successfully authenticated with the following command:
ssh -T firstname.lastname@example.org
We’re now ready to install Rails! Run the following commands, which install Rails (we’ll use 6.1.1), NodeJS, and Yarn:
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
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-get install -y nodejs yarn
gem install rails -v 6.1.1
Please verify that your Rails is properly installed with the following command:
Finally, you’ll likely want PostgreSQL for your Rails database, install it here and then run the following command to add the gem to support it:
sudo apt install libpq-dev
Bonus (optional): Configure remote WSL Ruby SDK in RubyMine
According to the 2020 Ruby on Rails Community Survey, 15% of surveyed Rails community members use RubyMine as their editor. For help deciding which is best for you, here’s a great article on a few. For our purposes, this bonus will cover RubyMine configuration.
First off, be sure to install RubyMine. Once done and in a project, navigate to Settings → Languages & Frameworks → Ruby SDK and Gems.
Hit the add button (+) → New remote…, and you should be prompted to configure it.
Determine your Ruby version with the following command (2.7.2 if you followed this tutorial):
Select WSL and your Linux distribution (Ubuntu if you followed this tutorial) from the dropdown. For Ruby or version manager path, enter the following, replacing [your version] with your Ruby version and [your Windows username] with the username used for your Windows filesystem:
/home/[your Windows username]/.rbenv/versions/[your version]/bin/ruby
If successful, you should see something like this:
Select the remote Ruby SDK by clicking the radio button. Next, navigate to Tools → Terminal, and replace the Shell path with the following:
Feel free to name it anything you’d like, such as WSL, and hit OK.
Let’s check our work! Select the Terminal from the bottom of RubyMine. If configured properly, it should open Ubuntu directly rather than Windows command prompt.
If you’ve done so, nice job! 👨💻
Congratulations! You now have Rails configured on Windows 10 through WSL. 🥳
Wondering where to go next?