Ruby Version Manager: rbenv

Docker

The best way to manage ruby for your project is by using Docker:

If you don’t have a Docker setup yet, you have to suffer, temporarily, by using rbenv. There used to be another tool called rvm for this sort of thing but it’s been abandoned for a long time now, it’s insecure to install from their website, has install errors, and it’s completely broken for newer versions of ruby and newer versions of OpenSSL. Your only real option is to use rbenv.

Prerequsites

You can only install this via homebrew, unfortunately.

Setup

  1. Install it via homebrew:

    brew install rbenv
    
  2. Get the shell setup command:

    rbenv init
    
  3. Update your Enable funtion in .bash_profile to contain rbenv:

    function Enable {
      case "$1" in
        "brew")
          eval "$(~/Workspace/Hub/brew/bin/brew shellenv)"
          shift
          ;;
        "rbenv")
          Enable rbenv
    
          eval "$(rbenv init - bash)"
          shift
          ;;
        *)
          return
          ;;
      esac
    }
    

    See: How to install Homebrew securely

  4. Finally, execute the same eval in your current shell:

    eval "$(rbenv init - bash)"
    

Usage

FIrst you must make sure to enable it in your current shell:

Enable rbenv

Install the ruby versions you need:

rbenv install 2.7.6
rbenv install 3.0.4
rbenv install 3.1.2

List versions:

rbenv versions

Set a default global ruby version:

rbenv global 3.1.2

OpenSSL

If you have OpenSSL issues, search for an older or compatible version:

brew search /openssl@/

Install it if you don’t have it already, then configure the builder to use it:

export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"

Now you can retry your install command!

Project

Set up your project by creating a .ruby-version file in the root directory, with the desired version:

3.1.2

Now when you cd . you should see the ruby --version changing, or a warning if you don’t have the appropriate ruby installed.