Dynamic Gemfile Ruby version for both rbenv and RVM - ruby

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.

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

Ruby, Versions, Gems, Versions and more, Oh My

Apologies for the topic title, couldn't resist.
Basically, For no reason whatsoever apart from trying out different things. I want to implode RVM and try out rbenv. Nothing against RVM, just want to try different peoples software and try different things out.
I can get rid of RVM and install rbenv no problem. Could you guys confirm a few things for me.
Firstly, if I do the above, and then install a few different ruby versions. I know I can place a .ruby-version file in a projects root directory to specify a specific ruby version but then when I say gem install <gemname> I take it that is installed globally for that ruby version.
If I then use bundler per project to manage what gems are used and I specify a gem version, what happens if the version specified in the lock file is different to the currently installed version and what happens if it's vice versa?
I just need a bit of clarification on what's doing what in regards to ruby, versions, gems and their versions.
One of the (many) lovely things about rbenv is that you can so easily look right at your ruby installation to see what's going on. For example, on my machine I can open ~/.rbenv and there's versions containing my ruby installations, such as 1.9.3-p327. Opening that, I can keep drilling down to see my gems installed for that version of ruby (in ~/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems). It is then trivial to run gem list or gem install somegem and confirm that what's being affected is this very collection of gems - if this is the current ruby (set in rbenv global and starred in rbenv versions).
In short, everything about your system thinks that ruby means this version of ruby and that its library is this library.
For Bundler, you'll need to install rbenv-bundler. A nice tutorial (easily found with Google) is here: http://dan.carley.co/blog/2012/02/07/rbenv-and-bundler/

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/

Can RVM find versions of ruby it did not install?

I'm running os x lion. A version of ruby was installed when I installed the OS. I then installed RVM and then installed ruby 1.9.2 and made it the default for new shells.
Default Ruby (for new shells)
ruby-1.9.2-p290 [ x86_64 ]
What happend to the older version of Ruby that was already on my system and how can I remove it since RVM doesn't see it?
When I try to use the new hash syntax in code written in textmate or sublime text 2 it fails. When the same code is run in the shell it works. So how do I make ruby 1.9.2 the only ruby installed on my system?
You do NOT want to uninstall the system Ruby. It is required for a lot of applications (including RVM) to function properly.
If you want applications like Textmate or Sublime Text to use a different version of Ruby, you can update their individual configuration settings to set the path to the Ruby binary you wish to use.
Since those apps don't run ruby from the shell like you do they don't work seamlessly with RVM. Fortunately RVM's web site has instructions on integrating RVM with TextMate. You shouldn't have trouble applying the same process for Sublime Text 2 but if you do the web does not want for tutorials on the topic.
Also, RVM is not responsible for any ruby it didn't install. the only other ruby it gives access to is the system ruby, and there it doesn't do gemsets etc. (This is also due to systems like debian that specifically block off things like gem update --system so you don't poison the package that was installed. RVM protects from that as well)

Using RVM, how to ensure gems and non-gems see the same version of ruby?

I'm trying to install bdb on Ubuntu as a gem as follows,
gem install bdb
I'm not using sudo, since I am installing with rvm. The ruby version on my system is 1.9.1 but the version I'm using for this installation is 1.8.7 (I'm installing rbot, you see). The problem I've encountered is:
checking for db_version() in -ldb-4.something... no
I assume this means that rubygems is not finding ldb4.something in the places it is looking. Is this because I installed libdb-4.something (7, actually) using sudo, and my system's ruby version is different from the ruby version I'm using for bdb? Using rvm, how should I manage this sort of situation? Maybe I'm doing things wrong, but it seems to me that my problem is that not everything I'm using is a gem, and the stuff that isn't gems is seeing the wrong version of ruby.
(if it turns out that my problem is something completely unrelated: I am still interested in the question. How do things I've installed on my system with a particular version of ruby get along with gems installed using rvm under different versions of ruby?)
"How do things I've installed on my system with a particular version of ruby get along with gems installed using RVM under different versions of ruby?"
They don't, and that is the point. RVM is for managing different versions of Ruby so that they are isolated from each other.
This means that the Ruby versions that you use for system (when you do sudo gem install that would be installing to system) have nothing to do with the current environment (when properly set up.)
When you are in a RVM environment, each version of Ruby installed is an isolated environment.
What I would recommend is this:
Go to the RVM Website and check it out.
At the end of the page, continue with your reading about how to use gemsets.
go to your project directory
rvm use 1.8.7#your_project_environment
create your .rvrmc file so that you will automatically be using this environment in your project on the future visits to this project, this file can contain "rvm use 1.8.7#your_project_environment" which will achieve this.
install all required gems
If you have additional questions, feel free to visit Wayne E. Seguin and a ton of other users of RVM and we will be happy to help you out. Of course, the first search for answers is the documentation on the web site.

Resources