Switching Ruby versions with Chruby

What is Chruby?

Chruby is a lightweight Ruby version management tool. Chruby's code is just 90 lines and it has perfect test coverage. 

Swap To Use Ruby Version Management Tools

Some convenience images don't support using rvm or rbenv, such as the Android image. While a Ruby system is available, it may not be the version you need.

If you encounter this you can utilize Chruby to download the proper Ruby version and swap to utilizing it in the rest of your job steps. The following will work out of the box for the cimg/android:api-29-node Android image:

- run:
    name: install chruby
    command: |
      wget -O chruby-0.3.9.tar.gz https://github.com/postmodern/chruby/archive/v0.3.9.tar.gz
      tar -xzvf chruby-0.3.9.tar.gz
      cd chruby-0.3.9/
      sudo make install
- run: ruby-install ruby 2.7.1
- run:
    name: Set chruby
    command: |
      echo 'source /usr/local/share/chruby/chruby.sh' >> $BASH_ENV
      echo 'chruby ruby-2.7.1' >> $BASH_ENV 

The above installs Chruby, then installs Ruby version 2.7.1 (as an example, you can change this to fit your needs), then sets that version to be used in the rest of the build. While the above will work outright for the image listed above, adjustments may be needed depending on what environment you are trying to install on. If you need assistance with that you can reach out to the Support team and we can take a look.

One thing to note is downloading and installing a version of Ruby takes a bit of time, so you may want to utilize caching to speed up future builds that implement this solution.

Was this article helpful?
2 out of 12 found this helpful



Article is closed for comments.