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 2022, the core team released version 3.2 with major improvements and new features. Now that RubyInstaller 3.2.0-1 has been released and is available for all to try, it’s a good time to set it up on Windows and try the features of Ruby 3.2. 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.2.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.2 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
Ruby32-x64
– this is where we will install Ruby 3.2 - 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 rubyinstaller-devkit-3.2.0-1-x64
(it’s approximately 125MB). 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. You may be presented with a screen asking if you intend to install this software.
Expand the details by clicking on [More Info] and you should see a screen like the one below. Click on [Run Anyway] to proceed to the installation.
The next step asks if you want to install the software for all users or only for the current user.
This is new in RubyInstaller for Ruby 3.2 and there is a note about this on the Release Page – for now, we will follow the recommended path to install for the current user but feel free to try it out for all users and if you find issues, feel free to report your observations and re-open this issue
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.2.0 on our computer now. We also installed everything to C:\Ruby32-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 895MB but on disk, it’s only taking 554MB – 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 from Ruby #{RUBY_VERSION}"
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.2.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.2.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.4.1
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.1)
base64 (default: 0.1.1)
benchmark (default: 0.2.1)
bigdecimal (default: 3.1.3)
bundler (default: 2.4.1)
cgi (default: 0.3.6)
csv (default: 3.2.6)
date (default: 3.3.3)
debug (1.7.1)
delegate (default: 0.3.0)
did_you_mean (default: 1.6.3)
digest (default: 3.1.1)
drb (default: 2.1.1)
english (default: 0.7.2)
erb (default: 4.0.2)
error_highlight (default: 0.5.1)
etc (default: 1.4.2)
fcntl (default: 1.0.2)
fiddle (default: 1.1.1)
fileutils (default: 1.7.0)
find (default: 0.1.1)
forwardable (default: 1.3.3)
getoptlong (default: 0.2.0)
io-console (default: 0.6.0)
io-nonblock (default: 0.2.0)
io-wait (default: 0.3.0)
ipaddr (default: 1.2.5)
irb (default: 1.6.2)
json (default: 2.6.3)
logger (default: 1.5.3)
matrix (0.4.2)
minitest (5.16.3)
mutex_m (default: 0.1.2)
net-ftp (0.2.0)
net-http (default: 0.3.2)
net-imap (0.3.4)
net-pop (0.1.2)
net-protocol (default: 0.2.1)
net-smtp (0.3.3)
nkf (default: 0.1.2)
observer (default: 0.1.1)
open-uri (default: 0.3.0)
open3 (default: 0.1.2)
openssl (default: 3.1.0)
optparse (default: 0.3.1)
ostruct (default: 0.5.5)
pathname (default: 0.2.1)
power_assert (2.0.3)
pp (default: 0.4.0)
prettyprint (default: 0.1.1)
prime (0.1.2)
pstore (default: 0.1.2)
psych (default: 5.0.1)
racc (default: 1.6.2)
rake (13.0.6)
rbs (2.8.2)
rdoc (default: 6.5.0)
readline (default: 0.0.3)
reline (default: 0.3.2)
resolv (default: 0.2.2)
resolv-replace (default: 0.1.1)
rexml (3.2.5)
rinda (default: 0.1.1)
rss (0.2.9)
ruby2_keywords (default: 0.0.5)
securerandom (default: 0.2.2)
set (default: 1.0.3)
shellwords (default: 0.1.0)
singleton (default: 0.1.1)
stringio (default: 3.0.4)
strscan (default: 3.0.5)
syntax_suggest (default: 1.0.2)
tempfile (default: 0.1.3)
test-unit (3.5.7)
time (default: 0.2.1)
timeout (default: 0.3.1)
tmpdir (default: 0.1.3)
tsort (default: 0.1.1)
typeprof (0.21.3)
un (default: 0.2.1)
uri (default: 0.12.0)
weakref (default: 0.1.2)
win32ole (default: 1.8.9)
yaml (default: 0.2.1)
zlib (default: 3.0.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.2 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 rack-2.2.5.gem
Fetching sinatra-3.0.5.gem
Fetching mustermann-3.0.0.gem
Fetching rack-protection-3.0.5.gem
Fetching tilt-2.0.11.gem
Successfully installed tilt-2.0.11
Successfully installed rack-2.2.5
Successfully installed rack-protection-3.0.5
Successfully installed mustermann-3.0.0
Successfully installed sinatra-3.0.5
Parsing documentation for tilt-2.0.11
Installing ri documentation for tilt-2.0.11
Parsing documentation for rack-2.2.5
Installing ri documentation for rack-2.2.5
Parsing documentation for rack-protection-3.0.5
Installing ri documentation for rack-protection-3.0.5
Parsing documentation for mustermann-3.0.0
Installing ri documentation for mustermann-3.0.0
Parsing documentation for sinatra-3.0.5
Installing ri documentation for sinatra-3.0.5
Done installing documentation for tilt, rack, rack-protection, mustermann, sinatra after 22 seconds
5 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.2 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 2 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: 320: ruby 3.2.0 (2022-12-25 revision a528908271) [x64-mingw-ucrt]
Located at: C:\Ruby32-x64\bin
Alternatively, you can add by using the full path to the Ruby installation bin folder as shown below.
$ pik add C:\Ruby32-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]
311: ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [x64-mingw-ucrt]
* 320: ruby 3.2.0 (2022-12-25 revision a528908271) [x64-mingw-ucrt]
921: jruby 9.2.13.0 (2.5.7) 2020-08-03 9a89c94bcc OpenJDK 64-Bit Server VM...
928: jruby 9.2.18.0 (2.5.8) 2021-06-08 d67cb7d6e0 OpenJDK 64-Bit Server VM...
932: jruby 9.3.2.0 (2.6.8) 2021-12-01 0b8223f905 OpenJDK 64-Bit Server VM ...
You can switch between versions by doing something like pik 266
or pik 300
and so on – basically use the 3 digit code that it shows on the line at the start.
What’s next?
You’re ready to use Ruby 3.2 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: https://github.com/oneclick/rubyinstaller2/wiki
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.