In this blog post, I will explain how to install Ruby with YJIT enabled and chruby. If you are looking for Ruby installation without YJIT, please visit How To Install Ruby 3.2 with chruby and uninstall Ruby 3.1 from chruby post.

YJIT is a lightweight, minimalistic Ruby JIT built inside CRuby. it is a new just-in-time compiler for Ruby that was introduced in Ruby 3.0.

YJIT promises faster performance for Ruby programs by compiling them at runtime. It lazily compiles code using a Basic Block Versioning (BBV) architecture. The target use case is that of servers running Ruby on Rails.

Requirements

  • Ruby dependencies.
  • chruby - I assume chruby is already installed, I’ll skip the installation process.
  • The Rust compiler rustc and Cargo (if you want to build in dev/debug mode)
    • The Rust version must be >= 1.58.0.

Ruby Dependencies

  1. Install the prerequisite dependencies for building the CRuby interpreter:

    • C compiler

    For RubyGems, you will also need:

    • OpenSSL 1.1.x or 3.0.x / LibreSSL
    • libyaml 0.1.7 or later
    • zlib

    If you want to build from the git repository, you will also need:

    • autoconf - 2.67 or later
    • bison - 3.0 or later
    • gperf - 3.1 or later
      • Usually unneeded; only if you edit some source files using gperf
    • ruby - 2.5 or later
      • We can upgrade this version to system ruby version of the latest Ubuntu LTS.
  2. Install optional, recommended dependencies:

    • readline/editline (libedit, to build readline)
    • libffi (to build fiddle)
    • gmp (if you with to accelerate Bignum operations)
    • libexecinfo (FreeBSD)
    • rustc - 1.58.0 or later (if you wish to build YJIT)

    If you installed the libraries needed for extensions (openssl, readline, libyaml, zlib) into other than the OS default place, typically using Homebrew on macOS, add --with-EXTLIB-dir options to CONFIGURE_ARGS environment variable.

     export CONFIGURE_ARGS=""
     for ext in openssl readline libyaml zlib; do
       CONFIGURE_ARGS="${CONFIGURE_ARGS} --with-$ext-dir=$(brew --prefix $ext)"
     done
    

Using Ubuntu, just copy and paste command below to install the required dependencies:

sudo apt install -y build-essential bison zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libffi-dev autoconf

How to Install Rust:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

MRI Ruby 3.2 Installation with YJIT Enabled for chruby

wget https://cache.ruby-lang.org/pub/ruby/3.3/ruby-3.3.1.tar.gz
tar -xvzf ruby-3.3.1.tar.gz
cd ruby-3.3.1

# Generate the configure file
./autogen.sh

# don't forget to create /opt/rubies directory
./configure --enable-yjit --prefix=/opt/rubies/ruby-3.3.1 --disable-install-doc

# If your machine have a lot of RAM, you can
# use `make -j install` to make installation faster
# `make install` compiles with a single cpu core only.
make install

You can test that YJIT works correctly by running:

# Quick tests found in /bootstraptest
make btest

# Complete set of tests
make -j test-all

If the installation is successful, upon reloading the shell, running chruby will display ruby-3.3.1:

$ chruby
   ruby-3.0.0
   ruby-3.1.0
   ruby-3.1.1
   ruby-3.2.2
   ruby-3.3.1

Note that there is also an environment variable RUBY_YJIT_ENABLE which can be used to enable YJIT. This can be useful for some deployment scripts where specifying an extra command-line option to Ruby is not practical.

You can verify that YJIT is enabled by checking that ruby -v --yjit includes the string +YJIT:

ruby 3.3.1 (2024-04-23 revision c56cd86388) +YJIT [x86_64-linux]