bideowego
bideowego

Reputation: 451

Dynamic Gemfile Ruby version for both rbenv and RVM

I use rbenv but other team members use RVM.

When specifying the ruby 2.x.x version in the Gemfile I’ve been doing this:

ruby ENV['RBENV_VERSION'] || '2.2.4'

which grabs the current version I’m using from rbenv and uses it for the Gemfile. However, in production, it is not present and uses the specified version after ||.

I’ve been searching for a similar way to do this in RVM, the ultimate goal being to set up a Gemfile where all developers can use their local version of Ruby and a concrete version is specified for production.

This would allow developers to use rbenv or RVM for a project as well as not need to install new versions of Ruby every time they work on a project with a version they don’t have installed.

Are there any RVM users that can give me an equivalent to ENV['RBENV_VERSION'] for RVM? I’ve been searching a lot and haven’t found anything I like.

The best answer I can see is having RVM users specify the version via an environment variable name agreed upon by the team and used like "Specifying a Ruby version via the environment" as well and use that instead in the Gemfile.

Upvotes: 4

Views: 652

Answers (1)

Michael Gaskill
Michael Gaskill

Reputation: 8042

To get the current Ruby version from any RVM instance, you can query it using this method:

rvm list default string | sed s/ruby-//

You can also use the RVM prompt tools to query the current version

rvm-prompt i v | sed s/ruby-//

or

rvm-prompt i v p g | sed s/ruby-//

depending on how detailed you want to allow. Docs for the rvm-prompt command can be found in "rvm-prompt".

If the user chooses to leave RVM at its default version, you get that version reported, but if the Ruby version has been changed in RVM, you get the currently chosen version.

You can chain this in any way that ENV['RBENV_VERSION'] is used, such as:

`rvm list default string | sed s/ruby-//` || 2.2.4

This will choose the current RVM Ruby version or 2.2.4 if RVM doesn't show a version.

Note that RVM has to exist (or at least something called rvm has to be available as an executable). If not, an additional script wrapper may be needed to handle the situation in which it doesn't exist and degrade gracefully.

Personally, I love having it available everywhere, including production environments. It's the very first thing that I install on a new OS instance, even before the text editor, and the second thing that I do is install the appropriate Ruby version with RVM.

Upvotes: 1

Related Questions