💊 How To Install #RubyonRails 💊 on Windows 7/8/10/11 → Tutorial 2022

Step-By-Step Tutorial on How To Install Ruby, Rails, RubyGems, Git, RMagick, MYSQL2 & SQLite3 on Windows…

--

💎 Ruby 3.0+ Running On 💻 Windows 11 (08/01/2022)

Article revised on 📆 Jan 2022 📆 to reflect the new update
of RubyInstaller2 (Devkit no longer required), Rails 6.0.3 & SQLite3 1.4.0.

Overview

To develop Rails on Windows, you require the following components:

  1. 💎Ruby
  2. 🔨 Build Tools (used for gem compilation)
  3. 📋 RubyGems
  4. 🚀 Rails
  5. 🚦 GIT
  6. 💻 IDE (Code Editor)

These are all freely available, and are required for any OS Ruby install ↴

📝 Note from the author (Jan 2022)

As far as I am aware, this is the ONLY Ruby/Rails on Windows tutorial which doesn’t require the installation of Bash. The bash dependency is around 215mb+ of unnecessary data, not worth it.

This tutorial explains the most space-efficient way of installing a pre-compiled version of Ruby, which only requires around 5mb of data + your bundles.

If you have any questions, please either leave a response here, or contact me directly at rpeck@frontlineutilities.co.uk.

💎Ruby💎

On Windows, you need a pre-compiled version of Ruby.

On Linux, you can compile from source. Not Windows.

Well, actually you can. But it requires MASSIVE amounts of patience in order to get the correct dependencies / libraries installed.

The best thing to do is download a pre-built version of Ruby.

This can be done with two packages → RailsInstaller / RubyInstaller:

Don’t use RailsInstaller…

You should **NOT** use RailsInstaller ONLY RubyInstaller.

⚠️ RailsInstaller does *NOT* keep libraries up to date ... ⚠️

RailsInstaller relies on outdated versions of Ruby. Not worth it…

💾 Download RubyInstaller (IMPORTANT)

The best thing to do is to download the ZIP version of RubyInstaller. Whilst there is an installer, it’s better to just download the binaries in the ZIP.

If you do want to do this, there are several options:

  • Ruby 2.3.x (not worth bothering with)
  • Ruby 2.4.x
  • Ruby 2.5.x
  • Ruby 2.6.x

Due to different build requirements, Ruby 2.4.x started using RubyInstaller2
(which uses MSYS2 over DevKit):

RubyInstaller 2.0 uses MSYS2- which we recommend over DevKit…

I would personally recommend downloading Ruby 2.5.x (the latest), but if you have an application designed for Ruby 2.4.x you’re also able to use that:

1️⃣ Identify Windows Architecture

The first step is to know your Windows version (x64 or x86):

  • Press Windows+ R keys on the keyboard
  • Type dxdiag and press OK
  • Identify the System Type from the list:

2️⃣ Download ZIP From RubyInstaller (may need to install 7Zip):

3️⃣ Save The Zip To Your Hard Drive

Save The ZIP File To A Folder On Your Hard Drive

4️⃣ Unzip To PERMANENT Folder:

Unzip To A Folder Without Any Spaces (This Is Where Your Ruby Install Will Reside)

5️⃣ Add to PATH:

The Windows PATH allows your system to load applications be referencing their name. To call ruby from CMD, you’ll need to add it to the PATH var:

→ Windows 7

  • Click on Start
  • Right-click on Computer → select Properties
  • From the System dialogue, select Advanced System Settings:
  • Select “Advanced” tab — “Environment Variables”:
  • From here, select the PATH variable from the SYSTEM Variables panel, and select Edit:

When the “path” variable setting loads, paste the path to the /bin directory of Ruby into it (keeping the other paths intact). After this, click “OK” to exit.

→ Windows 10

  • Press Windows button (keyboard)
  • Type ENV + press Enter
  • Select Environment Variables
  • The following should appear:
  • Next, click New to add the bin directory for the new ruby directory you created (also add the devkit bin directory if you’ve installed that):
This is the Windows 10 Version Of The PATH Variable Editor
  • Click OK and exit any CMD instances you have open.

🔨 Build Tools 🔨

Next, you need to install “build tools”.

These are used to compile gems that rely on external libraries
(nokogiri / rmagick / mysql2):

This error means you don’t have the required library installed for the gem


*ALL* operating systems require build tools as extra downloads. They are applications, files and libraries which help your system compile gems.

In Ubuntu, you’d install the build-essential package:

In Windows, you need to download the tools separately.

How you do this is determined by which version of RubyInstaller you choose. If you used 2.3.x, you use Devkit; 2.4.x + 2.5.x use MSYS2:

Devkit (≤ 2.3.x) (Good tutorial)

For older versions (up to 2.3.x) of RubyInstaller, you need “DevKit” ↴

Ruby 2.4.x did away with this, so if you’re planning on using Ruby 2.4.x or Ruby 2.5.x , you’ll be better using MSYS2 directly from their site.

ZIP (≤ 2.3.x ONLY → 2.4.x/2.5.x NEEDS to use MSYS2)…

After downloading ONE of the above files, load the self extracting archive and “unzip” to a temporary directory:

Extract The Files To The Folder Where They Will Permanently Stay

Once complete, you need to initialize DevKit and bind it to your Ruby installation:

  1. Load up cmd and cd into the RubyGems directory
  2. Type the following: ruby init.rb & press Enter
  3. Next, load up the config.yml
  4. Add the directory for your Ruby installation:
  • Next, in cmd you should type ruby setup.rb & press Enter
  • This should bind DevKit to your Ruby installation, allowing it to call all the libraries.

MSYS2 (STRONGLY Recommended → essential for ≥ 2.4x / 2.5.x)

Ruby 2.4.x / 2.5.x (RubyInstaller2) uses MSYS2

MSYS2 Is A CLI Toolchain To Download & Install Dependencies (VERY Similar To Apt)
RubyInstaller2 Requires MSYS2 — Which has to be installed independently.

Setting up MSYS2 is slightly more involved than DevKit, but more effective.

The point is that both install a series of files or applications which allow
you to perform build / compilation operations:

  • Save the installer to your hard drive
  • Double-click the installer to get it to load:
  • After the installer completes, you need to set up pacman
  • Load a new MSYS2 console and type the following:

You should see something similar to the following:

Using the above commands, pacman will be installed and ready to use.

Now, you need to install the various build dependencies. This used to be
done from within MSYS2 itself, but now it’s part of the RubyInstaller2 installation process:

  • Open up CMD
  • Type ridk install
  • The following screen should appear:

Press 3 and Enter.

From here, it should work to install the various components of the MSYS2 development toolchain. If this process fails (we had a Windows 8 user where the process hung randomly), you’ll want to switch back to MSYS2 and run one of the following commands (installs the build tools manually):

If this does not work (in Windows 8), you should continually resize the MSYS2 window. This is what it took to get it working on a reader’s system.

Installing C-dependent Gems (Nokogiri / SQLite3 etc)

The RubyInstaller2 repo has a good tutorial on how to do this:

⚠️🚨 NOTE ABOUT SQLITE3 🚨⚠️

SQLite3 1.4.0 is currently incompatible with Windows…

If you use SQLite3 (as we do in development), you’ll find the latest version of the gem to be incompatible with Windows.

The answer to this is to install the “mingw-w64-x86_64-dlfcn“ package through MSYS2:

This installs the correct package, allowing you to run the SQLite3 1.4.0 gem inside the Windows environment.

🧶 Yarn/WebPacker

If you want to install Webpacker (which requires yarn), you will need to install the packages which support it. Whilst MSYS2 supports both yarn & nodejs, we’re yet to find a way to make them work on a base/system level (they install, but are not accessible by Rails)…

As mentioned, whilst these install, they don’t work with Rails/Webpacker yet.

Instead, you will need to download and install the native packages from the Yarn & NodeJS websites:

Both of these packages can be installed anywhere on your system in order to get Yarn/Webpacker working…

📋 RubyGems 📋

Next, you need to install RubyGems.

RubyGems is the repository which stores all the “gems” you can install on Ruby. These gems are the “plugins” for the language, providing functionality.

As this is a separate package to Ruby, it has to be installed manually:

To install RubyGems, you need to download the ZIP and
unzip to a temporary folder:

Save the ZIP to your System

From there, you will need to open cmd.exe and cd into the TEMPORARY folder. You should then type ruby setup.rb and RubyGems willinstall:

If successful, you’ll be able to type gem -v in a new instance of cmd:

🚀 Rails 🚀

With RubyGems installed, you need to install the rails gem:

⚠️🚨 NOTE ABOUT Ruby 2.7.0+ 🚨⚠️

The recent updates in Ruby 2.7.0 have caused several issues for Rails.

Specifically, the current “nokogiri” gem is not compatible with > Ruby 2.7.0, so installing a “naked” version of Rails kicks up an error:

Whilst Nokogiri have released a “pre” version of their gem (which addresses this problem), it doesn’t install and Rails is still unable to install at the “system” level.

To get around this, you need to install Rails with the help of the bundler.

The best way I have found (to fix it) is to make sure that Bundler is using the “global” gem repository, and then add the following to your Gemfile:

gem ‘nokogiri’, ‘~> 1.11.0.rc2’

Doing this with Rails in the same Gemfile gets it installed (with the current restrictions). No other way, apart from that, presently.

If you’re using a Ruby version pre 2.7.0, you should open cmd and type:
gem install rails --no-document

  • You also need bundler because without it, you cannot use a Gemfile
  • You need rails because that’s the framework you’re using.

Installing Rails as a system-wide gem will give you the ability to call it whenever needed, not just as part of a Rails application:

🚦 GIT 🚦

You then need to install GIT.

Git is a source code management system (SCM), basically allowing you to sync your development source with production and staging environments.

It has taken over the role of FTP, allowing you to “push” code to “repositories”. If this sounds alien, you need to read up on it. It’s an essential part of modern web development — especially Rails. Github is a very good place to start:

The installation process should be performed as follows:

Deselect any OS integration — you don’t need “GIT” commands popping up in Windows Explorer
Make sure you use GIT from CMD
This ensures line endings are compatible with Windows and Linux etc
Make sure you’re using Windows’ own CMD window
Ensure you have all of the above checked

Once installed, you should load a new instance of cmd.exe and type the command git — version. If it returns a value, the installation was a success:

💻 IDE (Code Editor) 💻

Finally, you need an IDE (Integrated Development Environment).

Whist you’re free to use Notepad to edit code, the best system is to use a dedicated code editor. The old favorite was Sublime Text, however it was
not updated enough and cost.

We use Atom.io from GitHub (free & open source):

You can download here (free).

🎉 After this, you’re able to develop Ruby on Rails on Windows! 🎉

📥 Thanks For Reading! 📥

If you need further help, please feel free to ask below…

--

--