does using ":platforms =>" in your gemfile work?
Solution 1
:platforms => :ruby
does indeed exclude gems from being installed on Windows.
However, it does not work in a cygwin environment. In cygwin, it considers the platform to be :mri.
You'll also notice that ruby -e 'puts RUBY_PLATFORM'
outputs i386-cygwin
, not i386-mingw32
or i386-mswin
like it would on Windows ruby.
Were you working in a cygwin environment?
Solution 2
Add code to the Gemfile like this that excludes/includes gems depending on the OS platform
if RUBY_PLATFORM=~ /win32/
gem "windows-only-gem"
else
gem "os-agnostic-gem"
end
Solution 3
Bundler concept of platform differs from normal understanding of RUBY_PLATFORM
matching or RubyGems behaviors.
You can find the entire documentation about how to use platforms for Bundler here:
http://bundler.io/v1.14/man/gemfile.5.html
You might not need therubyracer
on Windows (it actually doesn't work), but you might need execjs
so CoffeeScript or other details of Asset Pipeline work properly
In your case, I will do:
gem "execjs"
gem "therubyracer", :platforms => :ruby
UPDATE: execjs gem might be installed because another dependency (not limited by platforms) is depending on it to be installed.
Solution 4
Rails 5:
if Gem.win_platform?
# Install gem for Windows
else
# Install another gem
end
Solution 5
I'm not sure about the :platform switch as I've never used it. However, an alternative that I think will work for your scenario would be to wrap your declarations for those two gems in a 'group' block in your Gemfile. Such as...
group :production do
gem 'therubyracer'
gem 'execjs'
end
This way, those gems will only be used in your production environment, not in development.
Note that I believe bundler will still install them in development (something to do with dependency checking), but they won't actually get loaded and therefore shouldn't cause problems.
jcollum
Updated on June 13, 2022Comments
-
jcollum almost 2 years
I have a Rails app that I'm developing on Windows and deploying to Linux. I suspect I'll just switch entirely over to Linux in the future. Anyway, on Linux I need 'execjs' and 'therubyracer' but I don't need those in Win7. So I put these lines in my gemfile:
gem 'therubyracer', :platforms => :ruby gem 'execjs', :platforms => :ruby
Ran a
bundle install
on the Linux VM and the app started up fine. But on Windows I get:Uncaught exception: Could not find execjs-1.2.11 in any of the sources
Now, from what I read (here under PLATFORMS) it tells me that "If a gem should only be used in a particular platform or set of platforms, you can specify them" and the sample is this:
gem "weakling", :platforms => :jruby
And it says "
ruby C Ruby (MRI) or Rubinius, but NOT Windows
". So to me that says that bundler should be ignoring theexecjs
line on Windows. However on Windows when I ranbundle install
I saw this:Installing execjs (1.2.11)
So that says to me I'm missing something about the docs or bundler is ignoring the platforms command. Am I doing something wrong?
PS>bundle -v Bundler version 1.0.21
-
jcollum over 12 yearsThis may work but doesn't actually answer my question. I didn't ask how can I work around it, I asked whether it should/does work.
-
Jon Garvin over 12 yearsI agree that it doesn't address the question directly, however it does address the underlying problem. It's not a "work around". It's an equally valid alternative that you may not have been aware of, and that you may (or may not) feel is appropriate for your specific situation.
-
jcollum over 12 yearsSo the docs are just wrong? That's what I'm really trying to get at.
-
Luis Lavena over 12 yearsYou can't use
if
blocks in your Gemfile. If you do, then Gemfile.lock will be regenerated and modified every time the project is accessed on different platforms. -
ffoeg over 12 yearsso what? Sounds like some dogma you should let go of. We dev on OSX and deploy in JRuby. We only check in Gemfile.lock that's generated in JRuby.
-
antinome over 10 years@ffoeg: I agree your approach works, but I think Luis Lavena's point is important for people who follow the recommended practice of checking your Gemfile.lock whenever you add a new gem or bundle update. And it's not a bad practice -- that way all developers have the exact same gem versions (except for the platform dependent ones, ideally).
-
Daniël W. Crompton over 10 yearsFor a linux or darwin env using the requires, as described here works well too stackoverflow.com/a/16475580/933358
-
Paul over 9 yearsThank you for
ruby -e 'puts RUBY_PLATFORM'