Here is our tutorial on how to install Ruby on Rails on Windows 10 using the Windows Subsystem for Linux. This tutorial uses code from a post by Go Rails, which can be found here, with some minor tweaks.

For this installation, we will be installing PostgreSQL as our database, which is great if you run your app on Heroku.

Update: You can now read an updated version of this tutorial by clicking here.





Setting Up Ruby

Installing the Ruby Dependencies:

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 python-software-properties libffi-dev

Install Ruby using rbenv:

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.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

rbenv install 2.5.1
rbenv global 2.5.1
ruby -v

Installing Bundler:

gem install bundler

To install Git, follow the Go Rails guide here.

Setting up and installing Rails

First install NodeJS:

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

Now install Rails:

gem install rails -v 5.2.0
rbenv rehash
rails -v
# Rails 5.2.0

Next we need to install the dependencies for the pg gem, but not PostgreSQL itself:

sudo apt-get install libpq-dev

Once this is complete, download and install PostgreSQL on Windows - not on the Windows Subsystem for Linux. Click Here to download and install the latest version of PostgreSQL.

Follow the instructions and create a new password. Also remember your default username is postgres.

Awesome - Now let's set up your new Rails app:

Note: In order for PostgreSQL to work, you will need to create your new app outside of the Bash environment.

Here's an example:


cd /mnt/c/Users/your-computer-user-name/Desktop
mkdir Rails
cd Rails

rails new appname -d postgresql

Now open and modify your database.yml file using your favourite text editor. Add your username and password as well as host.

username: postgres #Or a new user you set up inside pgadmin
password: your-password
host: localhost

Last step:

cd appname
rake db:create

rails s

Congrats! Your new app should be up and running on localhost:3000.

Notes:

To navigate to the folder where Ubuntu is installed, follow this path:

C:\Users\Your-Computer-Username\AppData\Local\lxss
C:\Users\Your-Computer-Username\AppData\Local\lxss\home #App folder for SQLite

Update:

If you installed Ubuntu through the Windows store, or if you are on a newer Windows build, these file paths are now hidden by default. This is to prevent you from using Windows tools to edit these files. However, if you do want to see these files, open up your quick access toolbar and paste this in %userprofile%\AppData\Local\Packages

This should lead you to the right folder: mine looks like this: C:\Users\user-name\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\home

To learn more about this and making these folders visible by default, please Click Here.

To purge or remove the Windows Subsystem for Linux, including your Rails install, open command prompt, not Bash and type:

 lxrun /uninstall # This will leave your home folder intact
 lxrun /uninstall /full # This will delete everything

To learn more about deleting your Bash environment, Click Here.

Update:

If you installed Ubuntu through the Windows store, you can now simply go to add/remove programs as you normally would and uninstall Ubuntu.