Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using bundler to load different versions of gems for different platforms

So I am developing a Sinatra for both windows and linux. The problem is I'm using Thin instead of Webrick and eventmachine for windows only works with a pre-release version while linux uses the latest stable. in the gemfile you of course cannot include the same gem with different versions like so:

gem "eventmachine", "~> 1.0.0.beta.4.1", :group => :development_win
gem "eventmachine", group => :development_linux
gem "thin

I was wondering if there was a way to work around this, maybe using one gemfile for windows and one gemfile for linux, what would the command be to load one or the other.

Alternatively is there a way to perhaps in git manage just the gemfile for two different platforms, perhaps through a branch for just the file (don't know if that's possible from what I've read of git branches).

like image 299
indigo0086 Avatar asked Jan 01 '12 21:01

indigo0086


People also ask

How do I install specific versions of gems?

Use `gem install -v` You may already be familiar with gem install , but if you add the -v flag, you can specify the version of the gem to install. Using -v you can specify an exact version or use version comparators.

How do I bundle a gem to a specific version?

The correct way to update the version of a gem to a specific version is to specify the version you want in your Gemfile, then run bundle install . As for why your command line was failing, there is no -version option.

What is the difference between gem install and bundle install?

Almost seems like running 'gem install' adds it to the global available gems (and hence terminal can run the package's commands), whereas adding it to the gemfile and running bundle install only adds it to the application. Similar to npm install --global. that's basically it.


2 Answers

You can do it like that:

# Windows
gem "eventmachine", "~> 1.0.0.beta.4.1", :platform => [:mswin, :mingw]

# C Ruby (MRI) or Rubinius, but NOT Windows
gem "eventmachine", :platform => :ruby

Full list of available platforms:

ruby      C Ruby (MRI) or Rubinius, but NOT Windows
ruby_18   ruby AND version 1.8
ruby_19   ruby AND version 1.9
ruby_20   ruby AND version 2.0
mri       Same as ruby, but not Rubinius
mri_18    mri AND version 1.8
mri_19    mri AND version 1.9
mri_20    mri AND version 2.0
rbx       Same as ruby, but only Rubinius (not MRI)
jruby     JRuby
mswin     Windows
mingw     Windows 'mingw32' platform (aka RubyInstaller)
mingw_18  mingw AND version 1.8
mingw_19  mingw AND version 1.9
mingw_20  mingw AND version 2.0 

You can find more information in Gemfile(5) man page here (see 'Platforms' section).

Another approach is to use RUBY_PLATFORM constant:

if RUBY_PLATFORM =~ /win32/
  gem "eventmachine", "~> 1.0.0.beta.4.1"
else
  gem "eventmachine"
end

I haven't seen full list of available values for RUBY_PLATFORM but you can run

ruby -e 'puts RUBY_PLATFORM'

on both your platforms and see the difference.

like image 200
KL-7 Avatar answered Sep 29 '22 14:09

KL-7


You can use the --gemfile option to use different gemfiles for different platforms. See the documentation here http://gembundler.com/man/bundle-config.1.html

like image 39
randomuser Avatar answered Sep 29 '22 15:09

randomuser