[Ruby on Rails (RoR)] Use asdf and Ruby plugin to install multiple Ruby versions

asdf and Ruby Plugin

asdf is a single CLI tool for managing multiple runtime versions. It extend with a simple plugin system to install your favourite language: Dart, Elixir, Flutter, Golang (Go), Java, Node.js, Python, Ruby …

This article is about how to use asdf and Ruby plugin to install multiple Ruby versions on macOS with the Homebrew package manager.

Prerequisites

  • Xcode Command Line Tools
    The Xcode Command Line Tools Package is a small self-contained package available for download separately from Xcode and that allows you to do command line development in macOS which is consists of the macOS SDK and command-line tools such as Clang, which are installed at this

    For more information about installing and using Xcode Command Line Tools, see the Xcode Command Line Tools

  • Homebrew
    Homebrew is the Missing Package Manager for macOS (or Linux).

    For more information about installing and using Homebrew, see the Homebrew - https://brew.sh/.

Installation

Install asdf

1
2
3
4
5
# Install asdf dependencies
$ brew install coreutils curl git

# Install asdf
$ brew install asdf

asdf Ruby Plugin

1
2
3
4
5
# Install Ruby plugin requirements
$ brew install openssl readline

# Install Ruby plugin
$ asdf plugin-add ruby https://github.com/asdf-vm/asdf-ruby.git

Add to your Shell

Add asdf.sh to your ~/.zshrc with:

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

Or see Add to your Shell - https://asdf-vm.com/#/core-manage-asdf to learn when you use other shells.

List all Ruby Version

Update Ruby plugin.

1
$ asdf plugin update ruby

List all Ruby versions.

1
$ asdf list all ruby

Install Ruby version

Install a Ruby version manually.

1
2
3
4
$ asdf install ruby 2.7.2

# Or install another version
# asdf install ruby 2.6.1

Migrating from another Ruby version manager

.ruby-version file

asdf uses the .tool-versions for auto-switching between software versions. To ease migration, you can have it read an existing .ruby-version file to find out what version of Ruby should be used. To do this, add the following to $HOME/.asdfrc:

1
legacy_version_file = yes

If you are migrating from version manager that supported fuzzy matching in .ruby-version like rvm or chruby, note that you might have to change .ruby-version to include full version (e.g. change 2.6 to 2.6.1).

FAQs

env: ruby_executable_hooks: No such file or directory

1
2
$ bundle
env: ruby_executable_hooks: No such file or directory

Install bundler gem, then run bundle against.

1
2
3
$ gem install bundler

$ bundle

/.asdf/shims/rails: line 8: /usr/local/Cellar/asdf/0.9.0/libexec/bin/asdf: No such file or directory

1
2
3
4
5
$ rails c
~/.asdf/shims/rails: line 8: /usr/local/Cellar/asdf/0.9.0/libexec/bin/asdf: No such file or directory

$ cat ~/.asdf/shims/rails
exec /usr/local/Cellar/asdf/0.9.0/libexec/bin/asdf exec "rails" "[email protected]"

Run asdf reshime to update all shims files after moving or deleting ~/.asdf/shims.

1
2
3
$ mv ~/.asdf/shims ~/.asdf/shims.0.9.0

$ asdf reshime

See bug: Updating asdf did not update version number in shim paths · Issue #1147 · asdf-vm/asdf - https://github.com/asdf-vm/asdf/issues/1147 to learn more.

References

[1] Manage asdf - https://asdf-vm.com/#/core-manage-asdf

[2] asdf-vm/asdf-ruby: Ruby plugin for asdf version manager - https://github.com/asdf-vm/asdf-ruby

[3] Home · rbenv/ruby-build Wiki - https://github.com/rbenv/ruby-build/wiki#suggested-build-environment

[4] Switching to asdf - https://wolfgangrittner.dev/switching-to-asdf-for-ruby/

[5] ASDF VM Version Manager for Ruby Tutorial - https://www.nikitakazakov.com/asdf-vm-version-manager-for-ruby-tutorial/

[6] Homebrew - https://brew.sh/

[7] How to Use asdf Version Manager on macOS | Peaceful Revolution - https://www.wiserfirst.com/blog/how-to-use-asdf-on-macos/