JRuby is a Java implementation of Ruby that runs atop the Java Virtual Machine. The JRuby wiki has this to say:
JRuby is a 100% Java implementation of the Ruby programming language. It is Ruby for the JVM.
JRuby provides a complete set of core “builtin” classes and syntax for the Ruby language, as well as most of the Ruby Standard Libraries. The standard libraries are mostly Ruby’s own complement of .rb files, but a few that depend on C language-based extensions have been reimplemented. Some are still missing, but we hope to implement as many as is feasible.
Since it runs on the JVM, it benefits from tooling and optimisations that are built into the Java Virtual Machine; this can lead to better performance. It also provides us a way for us to call Java classes and libraries from our Ruby programs.
Note: This post was written for JRuby 9.4.6.0. But the steps are the same for most versions.
Ok, so it’s Day 0 – the day we set up JRuby on Windows and run a simple script. We will do the following:
- Download and install JRuby on Windows
- Run a Ruby script
- Install a gem
- Set up pik for multiple JRubies
- Mapping Ruby commands to JRuby
This guide assumes that you already have Java on your PC. If you don’t, you need to set that up. This is one of the many guides on getting Java running on your PC.
Download and install JRuby on Windows
Getting JRuby running on Windows is really straighforward. Directly on the home page on the top right, you will see the links to get different packages. For Windows, you need to get one of the two exe versions:
- exe – this is the version for 32-bit Windows
- exe(x64) – this is the version for 64-bit Windows
Get the correct version for your computer and run it. Installing JRuby requires administrator permissions to install.
The default installation does two things:
- It installs JRuby into
c:\jruby-version
– as at the time of writing, it would create the directory intoc:\jruby-9.4.6.0
since I’m installing version 9.4.6.0. In the screenshots below, I changed the installation path to install tod:\
instead. - It adds this JRuby installation to the PATH on Windows. So, if you open a command prompt and run
jruby -v
you should see something like the screenshot below.
The downloads page for JRuby also provides other downloads and older versions, if you need something else.
I won’t walk through the individual steps, but you are likely to see similar screens as your installation proceeds.
You’ll see that it shows the installed version is:
- jruby 9.4.6.0
- It’s compatible with Ruby 3.1.4
- In this case, it’s running on: OpenJDK 64-Bit Server VM 25.265-b01 on 1.8.0_265-b01 (Java 8)
Note: if you are still using Java 8, you should consider updating. Newer versions of Java have better performance, and JRuby plans to drop support for old Java in the near future. This site has a guide to install and switch Java versions on Windows if you need it.
Good! With that done, we are ready to try running a simple script.
Create and run a simple script
Ok, hello world time. We all know that we just need this for the simplest Ruby script.
1
puts 'Hello, world'
Then, you can run this by doing:
$ jruby hello_world.rb
If you tried both ruby and JRuby exactly the same way, you will see the same output results.
$ ruby hello_world.rb
Hello, world
$ jruby hello_world.rb
Hello, world
You will notice one thing at this point – the JRuby version takes longer to run. This is something you will hear about in every JRuby talk – startup takes a bit longer and JRuby is best (at this point) for server deployments or for desktop scripts that need to run for longer. A single-line “Hello World” is probably one of the worst things that you could use to form an opinion about JRuby’s comparitive performance. We will come back to performance in a different post.
Install a gem
All Ruby programmers will install and use gems. In MRI, 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]
). JRuby works exactly the same as the second way (but just with jruby instead of ruby).
The equivalent JRuby command changes gem install
to jruby -S gem install
and that’s all it takes. For simplicity, let’s install sinatra
on JRuby and see what outputs we get.
$ jruby -S gem install --no-document sinatra
Fetching sinatra-4.0.0.gem
Fetching rack-protection-4.0.0.gem
Fetching mustermann-3.0.0.gem
Fetching rack-3.0.10.gem
Fetching tilt-2.3.0.gem
Fetching rack-session-2.0.0.gem
Successfully installed tilt-2.3.0
Successfully installed rack-3.0.10
Successfully installed rack-session-2.0.0
Successfully installed rack-protection-4.0.0
Successfully installed mustermann-3.0.0
Successfully installed sinatra-4.0.0
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 JRuby 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 by updating the PATH and other variables.
Open a command prompt that has JRuby on the path and do the equivalent of gem install pik
$ jruby -S 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 7 seconds
1 gem installed
Once this is done, you need to add the current installation to your pik (adjust the path to your JRuby executable).
$ pik add d:\jruby-9.4.6.0\bin
** Adding: 946: jruby 9.4.6.0 (3.1.4) 2024-02-20 576fab2c51 OpenJDK 64-Bit Server VM 25.265-b01 on 1.8.0_265-b01 +jit [x86_64-mswin32]
Located at: D:\jruby-9.4.6.0\bin
After this, you should be able to see something like this on the command line.
$ pik jr
Select which Ruby you want:
1. 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 [mswin32-x86_64]
2. 932: jruby 9.3.2.0 (2.6.8) 2021-12-01 0b8223f905 OpenJDK 64-Bit Server VM 25.265-b01 on 1.8.0_265-b01 +jit [mswin32-x86_64]
3. 946: jruby 9.4.6.0 (3.1.4) 2024-02-20 576fab2c51 OpenJDK 64-Bit Server VM 25.265-b01 on 1.8.0_265-b01 +jit [x86_64-mswin32]
4. 928: jruby 9.2.18.0 (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]
?
Enter a number to switch to that JRuby. The next time you do a > jruby -S gem list
it will use that JRuby.
You can also list all installed Ruby versions by doing:
$ 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]
330: ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x64-mingw-ucrt]
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 [mswin32-x86_64]
928: jruby 9.2.18.0 (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]
932: jruby 9.3.2.0 (2.6.8) 2021-12-01 0b8223f905 OpenJDK 64-Bit Server VM 25.265-b01 on 1.8.0_265-b01 +jit [mswin32-x86_64]
* 946: jruby 9.4.6.0 (3.1.4) 2024-02-20 576fab2c51 OpenJDK 64-Bit Server VM 25.265-b01 on 1.8.0_265-b01 +jit [x86_64-mswin32]
This site has a separate page on how to use pik if you want to know more.
Mapping the Ruby commands to JRuby
The short rule is this: irb is jirb, the rest are jruby -S command
. Some of this is from https://github.com/jruby/jruby/wiki/GettingStarted#running-rake-gem-rails-etc
Note: that’s an upper-case “S” – you can potentially spend a lot of time wondering why a lower case “s” does not behave properly (don’t ask me how I know).
This simply gives us this:
Ruby Command | JRuby Command |
---|---|
irb | jirb |
gem install | jruby -S gem install |
gem list | jruby -S gem list |
rails new project | jruby -S rails new project |
bundle install | jruby -S bundle install |
What about Bundler?
Keep in mind that bundler needs to be installed before that last command runs. You can install the latest version by doing this.
$ jruby -S gem install bundler
Fetching bundler-2.5.9.gem
Successfully installed bundler-2.5.9
Parsing documentation for bundler-2.5.9
Installing ri documentation for bundler-2.5.9
Done installing documentation for bundler after 2 seconds
1 gem installed
If you use bundler and look at the Gemfile.lock, you should see something like this in the file which shows you that it’s JRuby (actually, Java)
PLATFORMS
java
What’s next?
A good next post to read looks at running scripts and migrating to JRuby fromm Ruby. Also, check out other JRuby-related posts are linked from my post on why I like using JRuby.
Links and References
Here are the main links:
- JRuby Website: https://www.jruby.org/
- Download JRuby: https://www.jruby.org/download
- Getting started pages from the wiki: https://github.com/jruby/jruby/wiki/GettingStarted
- The wiki is at: https://github.com/jruby/jruby/wiki
- Other Walkthroughs and Tutorials: https://github.com/jruby/jruby/wiki/WalkthroughsAndTutorials
OK, so we have done enough for Day 0 – we know how to install JRuby, run a script with JRuby, install gems directly, switch between different JRuby installation, and also use Bundler. We are ready for more!