This repository hosts Ruby language ruleset for Bazel.
The ruleset is known to work with:
- Bazel 8 using WORKSPACE and Bzlmod (tested on CI).
- Bazel 7 using WORKSPACE and Bzlmod (no longer tested on CI).
- Bazel 6 using WORKSPACE and Bzlmod (no longer tested on CI).
- Install the ruleset following WORKSPACE instructions on the latest release.
- Download and install Ruby:
# WORKSPACE
load("@rules_ruby//ruby:deps.bzl", "rb_register_toolchains")
rb_register_toolchains(
version = "3.3.9",
# alternatively, load version from .ruby-version file
# version_file = "//:.ruby-version",
)- (Optional) Download and install Bundler dependencies:
# WORKSPACE
load("@rules_ruby//ruby:deps.bzl", "rb_bundle_fetch")
rb_bundle_fetch(
name = "bundle",
gemfile = "//:Gemfile",
gemfile_lock = "//:Gemfile.lock",
)- Start defining your library, binary and test targets in
BUILDfiles.
- Install ruleset following Bzlmod instructions on the latest release.
- Download and install Ruby:
# MODULE.bazel
ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby")
ruby.toolchain(
name = "ruby",
version = "3.0.6",
# alternatively, load version from .ruby-version file
# version_file = "//:.ruby-version",
)
use_repo(ruby, "ruby")- (Optional) Download and install Bundler dependencies:
# MODULE.bazel
ruby.bundle_fetch(
name = "bundle",
gemfile = "//:Gemfile",
gemfile_lock = "//:Gemfile.lock",
)
use_repo(ruby, "bundle", "ruby_toolchains")- Register Ruby toolchains:
# MODULE.bazel
register_toolchains("@ruby_toolchains//:all")- Start defining your library, binary and test targets in
BUILDfiles.
- See repository rules for the documentation of
WORKSPACErules. - See rules for the documentation of
BUILDrules. - See rails for the documentation of Ruby on Rails rules.
See examples directory for a comprehensive set of examples how to use the ruleset.
The following toolchains are known to work and tested on CI.
| Ruby | Linux | macOS | Windows |
|---|---|---|---|
| MRI 3.5 | π© | π© | π₯ |
| MRI 3.4 | π© | π© | π© |
| MRI 3.3 | π© | π© | π© |
| MRI 3.2 | π© | π© | π© |
| JRuby 10.0 | π© | π© | π© |
| TruffleRuby 25.0 | π© | π© | π₯ |
The following toolchains were previously known to work but no longer tested on CI.
| Ruby | Linux | macOS | Windows |
|---|---|---|---|
| MRI 3.1 | π© | π© | π© |
| MRI 3.0 | π© | π© | π© |
| MRI 2.7 | π© | π© | π© |
| JRuby 9.4 | π© | π© | π© |
| JRuby 9.3 | π© | π© | π© |
| TruffleRuby 24.0 | π© | π© | π₯ |
| TruffleRuby 23.0 | π© | π© | π₯ |
| TruffleRuby 22.0 | π© | π© | π₯ |
On Linux and macOS, ruby-build is used to install MRI from sources. Keep in mind, that it takes some time for compilation to complete.
On Windows, RubyInstaller is used to install MRI.
For faster MRI installation on Linux and macOS, you can use prebuilt Ruby binaries from rv-ruby instead of compiling from source. This significantly reduces installation time and ensures consistent, portable Ruby environments.
Configure rv-ruby Downloads
To securely download and properly cache the Ruby binaries, the ruby.toolchain
declaration must be updated with the rv_version and rv_checksums attributes.
We have provided the generate_rv_checksums utility to add/update these
attributes for you. The utility needs to know the rv-ruby version to use
(https://github.com/spinel-coop/rv-ruby/releases) and the version of Ruby to
download. By default, it will use the Ruby version specified in the
.ruby-version file.
bazel run @rules_ruby//tools/generate_rv_checksums -- 20251225After running the utility, the toolchain declaration in your MODULE.bazel
should look something like the following:
ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby")
ruby.toolchain(
name = "ruby",
version_file = "//:.ruby-version",
rv_version = "20251225",
rv_checksums = {
"linux-arm64": "0c08c35a99f10817643d548f98012268c5433ae25a737ab4d6751336108a941d",
"linux-x86_64": "f36cef10365d370e0867f0c3ac36e457a26ab04f3cfbbd7edb227a18e6e9b3c3",
"macos-arm64": "cd9d7a1428076bfcc6c2ca3c0eb69b8e671e9b48afb4c351fa4a84927841ffef",
"macos-x86_64": "e9da39082d1dd8502d322c850924d929bc45b7a1e35da593a5606c00673218d4",
},
)Configure Excluded Gems
When using rv-ruby, you must exclude default gems with C extensions from
bundle_fetch as these are pre-compiled in the rv-ruby binary. You may see
compilation errors if you do not exclude these gems.
We have provided the generate_excluded_gems utility to update the declaration
for you.
bazel run @rules_ruby//tools/generate_excluded_gemsThe utility reads the Ruby version being used and checks
https://raw.githubusercontent.com/janlelis/stdgems/main/default_gems.json to
determine which gems should be excluded. The utility adds/updates the
excluded_gems attribute with the correct list of gems. The bundle_fetch
declaration will look something like the following:
ruby.bundle_fetch(
name = "bundle",
gemfile = "//:Gemfile",
gemfile_lock = "//:Gemfile.lock",
excluded_gems = [
"date", "digest", "etc", "fcntl", "fiddle",
"io-console", "io-nonblock", "io-wait", "json",
"openssl", "pathname", "prism", "psych",
"stringio", "strscan", "zlib",
],
)Note
You can find an HTML-rendered list of the default gems for a Ruby version at https://stdgems.org/\<version> (e.g., https://stdgems.org/3.4.8 for Ruby 3.4.8).
Notes:
rv-rubyis only supported on Linux and macOS (x86_64 and arm64).- On Windows, the toolchain automatically falls back to RubyInstaller.
- Find available
rv-rubyreleases at https://github.com/spinel-coop/rv-ruby/releases - The utilities support
--nameto target specific toolchains/bundles and--module-bazelto specify a custom MODULE.bazel path. - Run utilities with
--dry-runto preview changes without modifying files.
On all operating systems, JRuby is downloaded manually. It uses Bazel runtime Java toolchain as JDK. JRuby is currently the only toolchain that supports Remote Build Execution.
On Linux and macOS, ruby-build is used to install TruffleRuby. Windows is not supported.
On Linux and macOS, you can potentially use any Ruby distribution that is supported by ruby-build. However, some are known not to work or work only partially (e.g. mRuby has no bundler support).
- JRuby/TruffleRuby might need
HOMEvariable exposed. Seeexamples/gem/.bazelrcto learn how to do that. This is to be fixed injruby/jruby#5661andoracle/truffleruby#2784. - JRuby might fail with
Errno::EACCES: Permission denied - NULerror on Windows. You need to configure JDK to allow proper access. This is described injruby/jruby#7182. - RuboCop < 1.55 crashes with
LoadErroron Windows. This is fixed inrubocop/rubocop#12062. - REPL doesn't work when used with
bazel test. To work it around, use a debugger with remote client support such asruby/debug. Seeexamples/gem/.bazelrcto learn how to do that. - Some gems contain files with spaces which cause Bazel error
link or target filename contains space. To work it around, use--experimental_inprocess_symlink_creationBazel flag. Seebazelbuild/bazel#4327for more details.