Ruby Programming on Windows: A Step-by-Step Guide
Ruby is a dynamic programming language you can use to write anything from simple scripts to games and web applications. It was first released in Japan in 1993, but gained popularity in 2005 as a language for server-side web development. Ruby is designed to be easy to use and fun for beginners, but powerful enough to create complex systems. It’s a great choice for beginners and experienced developers alike.
While there are many ways to set up Ruby on Windows, Microsoft recommends that you use the Windows Subsystem for Linux (WSL) and Bash to do your Ruby development. WSL is a Windows 10 feature that lets you run native Linux command line tools on Windows. Many Ruby libraries are designed to run on Linux, and can exhibit problems when run on Windows.
Microsoft partnered with Canonical and other Linux distributions to enable native support for the Bash shell and Linux command line tools to solve this issue. With Bash and WSL installed, you’ll edit your files with your favorite Windows tools, but use Bash and command line tools to execute Ruby and its related tools.
Prerequisites
You will need a computer running Windows 10 with the Creators Update, and access to install software with administrative privileges.
Step 1 — Installing Bash on Windows
You’ll use the command line to install and work with Ruby. The command line is a non-graphical way to interact with your computer. Instead of clicking buttons with your mouse, you’ll type commands as text and receive text-based feedback. The command line, also known as a shell, lets you automate many tasks you do on your computer daily, and is an essential tool for software developers.
Windows offers two command line interfaces out of the box: the classic Command Prompt, and PowerShell. We’re going to install Bash, a popular shell and command language that you’d find on Linux and macOS.
First, enable Developer mode on your machine. To do this, open the Settings app, select Update & Security, and then choose the For developers entry in the sidebar. Then check the Developer mode option and accept the prompt asking you to verify this change.
Next, open the Control Panel and select Programs. Then select Turn Windows features on or off. In the list of components that appears, check the option for Windows Subsystem For Linux (Beta). Then click OK and wait while Windows installs the additional components, which may take a few minutes.
When the computer reboots, open the Command Prompt and type:
bash
Step 2 — Installing RVM and Ruby
RVM automates the process of setting up a Ruby environment on an Ubuntu or macOS system, and since the Bash setup you’re running is based on Ubuntu, this is the quickest way to set things up on Windows as well. Let’s get it installed so we can use it to install Ruby.
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
Install the gnupg2 package:
sudo apt-get install gnupg2
Download RVM installation script:
\curl -sSL https://get.rvm.io -o rvm.sh
Step 3 — Creating a Simple Program
Let’s create a small “Hello, World” program to verify the setup.
puts "Hello, World!"
Run the program:
ruby hello.rb
Conclusion
With your local machine ready for software development, you can continue to learn more about coding in Ruby.