Installing Ruby on Rails... on Windows 10


Maybe you're moving from a Mac to a PC and you're used to Rails, or maybe you got stuck in the Rails installation guide and went down 100 different Stack Overflow rabbit holes and got nowhere. Well, i've been there and I can tell you that it's not impossible, we can do this!

The first part of this guide is almost exactly the Gorails Setup guide. If you've already gone through that, and you are stuck on the Postgresql setup - you can skip to that section. This tutorial also heavily leans on the blog How to set up PostgreSQL on Ubuntu for WSL by Karan Singh. This was the lifesaver that kept me from throwing my laptop out the window and paying Tim Cook for an overpriced machine. This blog also uses a solution on this Stack Overflow question. With these three sources combined, I was able to successfully install Rails on Windows and you can too!

There are a few things you need right off the bat. Windows Store (ships with Windows), and an up-to-date Windows version (Build 19041 and higher, you can check your version by using WIN+R and entering winver)

For convenience, let's grab Windows Terminal. This will allow you to have multiple tabs open at the same time and works with Ubuntu so you can quickly move back and forth between PowerShell/CMD and Ubuntu.

Speaking of Ubuntu, let's go ahead and install that next. This will have all the usual commands and shortcuts that you're used to. Whenever you want to make a new Rails app or start your server, you'll use Ubuntu. When you install it, you'll make a username and password - they can be unique if you want them to, but make the password something you'll remember. You'll use that whenever you use the `sudo` command.

FROM HERE ON IN, USE UBUNTU ONLY FOR THIS INSTALLATION. Think of this like you're using a simulation of a Mac inside Ubuntu. You'll need to configure Git here as if you didn't in your Powershell.

When you're on Ubuntu, the root folder is different when you're on a PC - there is no 'root' folder for a PC. So when you want to navigate to other folders in your computer, you'll have to go to /mnt/c -> which is the root of your C drive.

So the next step is to start on the Rails installation. You can follow along there or just stick to this page.

This will install some dependencies that you'll need for Ruby

sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libpeadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev

Now we'll install Ruby with Rbenv with the following commands

cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

Then to get the specific Ruby version we want, we use the following commands utilizing Rbenv to get 3.03

rbenv install 3.0.3
rbenv global 3.0.3

Confirm you've got Ruby installed..

ruby -v

Then install the Bundler gem

gem install bundler
rbenv rehash

Next we'll install Git (change the red text to your login info)

git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"
ssh-keygen -t rsa -b 4096 -C "YOUR@EMAIL.com"

The Gorails tutorial has us set up an SSH key for GitHub - but that's been depreciated from what I understand. GitHub now uses Personal Access Tokens - you can find this setup in your GitHub settings > Developer Settings > Personal access tokens. Following the GitHub setup shouldn't spark any issues.

Next, we'll get Node and Yarn. These are more dependencies for Rails, and hey you might use them in the future.

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

Finally we install Rails!

gem install rails -v 7.0.0.rc1
rbenv reshash

Now the Gorails tutorial suggests that you make all of your projects in the /mnt/c/*** files. Again when you're on Ubuntu, the root folder is different when you're on a PC - there is no 'root' folder for a PC. So when you want to navigate to other folders in your computer, you'll have to go to /mnt/c -> which is the root of your C drive.
I've found that rails commands take FOREVER when you're going through /mnt/c/etc. I've moved all my projects to the 'root' folder in Ubuntu and haven't had any problems.

Now we'll install Postgresql. Here's where the Gorails tutorial stops being helpful, as it asks us to install it through the Microsoft Store. This just led me to huge headaches & I ended up having to uninstall EVERYTHING and start again. Thankfully I found an amazing blog that I'll synthesize here.

sudo apt-get install postgresql

Next, we'll need to set up a password for Postgres, you can make it the same as any of your other passwords if that makes it easier. Basically, you can set up different users that have different database permissions.

sudo passwd postgres

Now we'll need to edit the postgres.conf file. This is hidden deep in your computer, and you can edit it like the GoRails guide suggests with the following command (the version might be different to what you installed):

sudo vi /etc/postgresql/10/main/postgresql.conf

Instead of learning how to edit it that way, I simply used the Everything program to search for that file and edit it in VSCode. I found it in:

C:\Users\Administrator\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs\etc\postgresql\12\main\postgresql.conf

Once you're inside - you'll want to edit one part that's commented out. Uncomment and edit it so that:

data_sync_retry = true.

Up next is the most important command you'll need!! This starts the PostgreSQL service on your machine, so Rails can finally create databases.

sudo /etc/init.d/postgresql start

You'll want to save that command - whenever you restart your PC, it won't start that process immediately. So you can run it whenever you boot up and want to start some Rails projects or make a simple script for yourself that runs it on bootup.

If you're having issues with other Rails projects that you've worked on and are now cloning on your PC, check out this Stackoverflow post. You'll have to go back into that postgresql.conf file again and change the port that you're using or change the listen addresses.

And there you go! Hopefully, you didn't have any issues following this, but if you did- please email me at daveryan145@gmail.com and I can revise this guide for future friends!