Ruby 3.2.0 is here and it offers some nice additions to the language.
One of the most exciting new things is the addition of a compiler, YJIT. YJIT was created by the folks from Shopify and has been producion tested for a while, so it is safe to use in your environment.
Some benchmarks show the difference in speed compared to Ruby without YJIT. I'll put some links if you want to know more:
https://speed.yjit.org/
https://www.solnic.dev/p/benchmarking-ruby-32-with-yjit
I will show you the steps necessary to install Ruby using rbenv as it is the most popular Ruby version manager.
Install Rust
To install Ruby 3.2.0 with YJIT, the first step is to install Rust. Rust is necessary if we want to add YJIT to our Ruby installation.
The easiest way to install Rust is using Rustup, which is also the recommended way:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
You'll be asked which type of installation you want, you can use the default.
If you want alternative installation methods you can follow this link.
After that, update your $PATH variable. You can do this in many ways, I will show you how to do it by updating your bashrc or zshrc (depending which shell you use).
Just open ~/.bashrc or ~/.zshrc using your preferred editor and at the end of the file just add the following:
export PATH="$HOME/.cargo/bin:$PATH"
This will add the rust bin folder to your path, so you can check if rust was installed correctly easier. After that you have to source your file:
source ~/.bashrc
or
source ~/.zshrc
Then you can check if the Rust installation was successful:
rustc --version
You should get an output like this that shows the version of Rust:
rustc 1.66.0 (69f9c33d7 2022-12-12).
Upgrade Ruby build
Before you install Ruby 3.2.0 using rbenv, first you should update your ruby-build tool.
brew update && brew upgrade ruby-build
This command could take some time, just so you know. :)
Install Ruby
You are now ready to install Ruby 3.2.0 with YJIT.
Enter the following command:
RUBY_CONFIGURE_OPTS="--enable-yjit" rbenv install 3.2.0
You notice the RUBY_CONFIGURE_OPTS variable where you explicitly say that you want to enable YJIT. You can also add the --with-jemalloc flag as well to install jemalloc, the general purpose malloc implementation that performs better at memory usage than the standard malloc.
Many benchmarks showed how much better your applications perform using jemalloc, I can also confirm that from my personal experience.
Here are a few links that show the performance gain from using jemalloc:
https://engineering.binti.com/jemalloc-with-ruby-and-docker/
https://engineering.appfolio.com/appfolio-engineering/2018/2/1/benchmarking-rubys-heap-malloc-tcmalloc-jemalloc
https://medium.com/motive-eng/we-solved-our-rails-memory-leaks-with-jemalloc-5c3711326456
You can set the new Ruby version for your local project or even on your entire environment using the local/global flags:
rbenv local 3.2.0
rbenv global 3.2.0
Don't forget to run the rehash command for rbenv:
rbenv rehash
The last step is to ensure that everything went right. Check your ruby version this way:
ruby -v --yjit
With the --yjit option, we should get the following output:
ruby 3.2.0 (2022-12-25 revision a528908271) +YJIT [x86_64-darwin21]
The +YJIT shows that YJIT has been enabled in your Ruby 3.2.0 installation.
Nice work!
I hope this little tutorial will help you. :)
Top comments (1)
It is now available on heroku. devcenter.heroku.com/articles/ruby...
Thanks!!