28

Is it possible to have rvm know which gemset it should be using while navigating under a certain directory, much in the same way you can have git the current branch's information just by navigating under that directory?

I understand how git works that way since each directory has its own .git directory in the root, but didn't know if it was possible since .rvm is more of a user-wide configuration. Or perhaps the answer is to make a .rvm file within each directory?

1
  • 2
    Things might have changed since this question was first posed. The rvm docs seem to suggest that .rvmrc might not be the best way to go about this, since .versions.conf, .ruby-version and .ruby-gemset all exist for essentially the same purpose.
    – chb
    Dec 18, 2013 at 20:18

6 Answers 6

32

For others visiting this, there is a new way to do this, without having to allow arbitrary shell script to be executed in a .rvmrc file.

Create a file named .ruby-gemset containing only the gemset name in.

gemset

Need an up to date version of rvm for this to work.

You can also specify the ruby version by creating a file named .ruby-version containing only the ruby version:

1.9.3

This format also has the advantage of being compatible with rbenv and rbfu.

If you have existing projects using the deprecated .rvmrc, you can convert them to the new format using the command:

rvm rvmrc to .ruby-version
3
  • This answer could use some cleaning up, it's confusing in it's current state. Do you mean, create a file called .ruby-gemset and have it contain only gemset my_gemset_name? The same for .ruby-version, have it contain only 1.9.3?
    – sergserg
    Apr 7, 2014 at 0:16
  • 2
    As stated, the .ruby-gemset should just contain just the gemset name and .ruby-version should contain just the ruby version
    – Yule
    Apr 7, 2014 at 8:05
  • 3
    nothing happens for me when I cd in to the directory, i've followed the directions exactly. Am I missing something?
    – esreli
    Dec 18, 2015 at 3:45
30

Create a .rvmrc file in each project/branch with contents similar to this:

rvm gemset use xxxx

Save it. Next time you cd into that folder, you'll probably get a security prompt from RVM to make sure you want to use that .rvmrc. After accepting, you'll see "Now using gemset 'xxxx'" every time you navigate into that folder.

3
  • yeah, I did it, but it doesn't work fine - every time I enter the project folder I got notification (green colour in terminal) that this project is using x.x.x version of ruby, but when I want do something I have errors (different range errors: from active record ones to used gems etc.) - but when I type rvm use x.x.x (same version like in after enter project folder notification) everything works fine - do you know how to solve this? :/
    – lukaszkups
    May 28, 2012 at 16:44
  • 10
    This is the old way to do things. The new way is to use .ruby-version and .ruby-gemset files. Both must be present. See @Yule's answer below.
    – Dave Peck
    Aug 10, 2013 at 1:43
  • @DylanMarkow: ok, cd work well because of .. cd. But how to run rvm by chdir in other script languages? chdir to directory with .ruby-version and .ruby-gemset doesn't allow to autoload gemsets Sep 8, 2014 at 13:56
27

I find the easiest way to achieve this is to navigate to the project folder and then use the following command:

rvm --rvmrc --create <ruby>@<desired-gemset-name>

e.g. rvm --rvmrc --create 1.9.2-p290@testing_gemset

In one stroke, RVM will create the .rvmrc file, populate it, install the correct ruby version - if necessary - and (usually) switch to the correct ruby version and gemset. I say usually because I find that I sometimes have to cd . after performing that command to get RVM to pick up the changes.

0
1

Create a .ruby-version file with the contents [ruby version]@[gemset] in the project folder.

Example:

$ rvm gemset list

    gemsets for ruby-2.6.3 (found in /home/ec2-user/.rvm/gems/ruby-2.6.3)
       (default)
       hello
    => sample
       toy

$ echo "2.6.3@sample" > .ruby-version
0

If you cannot get .ruby-version / .ruby-gemset working, consider whether your terminal is using shell login and try running the command in the shell section at http://rvm.io/support/faq:

$ rvm get [head|stable] --auto
-1

It is possible: http://rvm.io/workflow/rvmrc/:

rvm use ruby@gemset

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.