Getting a Rails app to run on Bash on Ubuntu on Windows

I’m the newest team member at DevMynd, and so far, the only one with a Windows PC. Since we do most of our work in Ruby on Rails, and since everyone else has Macs, this presents a challenge.

If I wanted to do this the easy way, I’d simply run Rails on Windows natively. But I decided instead to do it the hard way and use Linux and BASH. First, I had a couple of free days to get up to speed last week giving me some time to experiment. Second, I figured that running on a completely different platform might introduce hard-to-diagnose bugs. (Both Linux and MacOS are *nix operating systems while Windows is not). I hope that what follows will make this a considerably less-hard way for everyone else.

This is how I got everything working on a Windows 10 Creator Edition (build 10.0.15603) box using Bash on Ubuntu on Windows also known as Bash/WSL. WSL (Windows Subsystem for Linux) is a beta feature of Windows 10 so some of what follows may be different on other versions of Windows.

Also, this post assumes that you are coming into the Linux world for the very first time, and your mind is a deep, pure, clear lake of Windows expertise completely unencumbered by any knowledge of Linux whatsoever. If you already know Linux, a lot of this might be redundant.

Important!

  • Don’t change Linux files using Windows apps and tools. You will hose your Linux environment. That said, since your Bash/WSL environment can read the Windows file system, you can just point to it from within BASH to run your Rails project.
  • Read the  Bash on Ubuntu on Windows FAQ.

Steps

1.  Turn on Developer Mode

In Developer Features, turn on “Developer mode”. You will need to reboot after this step.

2. Turn on WSL

In Windows Features, turn on “Windows Subsystem for Linux (beta)”. You will need to reboot after this step.

3. Run BASH as administrator.

(Window key, “Bash”, right click, “run as administrator.”)

4. Install Ubuntu

(Ubuntu is the only *nix image available for WSL at the moment, following a partnership between Microsoft and Canonical.)

When BASH starts, it will prompt you to install Ubuntu on Windows. Hit “y” to do so.

5. Install Ruby prerequisites

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

6. Install rbenv

rbenv will let you use different versions of both Ruby and Rails as needed:

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

7. Install Ruby

Install the version or versions of Ruby you’ll need to use. For example, to install v2.4.0:

rbenv install 2.4.0

(Optional) Set a global, default version of Ruby. For example, to make v.2.4.0 the default on your system:

rbenv global 2.4.0

(Optional) Check what version of Ruby you’re using as a default:

ruby -v
rbenv -versions

8. Install Bundler:

gem install bundler

Then Rehash rbenv:

rbenv rehash

9. Install Rails

5.0.1 shown; you can install multiple versions

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

gem install rails -v 5.0.1

10. (Optional) Install MySQL

MySQL

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

Test your installation

Now you’re ready to create a sample application and see if it works.

1. Create an empty Rails application with a database.

a. SQLite

Create an empty application using the Rails default SQLite database:

rails new deleteme

b. MySQL

Create an empty application using MySQL and then start the MySQL server:

rails new deleteme -d mysql
sudo /etc/init.d/mysql start

2. Initialize your application.

This is the happy path:

cd deleteme
rake db:create
rails server

3. View your application

Open a browser and navigate to http://localhost:3000.

Congratulations!

You should now be looking at a Ruby on Rails welcome screen. Happy BASHing.

In a subsequent post, I’ll look at installing Postgres on Bash/WSL, and all the things I had to fix while doing it.

DevMynd – custom software development services with practice areas in digital strategy, human-centered design, UI/UX, and mobile and web application development.

David has programmed computers since the Carter administration and left law school to do it professionally in the mid-’90s. He’s been with DevMynd since 2017.