# How to build gem whose dependencies depend of the platform?

**URL:** <https://rubytalk.org/t/how-to-build-gem-whose-dependencies-depend-of-the-platform/61603>\
**Category:** ruby-talk\
**Created:** [26 January 2011 22:30 UTC](https://rubytalk.org/t/how-to-build-gem-whose-dependencies-depend-of-the-platform/61603 "2011-01-26T22:30:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jarmo\_Pertman](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@Jarmo\_Pertman](https://rubytalk.org/u/Jarmo_Pertman)\
**Post date:** [26 January 2011 22:30 UTC](https://rubytalk.org/t/how-to-build-gem-whose-dependencies-depend-of-the-platform/61603/1 "2011-01-26T22:30:53Z")

</div>

Hi!

I need to build a gem whose dependencies are dependent of the platform  
itself. For example when `gem install my_gem` is issued under Windows,  
then in gemspec i have to have spec.add\_dependency("gemX"), but if the  
command is executed under linux then i need to have  
spec.add\_dependency("gemY").

Is there any easy way to do this or should i just do it like this  
instead:  
1) remove all platform specific dependencies from gemspec  
2) when loading the gem, then use RUBY\_PLATFORM to decide what gems to  
load and rescue Gem::LoadError to print out friendly messages like  
"please install gemX"

Jarmo

---

<div class="post-metadata">

**Author:** ![Luis\_Lavena1](https://avatars.discourse-cdn.com/v4/letter/l/f08c70/32.png) [@Luis\_Lavena1](https://rubytalk.org/u/Luis_Lavena1)\
**Post date:** [26 January 2011 22:45 UTC](https://rubytalk.org/t/how-to-build-gem-whose-dependencies-depend-of-the-platform/61603/2 "2011-01-26T22:45:22Z")

</div>

This one is the best approach. Have that clearly in the documentation  
and the error you present to the user is the best option.

The other will be generate one gem for every platform and indicate  
there the dependencies. Lot of work for you 😛

> **···**
>
> On Jan 26, 7:25 pm, Jarmo Pertman \<jarm...@gmail.com\> wrote:
> 
> > Hi!
> > 
> > I need to build a gem whose dependencies are dependent of the platform  
> > itself. For example when `gem install my_gem` is issued under Windows,  
> > then in gemspec i have to have spec.add\_dependency("gemX"), but if the  
> > command is executed under linux then i need to have  
> > spec.add\_dependency("gemY").
> > 
> > Is there any easy way to do this or should i just do it like this  
> > instead:  
> > 1) remove all platform specific dependencies from gemspec  
> > 2) when loading the gem, then use RUBY\_PLATFORM to decide what gems to  
> > load and rescue Gem::LoadError to print out friendly messages like  
> > "please install gemX"
> 
> --  
> Luis Lavena

---

<div class="post-metadata">

**Author:** ![Jarmo\_Pertman](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@Jarmo\_Pertman](https://rubytalk.org/u/Jarmo_Pertman)\
**Post date:** [29 January 2011 11:21 UTC](https://rubytalk.org/t/how-to-build-gem-whose-dependencies-depend-of-the-platform/61603/3 "2011-01-29T11:21:07Z")

</div>

Ok, but this is not best solution also since user:  
1) gem install mylib  
2) create a script and `require "mylib"`  
3) run that script  
4) get an error about missing dependency  
5) nevertheless of just running gem install, user needs to run gem  
install again

It just feels not user friendly way to solve it. Another way would be  
to create separate gems of course, but it's not a good way either.

Jarmo Pertman

> **···**
>
> -----  
> IT does really matter - [http://www.itreallymatters.net](http://www.itreallymatters.net)
> 
> On Jan 27, 12:40 am, Luis Lavena \<luislav...@gmail.com\> wrote:
> 
> > On Jan 26, 7:25 pm, Jarmo Pertman \<jarm...@gmail.com\> wrote:
> > 
> > \> Hi!
> > 
> > \> I need to build a gem whose dependencies are dependent of the platform  
> > \> itself. For example when `gem install my_gem` is issued under Windows,  
> > \> then in gemspec i have to have spec.add\_dependency("gemX"), but if the  
> > \> command is executed under linux then i need to have  
> > \> spec.add\_dependency("gemY").
> > 
> > \> Is there any easy way to do this or should i just do it like this  
> > \> instead:  
> > \> 1) remove all platform specific dependencies from gemspec  
> > \> 2) when loading the gem, then use RUBY\_PLATFORM to decide what gems to  
> > \> load and rescue Gem::LoadError to print out friendly messages like  
> > \> "please install gemX"
> > 
> > This one is the best approach. Have that clearly in the documentation  
> > and the error you present to the user is the best option.
> > 
> > The other will be generate one gem for every platform and indicate  
> > there the dependencies. Lot of work for you 😛
> > 
> > --  
> > Luis Lavena

---

<div class="post-metadata">

**Author:** ![Uwe\_Kubosch1](https://avatars.discourse-cdn.com/v4/letter/u/53a042/32.png) [@Uwe\_Kubosch1](https://rubytalk.org/u/Uwe_Kubosch1)\
**Post date:** [29 January 2011 12:55 UTC](https://rubytalk.org/t/how-to-build-gem-whose-dependencies-depend-of-the-platform/61603/4 "2011-01-29T12:55:19Z")

</div>

I have solved this by making the gem depend on the "bundler" gem, and then put the gem dependecies into a Gemfile, and run the equivalent of "bundle install" as a post-install script. This takes care of everything. The only thing I am missing is that the dependencies are visible when inspecting the gem.

Having RubyGems drop the gemspec dependencies in favor of Gemfile dependencies would be my preferred way.

> **···**
>
> On 2011-01-29, at 12:21, Jarmo Pertman wrote:
> 
> > > > I need to build a gem whose dependencies are dependent of the platform  
> > > > itself. For example when `gem install my_gem` is issued under Windows,  
> > > > then in gemspec i have to have spec.add\_dependency("gemX"), but if the  
> > > > command is executed under linux then i need to have  
> > > > spec.add\_dependency("gemY").
> 
> --  
> With kind regards  
> Uwe Kubosch  
> Kubosch Consulting  
> uwe@kubosch.no
> 
> > **[kubosch.no - Welcome to Kubosch Consulting](http://www.kubosch.no/)**
> >
> > kubosch.no - bringing martial arts and information technology together.

---

<div class="post-metadata">

**Author:** ![Luis\_Lavena1](https://avatars.discourse-cdn.com/v4/letter/l/f08c70/32.png) [@Luis\_Lavena1](https://rubytalk.org/u/Luis_Lavena1)\
**Post date:** [29 January 2011 18:00 UTC](https://rubytalk.org/t/how-to-build-gem-whose-dependencies-depend-of-the-platform/61603/5 "2011-01-29T18:00:24Z")

</div>

You can display a post\_install message after you installed "mylib"  
that tells:

mylib depends on platform specific components, please install "mylib-  
windows" before you start using it.

And still rescue on the LoadError.

The other way around is what I mentioned, but then you will need to  
generate one gem for every RubyGems platform: linux, darwin, freebsd,  
mingw32, mswin32, etc and indicate the proper dependencies.

Even if the gem itself is not different.

I feel that is more work to do, don't you think?

> **···**
>
> On Jan 29, 8:16 am, Jarmo Pertman \<jarm...@gmail.com\> wrote:
> 
> > Ok, but this is not best solution also since user:  
> > 1) gem install mylib  
> > 2) create a script and `require "mylib"`  
> > 3) run that script  
> > 4) get an error about missing dependency  
> > 5) nevertheless of just running gem install, user needs to run gem  
> > install again
> > 
> > It just feels not user friendly way to solve it. Another way would be  
> > to create separate gems of course, but it's not a good way either.
> 
> --  
> Luis Lavena

---

<div class="post-metadata">

**Author:** ![Luis\_Lavena1](https://avatars.discourse-cdn.com/v4/letter/l/f08c70/32.png) [@Luis\_Lavena1](https://rubytalk.org/u/Luis_Lavena1)\
**Post date:** [29 January 2011 17:55 UTC](https://rubytalk.org/t/how-to-build-gem-whose-dependencies-depend-of-the-platform/61603/6 "2011-01-29T17:55:22Z")

</div>

RubyGems post\_install is not scripts, perhaps you're using extension  
compilation to make that happen, which is intrusive to the user.

You're also bringing Bundler when the user might not want it into the  
mix and slowing down the gem loading process.

Bundler is awesome for development, but is awful for runtime  
dependency. You should not be imposing bundler to the user and even  
less for runtime execution.

Also bundler do not handle properly all the platforms, using a  
different concept of "platform" of what RubyGems understand and is  
widely known.

> **···**
>
> On Jan 29, 9:55 am, Uwe Kubosch \<u...@kubosch.no\> wrote:
> 
> > On 2011-01-29, at 12:21, Jarmo Pertman wrote:
> > 
> > \>\>\> I need to build a gem whose dependencies are dependent of the platform  
> > \>\>\> itself. For example when `gem install my_gem` is issued under Windows,  
> > \>\>\> then in gemspec i have to have spec.add\_dependency("gemX"), but if the  
> > \>\>\> command is executed under linux then i need to have  
> > \>\>\> spec.add\_dependency("gemY").
> > 
> > I have solved this by making the gem depend on the "bundler" gem, and then put the gem dependecies into a Gemfile, and run the equivalent of "bundle install" as a post-install script. This takes care of everything. The only thing I am missing is that the dependencies are visible when inspecting the gem.
> 
> --  
> Luis Lavena

---

<div class="post-metadata">

**Author:** ![Uwe\_Kubosch1](https://avatars.discourse-cdn.com/v4/letter/u/53a042/32.png) [@Uwe\_Kubosch1](https://rubytalk.org/u/Uwe_Kubosch1)\
**Post date:** [30 January 2011 11:06 UTC](https://rubytalk.org/t/how-to-build-gem-whose-dependencies-depend-of-the-platform/61603/7 "2011-01-30T11:06:38Z")

</div>

> > > > > I need to build a gem whose dependencies are dependent of the platform  
> > > > > itself. For example when `gem install my_gem` is issued under Windows,  
> > > > > then in gemspec i have to have spec.add\_dependency("gemX"), but if the  
> > > > > command is executed under linux then i need to have  
> > > > > spec.add\_dependency("gemY").
> > 
> > I have solved this by making the gem depend on the "bundler" gem, and then put the gem dependecies into a Gemfile, and run the equivalent of "bundle install" as a post-install script. This takes care of everything. The only thing I am missing is that the dependencies are visible when inspecting the gem.
> 
> RubyGems post\_install is not scripts, perhaps you're using extension  
> compilation to make that happen, which is intrusive to the user.

You are right, I use the extension compilation to run the extra install step.

How is this intrusive?

> You're also bringing Bundler when the user might not want it into the  
> mix and slowing down the gem loading process.

I would rather add Bundler than not have the gem install all its dependencies. The user experience is what matters, and this way all dependencies install correctly and the gem is ready for use regardless of the users platform.

> Bundler is awesome for development, but is awful for runtime  
> dependency. You should not be imposing bundler to the user and even  
> less for runtime execution.

Why not? Bundler is awesome for runtime dependencies as well. It is the default mechanism for Ruby On Rails runtime dependencies.

> Also bundler do not handle properly all the platforms, using a  
> different concept of "platform" of what RubyGems understand and is  
> widely known.

The platform concept of Bundler is different from RubyGems platform, but Bundler handles all platforms properly and the platform concept of Bundler is as well known and as intuitive as RubyGems.

If RubyGems alone would allow developers to solve the problem of dependencies varying with the user's platform, that is what I would use. As long as RubyGems falls short to solve the problem, we need to solve it some other way. Using Bundler solves the problem, and does it nicely.

> **···**
>
> On 2011-01-29, at 18:55, Luis Lavena wrote:
> 
> --  
> With kind regards  
> Uwe Kubosch  
> Kubosch Consulting  
> uwe@kubosch.no
> 
> > **[kubosch.no - Welcome to Kubosch Consulting](http://www.kubosch.no/)**
> >
> > kubosch.no - bringing martial arts and information technology together.

---

<div class="post-metadata">

**Author:** ![Luis\_Lavena1](https://avatars.discourse-cdn.com/v4/letter/l/f08c70/32.png) [@Luis\_Lavena1](https://rubytalk.org/u/Luis_Lavena1)\
**Post date:** [30 January 2011 18:02 UTC](https://rubytalk.org/t/how-to-build-gem-whose-dependencies-depend-of-the-platform/61603/8 "2011-01-30T18:02:47Z")

</div>

> \> RubyGems post\_install is not scripts, perhaps you're using extension  
> \> compilation to make that happen, which is intrusive to the user.
> 
> You are right, I use the extension compilation to run the extra install step.
> 
> How is this intrusive?

If user grant RubyGems sudo to install the gem and it's dependencies,  
you're sneaking something that the user was not aware of.

If the user using a gem list find this gem he didn't install, on  
uninstallation no dependency warning will be triggered, and the  
original gem will not work because of that.

> \> You're also bringing Bundler when the user might not want it into the  
> \> mix and slowing down the gem loading process.
> 
> I would rather add Bundler than not have the gem install all its dependencies. The user experience is what matters, and this way all dependencies install correctly and the gem is ready for use regardless of the users platform.

This is similar case to what Hoe did in the past, as add it as  
dependency even that you used for development only. Search for it.

> \> Bundler is awesome for development, but is awful for runtime  
> \> dependency. You should not be imposing bundler to the user and even  
> \> less for runtime execution.
> 
> Why not? Bundler is awesome for runtime dependencies as well. It is the default mechanism for Ruby On Rails runtime dependencies.

> **[IbrasTen.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=ibrasten.com)**
>
> The leading source brandable domain names. Competitive prices. Excellent service. Get your domain name today.

> \> Also bundler do not handle properly all the platforms, using a  
> \> different concept of "platform" of what RubyGems understand and is  
> \> widely known.
> 
> The platform concept of Bundler is different from RubyGems platform, but Bundler handles all platforms properly and the platform concept of Bundler is as well known and as intuitive as RubyGems.

No, it doesn't, take for example JSON gem, is broken under 1.9.x or  
1.8.x for Windows and even when the user decided to fix it manually,  
bundler always force it's installation.

> <https://github.com/rubygems/bundler/issues/635>
>
> We've got 4 people working in our team, 2 on Macs, 1 on Linux and 1 on Windows. …They all have Bundler either at 1.0.0 final or something very recent (rc5, 6). And here's what happens:
> \- today I updated the lock file with bundle install on my Mac, and committed the change; a few hours later, the other Mac developer did the same, and his lock file had the Windows gems reversed - e.g.
> 
> my commit:
> 
> \`\`\`
> json (1.4.6)
> json (1.4.6-x86-mingw32)
> \`\`\`
> 
> his commit:
> 
> \`\`\`
> json (1.4.6-x86-mingw32)
> json (1.4.6)
> \`\`\`
> \- when the Linux developer calls bundle install, it's generated without the Windows versions at all
> \- when the Windows developer calls bundle install, it's generated with only the Windows versions (so json 1.4.6 generic disappears)
> 
> It's not a critical bug, as we can always remove those changes from the diff before committing, but it is very annoying...

> <https://github.com/rubygems/bundler/issues/589>
>
> Hello,
> 
> Tried to reach wycats and carllerche on this in the past, and since ther…e is no mailing list, decide to open "a bug".
> 
> The case is covered here:
> 
> http://gist.github.com/536191
> 
> Basically:
> \- Documentation states mswin is Windows
> \- ming is misspelled (is mingw) Minimalist GNU for Windows (MinGW)
> \- ming is excluded from valid platforms
> 
> All the above cases render RubyInstaller useless. First, RubyInstaller is MinGW based, meaning that 'mswin' will not fit the bill.
> 
> Users indicating 'mswin' will force the installation of x86-mswin32 gems, when the platform is x86-mingw32, so if the user already had sqlite3-ruby-1.3.1-x86-mingw32.gem installed, it will attempt to install the mswin32 platform gem instead.
> 
> Users indicating 'ming' will get an error:
> 
> \<pre\>\<code\>
> C:\\Users\\Luis\\Projects\\\_sandbox\\bundle-test\>bundle check
> \`ming\` is not a valid platform. The available options are: \[:jruby, :ruby\_18, :mswin, :ruby\_19, :ruby, :mri, :mri\_18, :mri\_19\]
> \</code\>\</pre\>
> 
> 
> I commented in the past this to Yehuda that if platform :windows is proposed, the correct usage of both x86-mswin32 and x86-mingw32 should be considered.
> 
> I believe the current state and the documentation shown so far will lead to errors for these users and complains to RubyInstaller which, IMHO, are not the root of it.
> 
> Thank you.

> **···**
>
> On Jan 30, 8:06 am, Uwe Kubosch \<u...@kubosch.no\> wrote:
> 
> --  
> Luis Lavena

---

<div class="post-metadata">

**Author:** ![Uwe\_Kubosch1](https://avatars.discourse-cdn.com/v4/letter/u/53a042/32.png) [@Uwe\_Kubosch1](https://rubytalk.org/u/Uwe_Kubosch1)\
**Post date:** [30 January 2011 22:23 UTC](https://rubytalk.org/t/how-to-build-gem-whose-dependencies-depend-of-the-platform/61603/9 "2011-01-30T22:23:33Z")

</div>

> > > RubyGems post\_install is not scripts, perhaps you're using extension  
> > > compilation to make that happen, which is intrusive to the user.
> > 
> > You are right, I use the extension compilation to run the extra install step.  
> > How is this intrusive?

> If user grant RubyGems sudo to install the gem and it's dependencies,  
> you're sneaking something that the user was not aware of.
> 
> If the user using a gem list find this gem he didn't install, on  
> uninstallation no dependency warning will be triggered, and the  
> original gem will not work because of that.

True. I consider this a flaw and would very much like to solve that, too.

> > > You're also bringing Bundler when the user might not want it into the  
> > > mix and slowing down the gem loading process.
> > 
> > I would rather add Bundler than not have the gem install all its dependencies. The user experience is what matters, and this way all dependencies install correctly and the gem is ready for use regardless of the users platform.
> 
> This is similar case to what Hoe did in the past, as add it as  
> dependency even that you used for development only. Search for it.

The difference is that Bundler is a good gem for setting up runtime dependencies. It is not a development only gem.

> > > Bundler is awesome for development, but is awful for runtime  
> > > dependency. You should not be imposing bundler to the user and even  
> > > less for runtime execution.
> > 
> > Why not? Bundler is awesome for runtime dependencies as well. It is the default mechanism for Ruby On Rails runtime dependencies.

> [http://www.ibrasten.com/articles/2010/11/15/reducing-jruby-startup-by-ditching-bundler](http://www.ibrasten.com/articles/2010/11/15/reducing-jruby-startup-by-ditching-bundler)

Good article, and valid if your main concern is application startup time. My main concerns are ease of installation, and performance after startup. Bundler fits that need.

> > > Also bundler do not handle properly all the platforms, using a  
> > > different concept of "platform" of what RubyGems understand and is  
> > > widely known.
> > 
> > The platform concept of Bundler is different from RubyGems platform, but Bundler handles all platforms properly and the platform concept of Bundler is as well known and as intuitive as RubyGems.
> 
> No, it doesn't, take for example JSON gem, is broken under 1.9.x or  
> 1.8.x for Windows and even when the user decided to fix it manually,  
> bundler always force it's installation.
> 
> [Gemfile.lock is generated differently on different platforms · Issue #635 · rubygems/bundler · GitHub](https://github.com/carlhuda/bundler/issues/635)  
> [Bundler platforms for Windows are messed up · Issue #589 · rubygems/bundler · GitHub](https://github.com/carlhuda/bundler/issues/589)

These are valid concerns, and it looks like they are being taken seriously, so hopefully they will be sorted out. I appreciate your efforts on improving Ruby on Windows.

If your gem hits any of these issues, you need to address them, and maybe using Bundler is not your solution. If your gem does not hit these issues, maybe you accept your gem will not be used on Windows, Bundler is a solution to the original problem of having a valid set of gems installed with your gem without the user having to do extra steps.

I would like to hear other solutions than using Bundler that enables the correct installation of dependent gems that vary with the user's platform.

> **···**
>
> On 2011-01-30, at 19:02, Luis Lavena wrote:
> 
> --  
> With kind regards  
> Uwe Kubosch  
> Kubosch Consulting  
> uwe@kubosch.no
> 
> > **[kubosch.no - Welcome to Kubosch Consulting](http://www.kubosch.no/)**
> >
> > kubosch.no - bringing martial arts and information technology together.
