6

I have two scripts which require the same version of Ruby. However, each script also requires a DIFFERENT version of a gem (nokogiri). One of the scripts will run with both versions of nokogiri (1.6.2.1 and 1.6.1). However the other script will ONLY run with version 1.6.1; and if 1.6.2.1 is installed, the script will not execute normally.

I know how to install multiple versions of Ruby with rbenv. But is it possible to install multiple instances of the SAME version of Ruby (2.1.2)? If so, how?

tSquirrel
  • 63
  • 3

5 Answers5

7

Make a local copy of a ruby-installer definition file using a custom name.

$ cp ~/.rbenv/plugins/ruby-build/share/ruby-build/2.1.5 2.1.5-nokogiri161

Install this as a custom definition file, no edits required.

$ rbenv install ./2.1.5-nokogiri161

Now you have a ruby version with a custom name and you can install custom gems on it.

$ rbenv shell 2.1.5-nokogiri161
$ gem install nokogiri -v 1.6.1

This is also useful for installing ruby versions with custom build flags. For example, a debug build with no compiler optimizations.

$ cp ~/.rbenv/plugins/ruby-build/share/ruby-build/2.1.5 2.1.5-debug
$ RUBY_CONFIGURE_OPTS="optflags=-O0" rbenv install ./2.1.5-debug
$ rbenv shell 2.1.5-debug
ender672
  • 413
  • 4
  • 7
3

1) Use rvm instead of rbenv, and using it feature called 'gemsets' you could use different versions of gems for one version of ruby

rvm 2.1.1
rvm gemset create first second
rvm 2.1.1@first
gem install nokogiri -v 1.6.1
rvm 2.1.1@second
gem install nokogiri -v 1.6.2

2) or you can use bundler

gem install bundler
/dir_1/Gemfile
source 'http://rubygems.org'
gem 'nokogiri', '1.6.1'

/dir_2/Gemfile
source 'http://rubygems.org'
gem 'nokogiri', '1.6.2'
edikgat
  • 861
  • 8
  • 9
2

I see two options:

  1. You could have different Gemfiles and let bundler take care of setting the right gem-version. If the scripts are in different directories, it should be no problem.

  2. You could use rbenv-gemset to have separate gem-environments

There are undoubtly more, but those two seem the easiest for your setup.

kronn
  • 925
  • 11
  • 31
2

Multiple instances of the same ruby is possible (regardless of the tool installing them) by just compiling them manually, with a different --prefix configured.

  • download the source
  • extract it
  • ./configure --prefix=~/.rbenv/version/2.1.2-tSquirrel
  • make
  • sudo make install

This is how the rbenv-docs propose to achieve this.

You could also rename the current "2.1.2"-directory and then use ruby-build to install 2.1.2 again.

kronn
  • 925
  • 11
  • 31
1

"Versions" in rbenv are just directories in ~/.rbenv/versions, I would guess you cold just rename/copy installed version and when you "select" version with "rbenv shell" you just declare from which directory you want to use binaries.

MBO
  • 30,379
  • 5
  • 50
  • 52