RVM and automatically switching gemsets - ruby

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?

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

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.

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.

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

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

It is possible: http://rvm.io/workflow/rvmrc/:
rvm use ruby#gemset

Related

How can I make RVM respect my bundler's settings on where to put gems?

I have my bundler configured to install gems into a .bundle/ directory inside each project.
$ cat ~/.bundle/config
---
BUNDLE_PATH: ".bundle"
BUNDLE_BIN: ".bundle/bin"
How can I make RVM respect this setting and not set the $GEM_HOME to another directory (inside ~/.rvm/) whenever I cd into my project?
So far I had a little script that would set $GEM_HOME (and $PATH and $GEM_PATH) whenever I would cd inside my project's directory, but with recent versions of rvm it has stopped working and now RVM basically unsets the Ruby version whenever I manually change $GEM_HOME and I have no clue how to make it work again.
First of all you should specify that you want to use the very separate gemset for your project (consider it’s name is myproject42):
$ cd myproject42 && rvm --rvmrc --create use 2.1.0#myproject42 --ignore-gemsets
rvmrc option will create the configuration file in your folder. You are already half-separated from the universe. Now you want to modify this file a bit, setting whatever you want:
...
unset __hook
# my exports
export GEM_PATH=`pwd`
export GEM_HOME=`pwd`
...
cd out and back in into this directory. This will result in the warning:
You are using '.rvmrc', it requires trusting, it is slower and ...
[LINES SKIPPED]
************************
y[es], n[o], v[iew], c[ancel]> y
Using: /tmp/myproject42
Once you confirm you are aware of this modification, the gemset is set to what you wanted (/tmp/myproject42 in this case.) You may verify that:
$ rvm gemset dir
Warning! PATH is not properly set up, '/tmp/myproject42/bin' is not available,
[LINES SKIPPED]
/tmp/myproject42
You might want to make some cleanup, like suppressing those warnings. The “howtos” are printed out inside this warnings, they are pretty straightfoward so I left the description of initial process “dirty” to show it exactly how it would be run on your machine.
I hope this helps.

RVM set per user ruby versions

How would you setup RVM, so that some users automatically get one version of ruby, while others get a different version of ruby?
For example, I want the following users to always have these version of ruby
git => 1.9
root => system
bob => 2.0
Looking at the rvm documenation, I can't find any referernce to per user settings.
How could I accomplish this?
I'm hoping there is a config file that can be placed somewhere. Because I'm automating the server installation with puppet, running rvm commands by hand isn't feasable.
Update
I've installed rvm, using this puppet module, https://forge.puppetlabs.com/maestrodev/rvm
It appears that all users have access to the rvm command.
You would just need to make a .rvmrc file for each user's $HOME directory.
http://rvm.io/workflow/rvmrc
The maestrodev/rvm module installs rvm and rubies system wide, then you need to use rvm:system_user to add each user to the rvm group
rvm::system_user { bturner: ; jdoe: ; jsmith: ; }
and as msergeant mentioned, add the .rvmrc to each user' home.

Gemsets file isn't working

I have a folder which contains a file named gemsets.rvmrc. The file contains this:
rvm use #pa
Running this code in the terminal successfully switches over to the correct gemset. I'd heard that putting this file in the folder would do that automatically when I CD to that folder, but this isn't the case. Running a ruby file in the same folder, test.rb, gives errors until I run that code in the terminal to switch to the correct gemset.
gemsets.rvmrc is not a supported file, it should be just .rvmrc, but using just gemset not always works - check this bug: https://github.com/wayneeseguin/rvm/issues/2078

Make rvm switch to system ruby on osx

I have a project that needs to use the system installed version of Ruby on OSX (1.8.7). My default rvm ruby is set to 2.0.0p0 and I want to keep it that way. Using RVM 1.19.6 (latest?)
From everything that I've read I should be able to simply create a .ruby-version file in my project directory and when I switch to that directory it should automatically switch to the system ruby.
According to the documentation I should be able to simply issue
rvm --create --ruby-version use system#myproject
and that should take care of everything. This however gives me
Unrecognized command line argument: 'rmvrc' ( see: 'rvm usage' )
What am I doing wrong? Is the documentation out of date?
Try creating a .rvmrc file in your project root, like:
echo 'rvm system#myproject --create' > .rvmrc
rvm reload
However I wouldn't recommend using system version, rvm installs the different ruby versions in its own directory and manages them easily. Alas system version is a bit outdated.

How to tell Terminal which version of Ruby to use?

I have two related questions that I was hoping someone could help out with.
I recently installed Ruby 1.9.2 on my Mac (running Snow Leopard 10.6.4) and I haven’t been able to figure out how to get Terminal to use the new Ruby as a default, rather than the factory-installed Ruby 1.8.7. The old Ruby 1.8.7 is located in my ~/usr/bin/ruby directory while the new Ruby 1.9.2 is in ~/usr/local/bin/ruby. Someone said that I need to put the new version of Ruby's directory in the PATH prior to the old version's directory so that the system looks there first - is this correct? If so, can anyone provide step by step instructions on how to do this?
I’ve created a new directory but can’t seem to figure out the correct way to add that directory to my PATH using the Terminal bash shell. I tried using the instructions that I found here (http://www.macgasm.net/2008/04/10/ad...thin-terminal/) twice but they didn't work for me. The directory containing my program ("Ruby_Programs") shows up in the PATH but when I try to run "ruby newprogram.rb" from the command line it results in ":ruby: No such file or directory -- newprogram.rb (LoadError)". The file definitely exists and is a functional Ruby program. I did change the name of the directory to "Ruby Programs" and then back to "Ruby_Programs" - could that have somehow caused this problem?
Any help would be greatly appreciated. Here is my current PATH:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/sbrriffe/src:/usr/X11/bin:/Users/sbriffe/Ruby_Programs/:
You might want to check out rvm. You can install multiple versions of ruby side by side and easily switch between them. If you follow the rvm installation notes you won't have any more path problems.
Your Ruby Programs directory shouldn't be in your path: the location of your ruby interpreter should be. Then, you cd to the location of your ruby program, and run it from there: ruby program.rb.
Since you are on a Mac, check out homebrew for something that will make installing software easier. I have my homebrew set up in /usr/local, and it works great.
Once you have installed stuff where you need it, then you'll want to adjust your $PATH. The items in $PATH are searched in the order they appear, so in your ~/.bashrc, you'll want to add:
export PATH=/usr/local/bin:$PATH
To make sure /usr/local/bin gets searched before /usr/bin.
I would use RVM to get everything installed, and then once you have RVM installed it is easy to set your default Ruby version.
Check out https://rvm.io/ -- once you have that installed you can change your default by using : $ rvm use 1.9.2 --default
hope that helps- you can do this with any version, not only 1.9.2

Resources