Set a default Ruby version for a specific directory (RVM) - ruby

How can I use RVM to set a default ruby version for a certain directory? So that every time I cd into that directory, it switches to my preferred version of Ruby.

Directly from the RVM documentation:
RVM supports multiple files allowing to configure a project for automated ruby switching. In any case make sure to add those files to your version control systems as it is part of the project configuration.
Listed in order of precedence:
.rvmrc - shell script allowing full customization of the environment,
.versions.conf - key=value configuration file
.ruby-version - single line ruby-version only
Gemfile" - comment: #ruby=1.9.3 and directive: ruby "1.9.3"

One way is to use a Gemfile and set the ruby version in it. like so:
ruby '2.2.0'
then when you enter the directory you will see the following message from rvm
RVM used your Gemfile for selecting Ruby, it is all fine - Heroku does that too,
you can ignore these warnings with 'rvm rvmrc warning ignore /Users/danmanstx/rails_projects/app/Gemfile'.
To ignore the warning for all files run 'rvm rvmrc warning ignore allGemfiles'.

Create a .ruby-version file in that directory with your version information. To set version as 2.1.2 for a directory, create the file with only "2.1.2" as the content.
$ cat .ruby-version
2.1.2

in rails 6 you only have to change what's inside of .ruby-version in your app folder

Or simply use rvm --ruby-version ruby_version#gemset --create , --create here will create the gemset if it is not present already. If you don't need to specify a gemset, but instead use the default gemset. leave out #gemset --create. ruby_version for example 2.0.0.

Related

warning from ruby version file

I cloned a project and it had a .ruby-version file to specify ruby version of the project. content:
2.0.0
when I switched into the project dir for the first time it gave me warning:
ruby-2.0.0-p645 is not installed. To install do: 'rvm install
ruby-2.0.0-p645'
However it's true that my system didn't have Ruby 2.0 but Ruby 2.2 instead. Does that warning makes sense? Is there a way I use duplicate functionality of (~>) gem dependency in ruby-version file?
Based on the warning, you are using rvm. As another answer says, rbenv can also use a .ruby-version file, but switching to rbenv isn't neccesary, and won't really change anything.
I don't think rvm's .ruby-version file supports a range of versions. It needs to specify a version name that could be passed to rvm use, and I don't think there's any way to say a range of versions there.
You can:
Install ruby 2.0.0 with rvm: rvm install 2.0.0. When you switch into the project directory, rvm will automatically switch you to using ruby 2.0.0 like the .ruby-version file directs.
Simply change your .ruby-version file to specify the version of ruby you want to use instead. Just edit it in a text editor, or run rvm --ruby-version use 2.2.3 to have it write the .ruby-version file for you. Now when you switch into the project directory, rvm will automatically switch you to 2.2.3.
Delete the .ruby-version file entirely. You aren't required to use it. Personally, I don't use them and don't find them helpful. The .ruby-version file is meant to force a particular version of ruby to be used in that project -- but most projects work with multiple versions of ruby, and this isn't really neccesary, and as you've seen can be a headache when it's out of date and is trying to force you to use an older version of ruby, when a newer one would probably work fine.
.ruby_version is used for rbenv
You should install rbenv
or create .rvmrc for rvm

Should RVM automatically change gemsets based on .ruby-gemset?

I updated RVM and started switching from .rvmrc to .ruby-gemset and, where necessary, .ruby-version.
However, so far, when I change into a directory with .ruby-gemset, it doesn't automatically start using the correct one as it would with .rvmrc.
Is it supposed to work the same way?
I'm using RVM 1.20.5.
RVM detects .ruby-version and only if it is present it will read .ruby-gemset.
RVM does not reads .ruby-gemset when .ruby-version is not available.
You could try .ruby-version (editted from current):
default

Understanding and using project specific .rvmrc rvm config files

Just getting started with Ruby.
I am trying to use rvm. Now, for Project A I am trying to specify a specify Ruby version and a gemset.
$ cat projecta/.rvmrc
rvm 1.8.7#projecta
My understanding is that the part before # specifies the Ruby version and the the part after # specifies the gemset name. A gemset IMU is to provide a project specific isolated location where you can install gems.
After I check-in this project, what can I do to automate the process of creating the gemset and installing the correct Ruby version for someone else checking the project out at a later date?
Please suggest appropriate alternatives, since I am just getting started with Ruby today.
The Old Way
to make sure gemset / ruby is available use this .rvmrc:
rvm use 1.8.7#projecta --install --create
It will install ruby if missing and create gemset if missing.
And a special note, please do not use 1.8.7, it's deprecated ruby, with almost no support (security patches till half of 2013), you should stick with latest available ruby:
rvm use ruby
which at this time is: 1.9.3-p194
Add On-Demand Syntax
Your syntax won't work as written. If you want to force people to compile rubies and create gemsets on demand, rather than being warned when things don't exist, you want a project .rvmrc file like this:
# Compile rubies on demand.
rvm_install_on_use_flag=1
# Create gemsets on demand.
rvm_gemset_create_on_use_flag=1
# Use ruby-1.8.7 while in project tree.
rvm use 1.8.7
# Use gemset "projecta" while in project tree.
rvm gemset use projecta
There are certainly other ways to do it, but this way makes everything explicit, and you can comment out individual lines if you need to do so.
See Also
https://rvm.io/workflow/rvmrc/

rvm doesn't notify when using a new gemset

I'm using rvmrc files per project. When I cd into my project the terminal/rvm does not indicate that I'm using a new gemset. How do I set it up?
In case it helps: I'm using oh-my-zsh.
it might depend on your project files, basically rvm will not print information about using when the use keyword is omitted:
rvm 1.9.3 # will be quiet
rvm use 1.9.3 # will print: Using...
the same result will be when you put those in .rvmrc, also generating new .rvmrc with --rvmrc will behave the same way:
rvm 1.9.3 --rvmrc # `cd` will be quiet
rvm use 1.9.3 --rvmrc # `cd` will print: Using...

custom_require.rb:36:in `require': no such file to load -- myapp(LoadError)

i get the above error, all i did was install rvm and update to ruby 1.9.2 from the default ruby that you get with mac.
all my gems appear in the gem list but do i need to specify a path somewhere?
cheers,
glenno.
Check that you have the gems installed gem list If not, install as usual gem install whatever or if you want a specific version gem install whatever -v 1.2.3 Or if you have bundler in your project (ie Rails), it's just bundle install
If you have all the gems, then this is probably a local file, and you're probably coming from Ruby version < 1.9, you need to either fix your $LOAD_PATH (which will depend on too many factors for me to explain here), or specify the full path to the file you want File.dirname(__FILE__) + '/myapp'
Otherwise, you need to give more info, like what version you used to be running, what myapp is, and where it is in relation to your file.
In my case it was rvm issue, I was using rvm 1.9 when i changed it to 1.8, migration worked.
rvm use 1.8
In my case I was converting to Cygwin64. The Cygwin directory name changed and Ruby upgraded to 1.9. After I fixed the Cygwin directory name, the second solution worked. Or at least got past this problem:
require File.dirname(FILE) + '/myapp.rb'

Resources