Ruby 3.0 on Windows: Day 0 - install and Hello World

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. Now that RubyInstaller 3.0.0-1 has been released it’s a good time to set it up on Windows and try the features of Ruby 3.0. 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.

Note: This post was written for Ruby 3.0.0 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 it’s Day 0 – the day we set up Ruby on Windows and run a simple script. We will do the following:

  • Download and install Ruby 3 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 Ruby30-x64 – this is where we will install Ruby 3.0
  • In Explorer then, right click on the new folder and click Properties which brings up a menu like the one you see below.
  • 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 3.0.0-1 (x64) (it’s approximately 135MB). 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 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. Ensure that the path shown matches the folder that we had set to be compressed.

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.

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 3.0.0 on our computer now. We also installed everything to C:\Ruby30-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 905MB but on disk, it’s only taking 559MB – so, I saved almost 350MB 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.

Hello, World!

Let’s write the simplest Hello World program and save it to a file called hello.rb.

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
3.0.0
=> nil
irb(main):002:0> 3.times {puts 'welcome '}
welcome
welcome
welcome
=> 3
irb(main):003:0" puts "Rocking it on #{RUBY_VERSION}"
Rocking it on 3.0.0
=> nil
irb(main):004:0> exit

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.2.3

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 ***

abbrev (default: 0.1.0)
base64 (default: 0.1.0)
benchmark (default: 0.1.1)
bigdecimal (default: 3.0.0)
bundler (default: 2.2.3)
cgi (default: 0.2.0)
csv (default: 3.1.9)
date (default: 3.1.0)
dbm (default: 1.1.0)
debug (default: 0.1.0)
delegate (default: 0.2.0)
did_you_mean (default: 1.5.0)
digest (default: 3.0.0)
drb (default: 2.0.4)
english (default: 0.7.1)
erb (default: 2.2.0)
etc (default: 1.2.0)
fcntl (default: 1.0.0)
fiddle (default: 1.0.6)
fileutils (default: 1.5.0)
find (default: 0.1.0)
forwardable (default: 1.3.2)
gdbm (default: 2.1.0)
getoptlong (default: 0.1.1)
io-console (default: 0.5.6)
io-nonblock (default: 0.1.0)
io-wait (default: 0.1.0)
ipaddr (default: 1.2.2)
irb (default: 1.3.0)
json (default: 2.5.1)
logger (default: 1.4.3)
matrix (default: 0.3.1)
minitest (5.14.2)
mutex_m (default: 0.1.1)
net-ftp (default: 0.1.1)
net-http (default: 0.1.1)
net-imap (default: 0.1.1)
net-pop (default: 0.1.1)
net-protocol (default: 0.1.0)
net-smtp (default: 0.2.1)
nkf (default: 0.1.0)
observer (default: 0.1.1)
open-uri (default: 0.1.0)
open3 (default: 0.1.1)
openssl (default: 2.2.0)
optparse (default: 0.1.0)
ostruct (default: 0.3.1)
pathname (default: 0.1.0)
power_assert (1.2.0)
pp (default: 0.1.0)
prettyprint (default: 0.1.0)
prime (default: 0.1.2)
pstore (default: 0.1.1)
psych (default: 3.3.0)
racc (default: 1.5.1)
rake (13.0.3)
rbs (1.0.0)
rdoc (default: 6.3.0)
readline (default: 0.0.2)
reline (default: 0.2.0)
resolv (default: 0.2.0)
resolv-replace (default: 0.1.0)
rexml (3.2.4)
rinda (default: 0.1.0)
rss (0.2.9)
securerandom (default: 0.1.0)
set (default: 1.0.1)
shellwords (default: 0.1.0)
singleton (default: 0.1.1)
stringio (default: 3.0.0)
strscan (default: 3.0.0)
tempfile (default: 0.1.1)
test-unit (3.3.7)
time (default: 0.1.0)
timeout (default: 0.1.1)
tmpdir (default: 0.1.1)
tracer (default: 0.1.1)
tsort (default: 0.1.0)
typeprof (0.11.0)
un (default: 0.1.0)
uri (default: 0.10.1)
weakref (default: 0.1.1)
win32ole (default: 1.8.8)
yaml (default: 0.1.1)
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 3.0 install. You can skip this is you are not keen. For simplicity, let’s install sinatra which is a web development framework.

>gem install sinatra
Fetching mustermann-1.1.1.gem
Fetching rack-protection-2.1.0.gem
Fetching tilt-2.0.10.gem
Fetching ruby2_keywords-0.0.2.gem
Fetching rack-2.2.3.gem
Fetching sinatra-2.1.0.gem
Successfully installed tilt-2.0.10
Successfully installed rack-2.2.3
Successfully installed rack-protection-2.1.0
Successfully installed ruby2_keywords-0.0.2
Successfully installed mustermann-1.1.1
Successfully installed sinatra-2.1.0
Parsing documentation for tilt-2.0.10
Installing ri documentation for tilt-2.0.10
Parsing documentation for rack-2.2.3
Installing ri documentation for rack-2.2.3
Parsing documentation for rack-protection-2.1.0
Installing ri documentation for rack-protection-2.1.0
Parsing documentation for ruby2_keywords-0.0.2
Installing ri documentation for ruby2_keywords-0.0.2
Parsing documentation for mustermann-1.1.1
Installing ri documentation for mustermann-1.1.1
Parsing documentation for sinatra-2.1.0
Installing ri documentation for sinatra-2.1.0
Done installing documentation for tilt, rack, rack-protection, ruby2_keywords, mustermann, sinatra after 285 seconds
6 gems 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 3.0 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 2.0.0.0
     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 3 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)

>pik_install C:\tools

Next, you add the current Ruby that is on the path by doing pik add as below.

$ pik add
** Adding:  300: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x64-mingw32]
 Located at:  C:\Ruby30-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]
* 300: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x64-mingw32]
  921: jruby 9.2.13.0 (2.5.7) 2020-08-03 9a89c94bcc OpenJDK 64-Bit Server VM 25.265-b01 on 1.8.0_265-b01 +jit [mswi...

You can switch between versions by doing something like pik 266 or pik 300 and so on.

What’s next?

You’re ready to use Ruby 3.0 on Windows – enjoy!

Links and References

Here are the main links:

  • Ruby website: https://rubyinstaller.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://wiki.github.com/larskanis/rubyinstaller2

OK, so we have done enough for Day 0 – 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.

comments powered by Disqus