warning from ruby version file - ruby

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

Related

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.
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.

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

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.

Multiple Rails apps and Ruby versions with rbenv

I have multiple Rails apps from 3.2.14 to 3.2.18 in my development environment. I'm using Ruby 1.9.3 (yes, I know) for these apps and it's installed and managed via rbenv. I'm about to build a couple of new apps with Rails 4.1 and Ruby 2.1. My question is, what is the best way to handle installing multiple versions of Ruby using rbenv. Currently 1.9.3 is set to global, but I want different Rails apps (4.1) to use Ruby 2.1.
I guess I'm a bit behind the curve with Ruby management so I could use a hand here. Again, looking to have multiple Rails apps (different versions 3.2.14-4.1.x) and multiple Ruby instances to support each app.
I appreciate the help in advance.
Create a .ruby-version file in your application root directory and specify the ruby version you want. For instance, if you want one application to use 1.9.3 and the other 2.1.0, you'll have a .ruby-version file each in those two applications, specifying the version:
/application1/.ruby-version
1.9.3
/application2/.ruby-version
2.1.0
Update
For every installed version of ruby, you'll have a shims directory which will house all your gems for that particular version. When you switch between ruby versions, rbenv will simply set the environment variables to the one you select.
In the example above, I set the local ruby version for the directory ruby_cookbook to 2.1.2 and a gem list will only show gems installed for 2.1.2.
I also created a .ruby-version with ruby version 1.9.3-p547 in my try_stuff directory. As you can see, gem list only shows gems installed for 1.9.3.
The gems you install are specific to the current version of ruby.
If your project root has a .ruby-version file, your environment will automatically set the current ruby version to the one specified. So, yes, your production environment will need the version you mention in your .ruby-version. If you use git for source control, you can add that file to your .git-ignore and you won't see it in your prod version.

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/

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