How We Moved Rails Development to Windows WSL2 Linux

July 29, 2020


Up till now we had been developing Rails 6 applications on linux using VMWare on a windows laptop. But now with the general availability of WSL 2, we can retire the old laptop and do everything together in one place.

Given that WSL 2 does not currently support GUI applications, we cant expect to replicate the Pepermint distro that was so nice to use. Instead we are taking the opportunity to retool to use VS code

Our toolset uncludes “Git”, “Rails”, “PostgreSQL”, and deployment to “AWS Beanstalk”

So here are the steps we took:-

1. Install the Windows Subsystem for Linux 2

This is a useful guide: https://dev.to/cdennig/wsl2-making-windows-10-the-perfect-dev-machine-391k

Follow these instructions: https://docs.microsoft.com/en-us/windows/wsl/install-win10

You may wish to pin your WSL 2 bash window to your task bar.

2. Install the Ubunto 20.04 LTS distro

You can install your favourite linux distro on WSL2 from the Micrsoft Store. Then make sure its using WSL2:

$ wsl --list --verbose
$ wsl --set-version Ubuntu-20.04 2

3. Install some dependencies for Ruby and Rails

## wsl2 bash:

$ sudo apt install curl
$ 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-get update
$ sudo apt-get install git-core 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 nodejs yarn

4. Set up rvm

## wsl2 bash:

$ gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
$ curl -sSL https://get.rvm.io | bash -s stable --ruby
$ ruby -v
$ echo "gem: --no-ri --no-rdoc" >> ~/.gemrc
$ gem update --system
$ rvm gemset list
$ rvm gemset use global
$ gem list
$ gem outdated
$ gem update
$ gem install bundler

5. Install Rails

## wsl2 bash:

$ rvm use ruby-2.7.0@rails6.3 --create
$ gem install rails
$ mkdir workspace
$ cd workspace
$ bundle install

6. Configure git

## wsl2 bash:

$ 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"
$ ssh -T git@github.com

7. Install PostgreSQL

We specifically needed version 9.5. Not all instructions include starting the service - which is required.

## wsl2 bash:

$ sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ focal-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
$ wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | $ sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install postgresql-common
$ sudo apt-get install postgresql-9.5 libpq-dev
$ service postgresql start

In the config steps below, swap “freddy” for your DB user name.

## wsl2 bash:

$ sudo -u postgres createuser freddy -s
## set a password for the user:
$ sudo -u postgres psql
## When psql runs, type '\password freddy' then enter password when prompted.
$ postgres=# \password freddy
## Ctl+z to exit psql.

If you need to install MySQL instead

$ sudo apt-get install mysql-server mysql-client libmysqlclient-dev

Setup master user & password:

## wsl2 bash:

$ sudo service mysql start
$ sudo mysql
## etc...

This might be worth a look: https://dev.to/hymanzhan/setting-up-wsl-2-for-web-development-3202

If you need Redis

$ sudo apt install redis-server
$ sudo service redis-server start --daemonize yes

8. Create a rails application

## wsl2 bash:

$ rails new newrails63pgapp -d postgresql

$ cd newrails63pgapp
$ echo "ruby-2.7" > .ruby-version
$ echo "default" > .ruby-gemset

## If you setup MySQL or Postgres with a username/password, modify the
## config/database.yml file to contain the username/password that you specified

## Create the database

$ rake db:create

## or do:

$ sudo -u postgres psql
$ postgres=# create user "freddy" with password 'your-password';
$ postgres=# create database "newrails63pgapp_dev" owner "freddy";
$ postgres=# create database "newrails63pgapp_test" owner "freddy";

$ rails server

## it works!

9. Fetch existing GitHub project

## wsl2 bash:

$ git clone https://github.com/Freddy/fred.git
$ cd fred
$ git remote set-url origin git@github.com:Freddy/fred.git
$ git remote -v
$ rvm use ruby-2.7@default --ruby-version --create
$ bundle install
$ echo "ruby-2.7" > .ruby-version
$ echo "default" > .ruby-gemset
$ rake -T

$ sudo -u postgres psql
$ postgres=# create database "fred_dev" owner "freddy";
$ postgres=# create database "fred_test" owner "freddy";

10. Install eb CLI

## wsl2 bash:

$ apt-get install \
    build-essential zlib1g-dev libssl-dev libncurses-dev \
    libffi-dev libsqlite3-dev libreadline-dev libbz2-dev

Then follow these instructions: https://github.com/aws/aws-elastic-beanstalk-cli-setup

11. Install VS code + extensions

You can install VS code from the Micrsoft Store.

The magic VS code extension that enables you to use WSL 2 for 100% of your development on Windows is “Remote - WSL” which you install from within VS code, from the Micrsoft Store.

Under the “help” menu the Introductory Videos might be worth a look as well as the Interactive Playground.

Then you can install a bunch of useful extensions that give you git tools, SQL tools and language-specific formattting & intellisence. Such as:

  • Project Manager
  • Slim
  • VSCode Ruby
  • Bookmarks
  • GitLens - Git supercharged
  • Rails
  • Ruby
  • Ruby Solargraph
  • SQLTools
  • SQLTools PostreSQL/Redshif Driver
  • vscode-gemfile
  • REST Client
  • Remote SSH
  • Resource Monitor
  • Test Explorer UI
  • Ruby Test Explorer

12. Debug your app in VS code

Follow this from section 4.3: https://www.endpoint.com/blog/2019/04/04/rails-development-in-windows-10-pro-with-visual-studio-code-and-wsl

In summary, you will:

a) Add these 2 gems and bundle install if not already there:

## gemfile:

group :development do
  ...
  gem 'ruby-debug-ide'
  gem 'debase'

b) Configure the debug runner and start it

# launch.json:

{
  "name": "Listen for rdebug-ide",
  "type": "Ruby",
  "request": "attach",
  "cwd": "${workspaceRoot}",
  "remoteHost": "127.0.0.1",
  "remotePort": "1234",
  "remoteWorkspaceRoot": "/home/freddy/workspace/fred"
}

c) using the console within VS code: run rails:

## console within VS code:

$ rdebug-ide ./bin/rails server webrick

d) open your browser at http://127.0.0.1:3000/, add some break points, enjoy!

© 2020 Keith P | Follow on Twitter | Git