skip to main content

Setting up Rails 6 and PostgreSQL on Windows using WSL and Ubuntu

How to set up Rails 6 and PostgreSQL on Windows Using the Windows Subsystem for Linux and Ubuntu

Alex Morton

January 11, 2021

Photo by Tadas Sar on Unsplash

During my first week as a Junior Software Engineer with the Orbit team, I had many things to do to get into the overall swing of things, and one of the most important tasks was getting our Rails application set up on my local machine.

The only problem? I was stuck with my personal Windows computer while waiting for a company MacBook to be shipped to me.

Alas, my first challenge: Getting Rails 6 and PostgreSQL working on Windows. How hard could it be?

Well, it turned out to be quite a confusing process, but after much head-scratching and jumping through many Google search hoops, I’m happy to report that we succeeded in getting everything up and running.

So without further ado, here’s how we were able to get Rails successfully installed on Windows:

1. Install Ubuntu on the Windows Subsystem for Linux (WSL)

First things first, let’s define a few key things. The Windows Subsystem for Linux (or WSL) allows developers to run a Linux environment directly from Windows, while Ubuntu is an operating system that’s based on Linux.

The main benefit of using the WSL is that we can 1) use Linux command-line tools and applications and 2) avoid the potential headache of navigating through Windows Powershell.

Here's the link to the Microsoft documentation on how to install the Windows Subsystem for Linux and install the Ubuntu app which is used to access it. (Note: you’ll need to sign up for Windows Insider, but it’s free and took just a few minutes for me to do.)

2. Install PostgreSQL for Windows

I used this PostgreSQL for Windows Installer to install PostgreSQL onto my computer. Keep an eye out for the link to “Download the installer” at the top of the page.

I opted to install PostgreSQL this way instead of trying to install PostgreSQL in the Ubuntu subsystem using apt-get (a command-line tool for installing software packages). It looks possible to install PostgreSQL that way (see this post), but I ran into issues trying to connect Rails and PostgreSQL while using apt-get.

PostgreSQL for Windows takes some time to download, so a wait-time of a few minutes is normal. Be sure to store the username and password you enter somewhere secure so you remember it.

Once it’s installed, you’ll also have access to a tool called pgAdmin - a graphical user interface (GUI) for any databases you’ll create in Rails, so it’s very handy - especially if you don’t have much direct experience with databases just yet.

3. Install Ruby

Note: you’ll do this through an Ubuntu terminal, accessed by launching the Ubuntu app from your Windows start menu

If you don’t yet have Ruby installed on your machine, I recommend installing Ruby with rbenv (a Ruby version and installation manager) as explained in this guide.

Keep in mind: Pay attention to the Ruby version number you need if you’re going to be running an app on a particular version (you’ll likely find this in the application’s Gemfile and/or .ruby-version file).

I messed up here and installed a different Ruby version than the one I needed. It didn’t end up being too big of a problem, though, because you can switch Ruby versions quite easily with rbenv.

Note: to switch between Ruby versions in the current shell with rbenv (if you have more than one version of Ruby installed), you can do this:

$ ruby -v # checks which version of Ruby is running
=> 2.7.1
$ rbenv shell 2.7.2 # to switch to Ruby 2.7.2
$ ruby -v
=> 2.7.2

If your project has a .ruby-version file, rbenv will look for it when you change into your project’s directory and switch the shell to that ruby. If for whatever reason that doesn’t work, you can trigger the change yourself with the rbenv shell command.

4. Configure Git on Ubuntu

Next, you’ll need to configure Git on Ubuntu.

5. Install Rails

Finally, it’s time to get Rails installed! This guide walked me through installing Rails, and it also explains the necessary step of installing a JS runtime (but here’s an extra guide for further instruction).

6. Start PostgreSQL server

To start the PostgreSQL server, run this command: sudo service postgresql start

If you run into any authentication issues, run sudo -u postgres -i and that should get PostgreSQL up and running.

7. Create users & databases in PostgreSQL

As I was getting PostgreSQL set up for Rails databases, I was running into a lot of trouble around creating users and databases from the command line.

So, instead of using the command line, I simply opened the pgAdmin tool and then manually created the users and databases from there.

After creating the Rails development and test databases, we then granted our “orbit” user permissions on both databases. Here’s how that looks in practice:

  1. From pgAdmin, create the “orbit” user with the right password (in the Login / Group Roles item). The Password input is located in the Definition tab.
  2. Give the role all the privileges.
  3. Create two databases: orbit1_development and orbit1_test (Note: your database names may differ. In general, you’ll want one database for the development environment and one for the test environment.)
  4. For each database, right click on Properties, then choose the Privileges tab, and then add the orbit user with all Permissions.

This guide gives more information about the above process.

8. Prepare to connect Rails to PostgreSQL

We’ll need to install development headers on Ubuntu to eventually be able to install the pg gem and successfully complete the bundle install. To do this, run sudo apt install libpq-dev.

Next, we’ll navigate into our Rails app in the Ubuntu window and run gem install pg.

To connect Rails and PostgreSQL, you’ll need to make some changes in the config/database.yml file. Specifically, you will need to set the host to 127.0.0.1 and set a username and password.

At this time, I updated the config/database.yml file with the correct database name, username, and password from before from a gist containing database information.

development:
<<: *default
database: orbit1_development
host: 127.0.0.1
username: orbit
password: password

9. Load the Rails app database into PostgreSQL

Next, within our Ubuntu window, we can run the command bundle exec rake db:schema:load which will directly load the contents of the db/schema.rb file into the database, followed by bundle exec rake db:seed if your app contains seed data.

After that, we can start the Rails server:

$ bundle exec rake db:schema:load
$ bundle exec rake db:seed # if your app has seed data
$ spring rails server

________

Wow! If you’re still here, I’m hoping you made it through to the other side, and that Rails 6 and PostgreSQL are successfully set up for you through the Windows Subsystem for Linux and Ubuntu.

Personally, after all of that work setting everything up on Windows, I must say it feels bittersweet to have to say goodbye to Ubuntu and the WSL once the company MacBook arrives in the coming days.

Regardless, if you’re ever in my position and you find yourself counting down the days until your MacBook arrives (or if you just enjoy using and developing on Windows!), then I can assure you that Windows for Rails isn’t as much of a headache as it might initially seem!


You might also like:

Why Orbit is Better Than Funnel for Developer Relations
DevRel teams need tools and models created specifically for our discipline, and not just those adopted from other fields.
Slack vs Discord vs Discourse: The best tool for your community
An in-depth comparison of 3 top community platforms across dozens of factors.
How we use Orbit to build Orbit
A guide to how we use our product to build our community.

Join our mailing list  📩

No spam, just insights, product and content updates, event invites, and the occasional space pun.

Orbit is a registered trademarks of Orbit Labs, Inc.

Join us in Orbit.


In early access, we’re working closely with our early users to setup their implementation and get the most out of Orbit. Complete this form and we’ll be in touch soon.

nevermind