rbenv: multiple instances of SAME ruby - ruby

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?

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

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'

I see two options:
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.
You could use rbenv-gemset to have separate gem-environments
There are undoubtly more, but those two seem the easiest for your setup.

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.

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

Related

Using a particular Ruby version on a project

I am trying to use a specific version of ruby on a particular project. I looked into the RVM and rbenv packages, but had little success with any of them.
These are the steps I took:
create project directory and navigate to it;
run rbenv install 2.3.1; after navigating to ~/.rbenv/versions I can see a "2.3.1" in there;
running "rbenv local" also echos 2.3.1;
My Gemfile is as follows:
ruby '2.3.1'
source 'https://rubygems.org/' do
gem 'test-kitchen'
gem 'kitchen-terraform'
end
However, when I run bundle install i get the following error:
Your Ruby version is 2.5.1, but your Gemfile specified 2.3.1
Its my first time working with Ruby, so I am struggling a little. I appreciate all help, and can provide more details if required.
If you store the ruby version of your project in .ruby-version and the gemset (i.e. gem list workspace) in .ruby-gemset of your project (which I recommend doing if the specific version is important to you), rvm should pick this up when you cd into your project directory. Sometimes it doesn't however depending on your setup or installation, in which case you can choose them explicitly with:
rvm use `cat .ruby-version`
rvm gemset use `cat .ruby-gemset
which I add to any installation scripts for example. You may need to run gem install bundler before running bundle install the first time you use the new gemset.
Set your default rbenv version like this:
$ rbenv shell jruby-1.7.1
See in: https://github.com/rbenv/rbenv#rbenv-shell

Specify Path for Installing ruby gem

I have two versions of Ruby installed.
How can I install my ruby gem so that it gets installed in the version specified.
Normally you need to specify which Ruby you're using first, then call the appropriate gem command once that's set.
RVM, rbenv and others all work in different ways. For example, with RVM:
rvm use 2.4.1
gem install xyz -v 1.2.3
For rbenv it's a bit different:
rbenv local 2.4.1
gem install xyz -v 1.2.3
Where that generates a .ruby-version file in your current directory.
Since the gem command itself is Ruby, the proper Ruby environment must be set, which is where the multi-Ruby version managers come in.

Bundler with rbenv version pathfile issue

So I am using rbenv to set my ruby version (for the specific project I'm working on this is 2.1.1). The issue is that bundler is unable to detect this change. I even tried to set the version in my gemfile:
source "https://my-proxy-address"
ruby "2.1.1"
gem 'fileutils'
gem 'json'
gem 'chef-api'
However this then causes the exact error message as seen here at the end of the tutorial:
username#hostname:~/Desktop/working-bundler-env$ rbenv version
2.1.1 (set by /Users/username/.rbenv/version)
username#hostname:~/Desktop/working-bundler-env$ ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin15.0]
username#hostname:~/Desktop/working-bundler-env$ bundle install
Your Ruby version is 2.0.0, but your Gemfile specified 2.1.1
The tutorial has the solution to the issue (edit a pathfile), however they dont say what file to change. What file do I change?
$ rbenv
rbenv 1.0.0
Usage: rbenv <command> [<args>]
Some useful rbenv commands are:
commands List all available rbenv commands
local Set or show the local application-specific Ruby version
global Set or show the global Ruby version
shell Set or show the shell-specific Ruby version
install Install a Ruby version using ruby-build
uninstall Uninstall a specific Ruby version
rehash Rehash rbenv shims (run this after installing executables)
version Show the current Ruby version and its origin
versions List all Ruby versions available to rbenv
which Display the full path to an executable
whence List all Ruby versions that contain the given executable
See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme
$ which bundle
/usr/local/bin/bundle
So I noticed that when I ran gem bundle install the version was 1.13.3. After running sudo find / -name bundle I found two file locations that included that version number.
/Users/myusername/.gems/gems/bundler-1.13.3/exe/bundle
/Users/myusername/.gems/gems/bundler-1.13.3/lib/bundler/man/bundle
After attempting to run /Users/myusername/.gems/gems/bundler-1.13.3/exe/bundle install it works perfectly. So I just made an alias in my ~/.bash_profile that overwrote the incorrect bundle command.
alias bundle=/Users/alexcohen/.gems/gems/bundler-1.13.3/exe/bundle
The only side effect of this gem (for better or worse) is that it creates .bundle and path directories in the directory where I run bundle install where the gems are downloaded into.
I still have to investigate why this is happening, but I think that the bundle command in my macs terminal was referencing some type of broken bundler gem or file somewhere in my system.

How to switch cocoapods (or other gem) version on command line?

There are some answers saying using gem 'cocoapods', '0.33.1' or gem 'pod', '0.33.1
but this does not work.
ERROR: While executing gem ... (Gem::CommandLineError)
Unknown command cocoapods,
In one project I need to use 0.33.1, but 0.34.4 in another. How do I switch the cocoapods version quickly on command line? I don't want to use gem install or uninstall.
I am using rbenv with ruby version 2.0.0p0. Keep using rbenv is preferred but I can switch to rvm or pure ruby only if there is no other options.
Thanks.
You can do the following to use different versions:
pod _0.34.4_ install
or
pod _0.34.1_ install
This is precisely the purpose of Bundler
In one project, you specify this in the Gemfile:
gem 'cocoapods', '0.33.1'
along with all other gems.. and run bundle install to install them.
There are some other steps to make sure you load those gems from bundler, I'd read that site for the full documentation.
Rails projects have this already set up for you, but if this is a plain old ruby project, you have to wire it up yourself.
FYI I am using the following temp solution.
Since I am using rbenv, I install different versions of cocoapods under different version of ruby.
For example,
rbenv global 2.1.0
gem install cocoapods -v 0.34.4
rbenv global 2.0.0-p0
gem install cocoapods -v 0.33.1
When I work on different Xcode projects that requires different version of Cocoapods, I just switch ruby version on the fly, and Cocoapods version is switched accordingly.
As I mentioned these are xCode projects and we are not allowed to add gemfile or install gem in the project folder. So this works for me.

Bundler::GemfileNotFound whenever I try to run bundle install

I'm working in kali-linux (a linux distro witch is the continuation of Back-Track, based in Ubuntu, just that now is based on Debian wheezy) for some penetration testing. Everything was working just fine, until I decided to update my systems tools. Now whenever I try to run a tool based on ruby, it trows me:
Could not find gem 'ruby-progressbar (>= 1.1.0) ruby' in the gems available on this machine.
Run `bundle install` to install missing gems.
I proceed to run bundle install but then it comes with Bundler::GemfileNotFound error.
Kali use by default ruby, for using gems. The software don't 'require' any other package but ruby seems not-fully-configured/installed for the problem at hand.
$ ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [i486-linux]
$ rvm
bash: rvm: command not found
$ rbenv
bash: rbenv: command not found
I faced the same problem when I was trying to bundle install without realizing that I was not in the project directory. Make sure you're in the current project directory.
to avoid this error you should be at the root of your application and create GemFile and specify all gems needed in there, and run bundle install
The problem was that for some weird motive Ruby didn't detected that bundler was installed, although the package manager says so.
$ apt-cache policy bundler
bundler:
Installed: 1.3.5-2
Candidate: 1.3.5-2
$ bundle --version
Bundler::GemfileNotFound
I simply run gem install bundler then bundler install as root in the tool root path and everything works as charm.
Do you use rvm or rbenv? If so, make sure you are using a particular ruby version.
For rvm, rvm list and look for an indication next to your ruby version. If the correct one is not listed, run rvm install x.y.z. If the correct one is not selected, run rvm use x.y.z
If you want to segregate your gems for a given project, create a gemset. Otherwise, you should be good to go.
Run gem install bundler. You should not have to do this as sudo. This will install bundler in either the Default rvm gemset, or the selected gemset.
Bundler should now be available and can be run using bundle. This is the same as bundle install.
Try this:
sudo ln -s /var/lib/gems/1.8/bin/bundle /bin/bundle
sudo ln -s /var/lib/gems/1.8/bin/bundler /bin/bundler
Worked for me in debian.

Resources