Switching Ruby versions with Chruby

Some convenience images don't support using rvm or rbenv, such as the Andriod image. While a system Ruby 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.

- 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, then sets that version to be used in the rest of the build. 

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?
1 out of 1 found this helpful

Comments

0 comments

Article is closed for comments.