Ruby 3.2.1 with YJIT

I've been using Ruby 3.2.1 + YJIT for some time now and overall, I'm seeing anywhere from 10-20% performance improvements over running Ruby 3.0.5. Sorry, I don't have an apple-to-apple comparison for just YJIT vs non-YJIT. However, the performance improvements are real world scenarios on a Ruby on Rails 7.0.4 application.

So, I've recently formatted my primary desktop which is running macOS 13.2.1 and figured that I would document my steps on my preferred way of installing Ruby as well as getting YJIT to work. It's important to note that this is also an Apple Silicon machine. These steps should work regardless of which Apple Silicon you're using.

First, I'll install homebrew. This will automatically install the Command Line Tools for Xcode.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

You'll then be prompted to run some additional commands.

(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/$(whoami)/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

We can then start our install of Ruby. However, there are a few things that we should do first. Firstly, we need to select a manager for our Ruby interpreter. I normally use RVM as it's what I used for the longest time. It works well and I never had any complaints. However, with Ruby 3.2.1, we're also going to need an installation of Rust if we want to enable YJIT. So, instead of RVM, I'm going to be using ASDF.

brew install asdf
echo -e "\n. $(brew --prefix asdf)/libexec/asdf.sh" >> ${ZDOTDIR:-~}/.zshrc
source ~/.zshrc

Now that we have a manager for our Ruby and Rust, we can now proceed to install Rust.

asdf plugin add rust
asdf install rust latest
asdf global rust latest

Now that Rust is installed, we can proceed with our Ruby installation. Normally there are some dependencies that we need to handle, but luckily, asdf will take care of that for us.

asdf plugin add ruby

However, there is a strange thing with the mapping of the libraries so if we were to try and install Ruby right now, it would fail. So let's take care of this. This is around the libyaml package and we need to set some environment variables for this. I've added all four of these to my ~/.zshrc so that Ruby can install properly and YJIT is enabled out of the box.

brew install openssl@3 readline libyaml gmp
echo 'export LDFLAGS="-L$(brew --prefix libyaml)/lib"' >> ~/.zshrc
echo 'export CPPFLAGS="-I$(brew --prefix libyaml)/include"' >> ~/.zshrc
echo 'export RUBY_YJIT_ENABLE=1' >> ~/.zshrc
echo 'export RUBY_CONFIGURE_OPTS=--enable-yjit' >> ~/.zshrc
source ~/.zshrc
asdf install ruby 3.2.1
asdf global ruby 3.2.1

I had to restart my terminal, but once I did, everything seemed to work as expected.

ruby -v
ruby 3.2.1 (2023-02-08 revision 31819e82c8) +YJIT [arm64-darwin22]