Skip to content
<k/>
0%
Loading assets · 0s
<k/>
Loading...

Technical Journal

How I Set Up Ruby on Rails on Windows via WSL

3 min readruby-on-railswslwindowsmysqljournal

I've been using Ruby on Rails on and off for a while. The convention-over-configuration approach keeps boilerplate to a minimum. When I first set it up on Windows, though, I struggled to find good guides, especially in Indonesian. Getting a stable environment took longer than it should have.

To make sure I don't have to go through this again, I wrote it down as a personal journal. This is the exact setup I use for Ruby on Rails on Windows via WSL.

1. Enabling WSL (Windows Subsystem for Linux)

I've found that running Rails on Windows via WSL is a lot more stable than trying to use RubyInstaller directly.

  1. I open PowerShell as an Administrator.

  2. I run the following command to install WSL and the default Ubuntu distribution:

    wsl --install
  3. I restart my computer. Once restarted, a terminal window appears asking me to set up my UNIX username and password. I make sure to remember these since I need them for sudo commands.

2. Installing Dependencies and rbenv

Before installing Ruby, I need to make sure my Ubuntu environment has the necessary compilers and libraries. I open my Ubuntu / WSL terminal and run:

sudo apt update && sudo apt upgrade -y
sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libncurses5-dev libffi-dev libgdbm-dev -y

Instead of using a system-wide package manager for Ruby, I prefer to use rbenv. It allows me to easily switch between multiple Ruby versions.

I clone the rbenv repository into my home directory:

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

Next, I install ruby-build as an rbenv plugin. This provides the rbenv install command:

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

3. Installing Ruby

With rbenv ready, installing Ruby is straightforward. I usually install a recent stable version (for example, 3.3.0):

rbenv install 3.3.0
rbenv global 3.3.0

I verify my installation by checking the Ruby version:

ruby -v

Finally, I install bundler, which manages gem dependencies for my Ruby projects:

gem install bundler

4. Configuring MySQL for Rails (The Tricky Part)

I got stuck for an embarrassingly long time when trying to use MySQL with Rails on Windows/WSL. When I ran bundle install, the mysql2 gem kept failing to build because it couldn't find the native C headers.

To fix this, I had to install the MySQL server and the client development libraries inside my WSL environment:

sudo apt install mysql-server libmysqlclient-dev -y

Once installed, I start the MySQL service:

sudo service mysql start

Note: I sometimes need to set up a root password or configure a dedicated MySQL user for my Rails application depending on my MySQL version's default authentication plugin.

5. Creating My Ruby on Rails Project

Rails is installed. I can generate my first application.

I install Rails:

gem install rails

Now, I create a new Rails application configured to use MySQL as its primary database:

rails new my_awesome_app -d mysql

I navigate into my new project directory:

cd my_awesome_app

Before starting the server, I need to create the database:

rails db:create

If this step fails, I always double-check that my MySQL service is running and that my config/database.yml file contains the correct database credentials.

Finally, I boot up the Rails server:

rails server

I open my browser and navigate to http://localhost:3000. The Rails welcome page should show up.

Conclusion

WSL makes Rails on Windows manageable. Using rbenv instead of a system-wide Ruby, and making sure the native C dependencies are installed before running bundle install, is the part that tripped me up the most. Once it's done once, it's easy to replicate.

Comments