Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write (from the Ruby web site). On Christmas Day 2020, the core team released version 3.0 with major improvements and new features. However, Ruby 3.0 was a major release with lots of changes and some people are still more familiar with Ruby 2.x first. Last week, it was announced that Ruby on Rails 7.0 would require Ruby 2.7 as the minimum supported version. So, I’m updating my system from Ruby 2.6.8 to Ruby 2.7.4 as my main working version. If you’re new to Ruby 2.7 on Windows, this post will help you get started with Ruby 3 on Windows, and covers the basics of installation. This post is along the same lines as the JRuby Day 0 post from earlier and also the Ruby 3.0 Day 0 post
Note: This post was written for Ruby 2.7.4 but should work for other versions also.
There are a few ways to run Ruby on Windows (RubyInstaller, Windows Subsystem for Linux, Docker, etc.) but we will follow the simplest and most native way – to use the RubyInstaller.
Ok, so we are getting started – so, we set up Ruby on Windows and run a simple script. We will do the following:
- Download and install Ruby 2.7 on Windows
- Run a Ruby script
- Install a gem
- Set up pik for multiple Ruby versions
Before we get started
A full Ruby installation, as we intend to do, will take upwards of approximately 900MB of disk space. I recommend that you set the folder to be compressed automatically by Windows so that it saves you space. This becomes important especially on SSDs where the space might be limited to start with anyway.
You can do this step after you finish the installation but it takes much longer than doing it now when the folder is empty so that Windows will automatically compress the files on the fly as they get written to the directory as part of the installation.
So, this is what you need to do:
- In C Drive, create a folder called
Ruby27-x64– this is where we will install Ruby 2.7
- In Explorer then, right click on the new folder and click Properties which brings up the menu as below. (Note that this screenshot is from my Ruby 3.0 installation, so the path name is wrong because I forgot to screenshot this for Ruby 2.7).
- Click on Advanced… which brings up the ‘Advanced Attributes’ menu.
- Check the setting for ‘Compress contents to save disk space’ – this will ensure that the file in this folder are compressed on the disk and will save you a lot of space on the installation. At the end of the installation, we will see how much!
Download and install Ruby on Windows
Head on over to the RubyInstaller Downloads site and grab the package for
Ruby+Devkit 2.7.4-1 (x64) (it’s approximately 140MB). In general, you would want to get the 64-bit package (x64) but in case you want the 32-bit, you should get the x86 package listed below it. For widest compatibility with all types of gems including C native extensions, you should get the ‘Ruby+Devkit’ package though if you only want the Ruby runtime, the package without Devkit (listed further down) would also work. I would strongly recommend getting the package with Devkit. The screenshot below shows the different items listed on the page.
Run the dowloaded file. Read and accept the license and click Next.
This takes us to the next page and asks us for the path, whether it should add Ruby to our system path and if it should associate Ruby files with this installation. I personally prefer to uncheck the second option so that I have more choice in which Ruby version runs the script. Leave the option for UTF-8 as default external encoding checked. Ensure that the path shown matches the folder that we had set to be compressed. Click on Install to move on.
Since we created the folder before the installation (to set the attribute for compressing it), the Ruby installer will warn you that the folder already exists and will ask to confirm that it is OK to proceed. Our folder is empty and it is OK to go ahead! Click Yes.
In general, it is a good idea to install all these components since it makes the documentation available to you offline, and also prepares the MSYS2 development toolchain which is needed for compiling native gems. Select everything and click Next.
You will next see a lot of files being installed – just let that go ahead. Depending on your computer, this will take up to a few minutes.
This finally comes to the screen that tells you that the main installation is done and the next step is to move on to the MSYS2 Setup. Check the option to ‘Run ridk install’ so that it can proceed and click ‘Finish’. Note also the important links that are shown here.
This then brings us the Command Line based installation for the MSYS2 toolchain. I would recommend that you either choose 1,2,3 or just use the default 1,3 at this stage. (To use the default, you can also just press ENTER).
Eventually, it comes to the prompt below and just press ENTER at this stage again.
The installer will exit now and we are good to get started.
Is it all there?
Since the installation is all done, you should be able to open a new Command Line Prompt and type
ruby -v to see it run.
So, yes, we do seem to have Ruby 2.7.4 on our computer now. We also installed everything to
C:\Ruby27-x64 and can go and take a look at the files and folders there. Talking about that folder, you can now right click on that folder and check properties again. The exact numbers might be different, but you will likely see something like this. On my computer, the actual file sizes are approximately 980MB but on disk, it’s only taking 590MB – so, I saved almost 400MB by setting the folder to be compressed. Also, since this is applied to all the files and folders, as you install more gems, those will also be automatically compressed and will continue to take up lesser disk space.
Let’s write the simplest Hello World program and save it to a file called
1 puts 'Hello, world'
Then, you can run this by doing:
$ ruby hello.rb
Using Interactive Ruby
Next, you can start up Interactive Ruby and try something in it to see it work.
$ irb irb(main):001:0> puts RUBY_VERSION 2.7.4 => nil irb(main):002:0> puts 'Hello, World!' Hello, World! => nil
Working with gems
All Ruby programmers will install and use gems. In Ruby, we use
gem install [gem_name] which is shortcut for running the system command
gem (which could be done as
ruby -S gem install [gem_name]).
You can check the version of
gem as below.
$ gem -v 3.1.6
Gems already installed
A lot of gems are installed as part of the Ruby installation. Do the following to see the list. It should show you something like the list below.
$ gem list *** LOCAL GEMS *** benchmark (default: 0.1.0) bigdecimal (default: 2.0.0) bundler (default: 2.1.4) cgi (default: 0.1.0) csv (default: 3.1.2) date (default: 3.0.0) dbm (default: 1.1.0) delegate (default: 0.1.0) did_you_mean (default: 1.4.0) etc (default: 1.1.0) fcntl (default: 1.0.0) fiddle (default: 1.0.0) fileutils (default: 1.4.1) forwardable (default: 1.3.1) gdbm (default: 2.1.0) getoptlong (default: 0.1.0) io-console (default: 0.5.6) ipaddr (default: 1.2.2) irb (default: 1.2.6) json (default: 2.3.0) logger (default: 1.4.2) matrix (default: 0.2.0) minitest (5.13.0) mutex_m (default: 0.1.0) net-pop (default: 0.1.0) net-smtp (default: 0.1.0) net-telnet (0.2.0) observer (default: 0.1.0) open3 (default: 0.1.0) openssl (default: 2.1.2) ostruct (default: 0.2.0) power_assert (1.1.7) prime (default: 0.1.1) pstore (default: 0.1.0) psych (default: 3.1.0) racc (default: 1.4.16) rake (13.0.1) rdoc (default: 220.127.116.11) readline (default: 0.0.2) reline (default: 0.1.5) rexml (default: 18.104.22.168) rss (default: 0.2.8) sdbm (default: 1.0.0) singleton (default: 0.1.0) stringio (default: 0.1.0) strscan (default: 1.0.3) test-unit (3.3.4) timeout (default: 0.1.0) tracer (default: 0.1.0) uri (default: 0.10.0) webrick (default: 1.6.1) xmlrpc (0.3.0) yaml (default: 0.1.0) zlib (default: 1.1.0)
You will note that importantly bundler is installed and you will often use that when you start working seriously with Ruby.
Installing a gem
Let’s install a gem into our new Ruby 2.7 install. You can skip this is you are not keen. For simplicity, let’s install
yard which helps with documenting your code.
$ gem install yard Successfully installed yard-0.9.26 Parsing documentation for yard-0.9.26 Done installing documentation for yard after 2 seconds 1 gem installed
So, we know how to install gems now. Let’s install a gem that is very helpful on Windows.
Installing pik when using multiple Ruby Installations
pik is a gem that allows you to switch between different Ruby installations on a Windows computer. Although it is no longer maintained according to the github page, I found that it still works fine for me. It also works with JRuby (not just Ruby) by updating the PATH and other variables.
Open a command prompt that has Ruby 2.7 on the path and do
gem install pik
$ gem install pik Fetching pik-0.2.8.gem ---------------------------------------------------------------------------- * If you're upgrading from a version <= 0.1.1, you'll want to delete the pik.bat file from all of your ruby versions. Gem uninstall should do the trick. * Install pik to a location that's in your path, but someplace other than your ruby\bin dir If you're upgrading from a more recent version, pik_install will overwrite the older files as needed. >path PATH=C:\tools\;C:\ruby\186-p368-mingw32\bin;C:\WINDOWS\system32;C:\WINDOWS >pik_install C:\tools * If this is a first-time install, add all the versions of ruby that you want to use with pik >pik add Adding: 186: ruby 1.8.6 (2009-03-31 patchlevel 368) [i386-mingw32] Located at: c:/ruby/186-p368-mingw32/bin >pik add C:\ruby\IronRuby-091\bin Adding: 091: IronRuby 0.9.1.0 on .NET 22.214.171.124 Located at: C:/ruby/IronRuby-091/bin >pik add C:\ruby\jruby-1.4.0RC1\bin Adding: 140: jruby 1.4.0RC1 (ruby 1.8.7 patchlevel 174) (2009-09-30 80c263b) (Java HotSpot(TM) Client VM 1.6.0_14) [x86-java] Located at: C:/ruby/jruby-1.4.0RC1/bin ---------------------------------------------------------------------------- Successfully installed pik-0.2.8 Parsing documentation for pik-0.2.8 Installing ri documentation for pik-0.2.8 Done installing documentation for pik after 1 seconds 1 gem installed
The first time you ever use pik on a computer, you will need to install it to a folder that is on your PATH other than the bin folder of any Ruby installation. Two common options are to install it to
c:\windows\System32 (you will need to use an adminstrator command prompt for that) or to create a folder such as
c:\tools for dropping executables of this kind and adding that folder to your system path. If you need help with any of these steps, just ask below and I will add the information here. Whichever folder you choose, do as shown below (but replace ‘c:\tools’ with the actual directory)
Next, you add the current Ruby that is on the path by doing
pik add as below.
$ pik add ** Adding: 274: ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x64-mingw32] Located at: C:\Ruby27-x64\bin
Alternatively, you can add by using the full path to the Ruby installation bin folder as shown below.
$ pik add C:\Ruby30-x64\bin
Finally, to see all the Ruby installations that you have, just do
pik list and you should get a list of Ruby versions that you have added. My list is shown below.
$ pik list 187: ruby 1.8.7 (2013-06-27 patchlevel 374) [i386-mingw32] 193: ruby 1.9.3p551 (2014-11-13) [i386-mingw32] 224: ruby 2.2.4p230 (2015-12-16 revision 53155) [x64-mingw32] 266: ruby 2.6.6p146 (2020-03-31 revision 67876) [x64-mingw32] * 274: ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x64-mingw32] 300: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x64-mingw32] 921: jruby 126.96.36.199 (2.5.7) 2020-08-03 9a89c94bcc OpenJDK 64-Bit Server VM 25.265-b01 on 1.8.0_265-b01 +jit [mswin32-x86_64] 928: jruby 188.8.131.52 (2.5.8) 2021-06-08 d67cb7d6e0 OpenJDK 64-Bit Server VM 25.265-b01 on 1.8.0_265-b01 +jit [mswin32-x86_64]
You can switch between versions by doing something like
pik 274 or
pik 300 and so on.
You’re ready to use Ruby 2.7 on Windows – enjoy!
Links and References
Here are the main links:
- Ruby website: https://www.ruby-lang.org/
- RubyInstaller Website: https://rubyinstaller.org/
- Download RubyInstaller for Windows: https://rubyinstaller.org/downloads/
- RubyInstaller Google Group: https://groups.google.com/g/rubyinstaller?pli=1
- Wiki by Lars Kanis: https://github.com/oneclick/rubyinstaller2/wiki
- Using pik to switch between Ruby versions
OK, so we have done enough! We know how to install Ruby, run a script, install gems directly and switch between different Ruby installations. We are ready for more! If you have any comments, please leave them below so that we can improve this page.