When changing into Rails folder, have rvm pick ruby and gemset? - ruby-on-rails

For my different Rails folders, I would like to have rvm automatically load the correct gemset when running anything from 'bundle install' to doing my 'autotest' or rails console or server. Is this possible? Currently I have to manually do 'rvm use' which is getting a bit tedious as I am working on multiple Rails projects at the same time.

Create a .rvmrc file in your rails directory, and rvm will automatically load it. An example .rvmrc that loads Ruby 1.9.2 and a gemset named "rails3":
.rvmrc
rvm 1.9.2#rails3
You can do a lot more too, described in detail here: https://rvm.io/workflow/rvmrc/

For current versions of RVM, using two files is best practice:
.ruby-version
.ruby-gemset
http://rvm.io/workflow/projects

You can also use this in Gemfile
ruby '2.2.0'
ruby-gemset=significa
This way rvm will automatically pick the configured version

For current RVM version 1.29.1, the recommended way is
rvm --ruby-version use <ruby-version>#<gemset>
Exmaple
rvm --ruby-version use 2.4.0#rails5
cat .ruby-version # 2.4.0
cat ruby-gemset # rails5
this will generate two file .ruby-version and .ruby-version in your porject directory. This will compatible with other Ruby Versions Managers

Yaw just create two plain-text files and put into your project folder:
.ruby-gemset and .ruby-version
.ruby-gemset should contain only gemset alias name and nothing else
.ruby-version follows the same rules, put your ruby version or alias there

You can easily do that by placing an .rvmrc file at the base of your project.

I had the same problem. But, I found I had installed a ruby version globally while installing rvm.
I uninstalled that global version and everything started to work fine with .ruby-version and .ruby-gemset file in project's root directory.

Related

RVM's current and default version isn't actually being used

I am running my rails app from a virtualbox build by vagrant using puppet scripts. Every time I login to the box, I have the following problem:
When I run rvm list one of the things it lists is the following:
=* ruby-2.1.1
But when I try to run rails console, it tells me I need to install missing gems. When I run rvm use default, and then run rails console, it works. Why is the default and current setting in rvm not working--why do I have to go to the extra step of also telling rvm which ruby version to use?
Note: I do have a .ruby-version file with 2.1.1 in it. I'm using rvm version 1.25.25
Because you have to tell rvm which version to use.
In earlier version of rvm we have to define .rvmrc file which mention which ruby and gemset to use.
In recent version of rvm we have to define .ruby-version file with ruby version in it and .ruby-gemset file with the name of gemset.
If you just want a quick solution then in your rails directory make a .ruby-version file with content 2.1.1
correct syntax is:
rvm --default use ruby-2.1.1#global
This command sets ruby to selected default permanently. All new terminals will use your default Ruby. Also you don't mention anything about gemset, so I presume global would exist if you didn't mess up your setup.
This solved it:
I added rvm use --default to the machine's ~/.bashrc file.

Why does rvm switch gemsets when I cd into my rails project directory?

For what possible reason would rvm switch gemsets when I cd into my rails project directory?
I don't have an .rvmrc file. I set a default gemset (which "sticks" for other directories). Here's what I mean:
[20130109_234306] will#localhost:~
$ rvm gemset list
gemsets for ruby-1.9.3-p362 (found in /Users/will/.rvm/gems/ruby-1.9.3-p362)
(default)
global
=> ruby-1.9.3
[20130109_234313] will#localhost:~
$ cd Projects/rails_project
[20130109_234320] will#localhost:~/Projects/rails_project
$ rvm gemset list
gemsets for ruby-1.9.3-p362 (found in /Users/will/.rvm/gems/ruby-1.9.3-p362)
=> (default)
global
ruby-1.9.3
The Gemfile in the rails project directory will tell RVM to switch rubies if the Gemfile specifies a ruby version (something like ruby="1.9.3" or #ruby=1.9.3#gemset).
This is because RVM supports more then just .rvmrc => https://rvm.io/workflow/projects/#ruby-versions a full list can be found here: https://github.com/wayneeseguin/rvm/blob/master/scripts/functions/rvmrc#L743-L744
Note that RVM will check the following files before the Gemfile: .rvmrc .versions.conf .ruby-version .rbfu-version .rbenv-version. So creating a .rvmrc file with the line "rvm use ruby-1.9.3-p362#ruby-1.9.3" would make RVM ignore the Gemfile.
RVM has a feature called Project .rvmrc. In ~/Projects/rails_project there is a file called .rvmrc, which contains instructions for RVM for when you move into that project.
Edit: I missed the line about you not having that file. Sorry.
You can disable the project .rvmrc which disables project Gemfile checking as well. To do this, add the following line to /etc/rvmrc or ~/.rvmrc. Then you wont have to add a .rvmrc file to any project, as they will be ignored with the Gemfile. This was tested on rvm 1.19.6 (Stable).
rvm_project_rvmrc=0
Source: https://rvm.io/workflow/rvmrc
have two way at least to achieve this, eg:
echo 2.1.1 > .ruby-version and echo rails4 > .ruby-gemset
add first line #ruby=2.1.1#rails4 to Gemfile
why .ruby-gemset ?
.ruby-version also supports gemsets in the form of ruby#gemset but this is not compatible with other Ruby Versions Managers. Another file might be used to specify the gemset without breaking compatibility - .ruby-gemset.
I think above is the best practice .

Use my system's gems instead of an RVM gemset on a pre-bundler Rails app

I have several old (pre-bundler / pre-rvm) Rails projects that use my system's gems.
Now I've installed RVM to ride the latest Rails version, but my old applications are now using a gemset: (I'm not sure exactly what I did to make this happen)
~/rails_apps/rapgenius >: echo $GEM_HOME
/Users/tom/.rvm/gems/ruby-1.8.7-p302
I want to use my system's gems by default, and, if I have an .rvmrc file in a directory, I want to use the gemset it specifies in that directory. Like this:
~/rails_apps/reader2000 >: cat .rvmrc
rvm 1.9.2#reader2000
~/rails_apps/reader2000 >: echo $GEM_HOME
/Users/tom/.rvm/gems/ruby-1.9.2-p0
How can I achieve this?
You can try
rvm use system --default

How can I use multiple versions of rails in the same machine

I have installed rails 3.0.10 and 2.3.5 in my machine. I wanna shuffle between them but I am unable to use 2.3.5. When I run a command, the system recognizes only 3.0.10.
rvm lets you easily manage multiple installs of Ruby, each with their own list of gemsets.
Edit: Based on your comment about looking into gemsets, I'll point out one of the single coolest features with rvm. Once you get your gemset setup, create a .rvmrc file in your Rails root directory. Add the following to it:
rvm 1.9.2#foo
Where "1.9.2" is whatever Ruby you're using and "foo" is the gemset name. rvm will automatically start using this set when you cd in to that directory.
Definitely use rvm, create a .rvmrc file in the root directory of each of your projects.
For rails 3 stuff, it should contain a single line: rvm 1.9.2#projectName
Replacing projectName with an identifier for your project. Then use rvm gemset create projectName
Everytime you go into that directory, you'll be using that version of ruby with that particular gemset so you won't mix up versions or ruby or gems!

How do i specify that i want ruby1.9 in my Rails Projects?

Simply, how do you specify that you want ruby1.9 for your project ? And how do you check that it is indeed using it ? I have 1.8.7 and 1.9 in my system currently.
If you use RVM you can add .rvmrc file in project root directory with content:
rvm use 1.9.2-p180
After that when you go to a project directory with terminal, rvm will switch to specified ruby automatically:
$> cd ~/code/project
Using /home/voldy/.rvm/gems/ruby-1.9.2-p180

Resources