I have been using rvm to manage my ruby versions for a long time. With a few macOS upgrades, ruby installations via rvm has been failing for a myriad of reasons which prompted me to look for an alternative version manager. I settled on using docker images and docker as my version manager.

Installations

Install docker engine from here

A Dockerfile for every ruby version

Its best to have a separate dockerfile for every ruby project and import the ruby version of your liking in the first line of the dockerfile.

# Import your ruby version
FROM ruby:2.7.1
# Install bundler gem
RUN gem install bundler
# Assign a work directory
WORKDIR /work
Code Snippet 1: dockerfile for ruby version 2.7.1

Run your script from docker

  1. Copy the dockerfile to the folder where you have your ruby code.

  2. Build your ruby docker image

    docker build -t ruby-2.7.1 .
    
    Code Snippet 2: Build Docker

    I use the respective ruby version as the tag of the docker image. Here it is ruby-2.7.1

  3. Start the docker image. pwd will associate the current folder to the docker’s work directory

    docker run --name ruby-2.7.1 -d -v "$(pwd)":/work -it ruby-2.7.1
    
    Code Snippet 3: Run the docker image
  4. Access the running docker container’s shell using exec and run bundle install if you have a gemfile for your project

    docker exec -it ruby-2.7.1 /bin/bash
    cd your_work_directory
    bundle install
    
    Code Snippet 4: bundle install is optional
  5. Run your ruby script

    ruby script.rb
    
  6. Exit the docker container and stop the container once you are done with development

    # Press ctl+c to exit from the container
    docker stop ruby-2.7.1
    # To start development, just start the container again and follow the above steps
    docker start ruby-2.7.1
    

Whats so special?

Most of the commands mentioned here are non-trivial, the best takeaway is when you need a new ruby version, you could just duplicate the current dockerfile and change the first line of the dockerfile to the ruby version you require. Also debugging becomes very easy and byebug works with no problems.

On the other hand, if you would just like to run a ruby script with the docker image

docker run ruby-2.7.1 ruby sample_script.rb
Code Snippet 5: Run your script with ruby docker image

As an end note, for those of you who would still like to make your rvm working again in the new macOS versions, Jakob Skjerning has a very good blog on solving the issues.