# Library gem dependencies

**URL:** <https://discourse.chef.io/t/library-gem-dependencies/3820>\
**Category:** Chef Infra (archive)\
**Created:** [March 5, 2013, 9:28am UTC](https://discourse.chef.io/t/library-gem-dependencies/3820 "2013-03-05T09:28:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_Darwin1](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Sam\_Darwin1](https://discourse.chef.io/u/Sam_Darwin1)\
**Post date:** [March 5, 2013, 9:28am UTC](https://discourse.chef.io/t/library-gem-dependencies/3820/1 "2013-03-05T09:28:34Z")

</div>

Let’s say a cookbook library requires a gem called treetop (it could be any gem  
actually), and so it has this line:

require ‘treetop’

I want to get that installed as a pre-requisite, and so I add this code into  
the default recipe:

r = chef\_gem “treetop” do  
action :nothing  
end  
r.run\_action(:install)

But apparently it won’t get called soon enough. The library has errors about  
treetop missing.

As another attempt, I create a different cookbook called pre-requisites. I add  
the code “r = chef\_gem “treetop” do” into the pre-requisites cookbook, and  
have that as a cookbook dependency. Nope. This also doesn’t get called soon  
enough. treetop is missing. Installing the treetop gem by hand will fix  
it.

The issue seems to be with a library and not with a recipe file, that is the  
twist.

---

<div class="post-metadata">

**Author:** ![Philippe\_Berard](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/philippe_berard/32/201_2.png) [@Philippe\_Berard](https://discourse.chef.io/u/Philippe_Berard)\
**Post date:** [March 5, 2013, 9:32am UTC](https://discourse.chef.io/t/library-gem-dependencies/3820/2 "2013-03-05T09:32:54Z")

</div>

I don’t know if I do it correctly, but here’s my answer.

Just do it that way :  
chef\_gem 'treetop’  
require ‘treetop’

It will be installed at the right time before being loaded by the recipe.

Regards,

– Philippe Bérard

-----Message d’origine-----  
De : Sam Darwin [[mailto:samuel.d.darwin@gmail.com](mailto:samuel.d.darwin@gmail.com)]  
Envoyé : mardi 5 mars 2013 10:29  
À : [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
Objet : [chef] library gem dependencies

Let’s say a cookbook library requires a gem called treetop (it could be any  
gem actually), and so it has this line:

require ‘treetop’

I want to get that installed as a pre-requisite, and so I add this code into  
the default recipe:

r = chef\_gem “treetop” do  
action :nothing  
end  
r.run\_action(:install)

But apparently it won’t get called soon enough. The library has errors  
about  
treetop missing.

As another attempt, I create a different cookbook called pre-requisites. I  
add the code “r = chef\_gem “treetop” do” into the pre-requisites cookbook,  
and  
have that as a cookbook dependency. Nope. This also doesn’t get called  
soon  
enough. treetop is missing. Installing the treetop gem by hand will  
fix  
it.

The issue seems to be with a library and not with a recipe file, that is the  
twist.

---

<div class="post-metadata">

**Author:** ![millisami\_r](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/millisami_r/32/418_2.png) [@millisami\_r](https://discourse.chef.io/u/millisami_r)\
**Post date:** [March 5, 2013, 9:35am UTC](https://discourse.chef.io/t/library-gem-dependencies/3820/3 "2013-03-05T09:35:03Z")

</div>

Yup, mine too is working the chef\_gem way:

%w(libxml2-dev libxslt1-dev libcurl4-gnutls-dev).each do |pkg|  
r = package pkg do  
action :nothing  
end  
r.run\_action(:install)  
end

chef\_gem "aws-sdk"  
require 'aws'

* * *

@millisami  
~ Sachin Sagar Rai  
Ruby on Rails Developer  
[http://tfm.com.np](http://tfm.com.np)  
[http://nepalonrails.com](http://nepalonrails.com) ([http://nepalonrails.tumblr.com](http://nepalonrails.tumblr.com))  
[http://funsole.com](http://funsole.com)  
Sent with Sparrow ([http://www.sparrowmailapp.com/?sig](http://www.sparrowmailapp.com/?sig))

On Tuesday, March 5, 2013 at 3:17 PM, Philippe Bérard wrote:

> I don't know if I do it correctly, but here's my answer.
> 
> Just do it that way :  
> chef\_gem 'treetop'  
> require 'treetop'
> 
> It will be installed at the right time before being loaded by the recipe.
> 
> Regards,
> 
> -- Philippe Bérard
> 
> -----Message d'origine-----  
> De : Sam Darwin [[mailto:samuel.d.darwin@gmail.com](mailto:samuel.d.darwin@gmail.com)]  
> Envoyé : mardi 5 mars 2013 10:29  
> À : [chef@lists.opscode.com](mailto:chef@lists.opscode.com) ([mailto:chef@lists.opscode.com](mailto:chef@lists.opscode.com))  
> Objet : [chef] library gem dependencies
> 
> Let's say a cookbook library requires a gem called treetop (it could be any  
> gem actually), and so it has this line:
> 
> require 'treetop'
> 
> I want to get that installed as a pre-requisite, and so I add this code into  
> the default recipe:
> 
> r = chef\_gem "treetop" do  
> action :nothing  
> end  
> r.run\_action(:install)
> 
> But apparently it won't get called soon enough. The library has errors  
> about  
> treetop missing.
> 
> As another attempt, I create a different cookbook called pre-requisites. I  
> add the code "r = chef\_gem "treetop" do" into the pre-requisites cookbook,  
> and  
> have that as a cookbook dependency. Nope. This also doesn't get called  
> soon  
> enough. treetop is missing. Installing the treetop gem by hand will  
> fix  
> it.
> 
> The issue seems to be with a library and not with a recipe file, that is the  
> twist.

---

<div class="post-metadata">

**Author:** ![Sam\_Darwin1](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Sam\_Darwin1](https://discourse.chef.io/u/Sam_Darwin1)\
**Post date:** [March 5, 2013, 9:38am UTC](https://discourse.chef.io/t/library-gem-dependencies/3820/4 "2013-03-05T09:38:17Z")

</div>

In a libraries file, not a recipe file.

On Tue, Mar 5, 2013 at 11:32 AM, Philippe Bérard  
[philippe.berard@jalios.com](mailto:philippe.berard@jalios.com) wrote:

> I don't know if I do it correctly, but here's my answer.
> 
> Just do it that way :  
> chef\_gem 'treetop'  
> require 'treetop'
> 
> It will be installed at the right time before being loaded by the recipe.
> 
> Regards,
> 
> -- Philippe Bérard
> 
> -----Message d'origine-----  
> De : Sam Darwin [[mailto:samuel.d.darwin@gmail.com](mailto:samuel.d.darwin@gmail.com)]  
> Envoyé : mardi 5 mars 2013 10:29  
> À : [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
> Objet : [chef] library gem dependencies
> 
> Let's say a cookbook library requires a gem called treetop (it could be any  
> gem actually), and so it has this line:
> 
> require 'treetop'
> 
> I want to get that installed as a pre-requisite, and so I add this code into  
> the default recipe:
> 
> r = chef\_gem "treetop" do  
> action :nothing  
> end  
> r.run\_action(:install)
> 
> But apparently it won't get called soon enough. The library has errors  
> about  
> treetop missing.
> 
> As another attempt, I create a different cookbook called pre-requisites. I  
> add the code "r = chef\_gem "treetop" do" into the pre-requisites cookbook,  
> and  
> have that as a cookbook dependency. Nope. This also doesn't get called  
> soon  
> enough. treetop is missing. Installing the treetop gem by hand will  
> fix  
> it.
> 
> The issue seems to be with a library and not with a recipe file, that is the  
> twist.

---

<div class="post-metadata">

**Author:** ![Philippe\_Berard](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/philippe_berard/32/201_2.png) [@Philippe\_Berard](https://discourse.chef.io/u/Philippe_Berard)\
**Post date:** [March 5, 2013, 10:10am UTC](https://discourse.chef.io/t/library-gem-dependencies/3820/5 "2013-03-05T10:10:00Z")

</div>

I'm afraid to say that it works in a recipe.

Regards,

-- Philippe Bérard

-----Message d'origine-----  
De : Sam Darwin [[mailto:samuel.d.darwin@gmail.com](mailto:samuel.d.darwin@gmail.com)]  
Envoyé : mardi 5 mars 2013 10:38  
À : [chef@lists.opscode.com](mailto:chef@lists.opscode.com); Philippe Bérard  
Objet : [chef] Re: library gem dependencies

In a libraries file, not a recipe file.

On Tue, Mar 5, 2013 at 11:32 AM, Philippe Bérard  
[philippe.berard@jalios.com](mailto:philippe.berard@jalios.com) wrote:

> I don't know if I do it correctly, but here's my answer.
> 
> Just do it that way :  
> chef\_gem 'treetop'  
> require 'treetop'
> 
> It will be installed at the right time before being loaded by the recipe.
> 
> Regards,
> 
> -- Philippe Bérard
> 
> -----Message d'origine-----  
> De : Sam Darwin [[mailto:samuel.d.darwin@gmail.com](mailto:samuel.d.darwin@gmail.com)]  
> Envoyé : mardi 5 mars 2013 10:29  
> À : [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
> Objet : [chef] library gem dependencies
> 
> Let's say a cookbook library requires a gem called treetop (it could  
> be any gem actually), and so it has this line:
> 
> require 'treetop'
> 
> I want to get that installed as a pre-requisite, and so I add this  
> code into the default recipe:
> 
> r = chef\_gem "treetop" do  
> action :nothing  
> end  
> r.run\_action(:install)
> 
> But apparently it won't get called soon enough. The library has errors  
> about  
> treetop missing.
> 
> As another attempt, I create a different cookbook called  
> pre-requisites. I add the code "r = chef\_gem "treetop" do" into the  
> pre-requisites cookbook, and  
> have that as a cookbook dependency. Nope. This also doesn't get called  
> soon  
> enough. treetop is missing. Installing the treetop gem by hand will  
> fix  
> it.
> 
> The issue seems to be with a library and not with a recipe file, that  
> is the twist.

---

<div class="post-metadata">

**Author:** ![Gregory\_Symons](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/gregory_symons/32/189_2.png) [@Gregory\_Symons](https://discourse.chef.io/u/Gregory_Symons)\
**Post date:** [March 5, 2013, 7:23pm UTC](https://discourse.chef.io/t/library-gem-dependencies/3820/6 "2013-03-05T19:23:47Z")

</div>

Libraries are loaded before recipes are compiled, so you can't install  
gems for direct use by libraries. What you can do, though, is install  
the gems using chef\_gem the way you normally would, and then have a  
load\_dependencies method in your library that requires in all the gems  
you needed (warning, untested code follows):

module Cookbook  
class MyLibrary  
class \<\< self  
def load\_dependencies  
require 'my\_gem'  
end

```
    def foo_uses_my_gem bar
        MyLibrary.load_dependencies
        MyGem::do_the_thing_with_the_bar bar
    end
end
end

```

end

And in your recipe you can do this:

chef\_gem 'my\_gem'  
MyLibrary.foo\_users\_my\_gem node[:cookbook][:bar]

Greg  
On Tue 05 Mar 2013 04:10:00 AM CST, Philippe Bérard wrote:

> I'm afraid to say that it works in a recipe.
> 
> Regards,
> 
> -- Philippe Bérard
> 
> -----Message d'origine-----  
> De : Sam Darwin [[mailto:samuel.d.darwin@gmail.com](mailto:samuel.d.darwin@gmail.com)]  
> Envoyé : mardi 5 mars 2013 10:38  
> À : [chef@lists.opscode.com](mailto:chef@lists.opscode.com); Philippe Bérard  
> Objet : [chef] Re: library gem dependencies
> 
> In a libraries file, not a recipe file.
> 
> On Tue, Mar 5, 2013 at 11:32 AM, Philippe Bérard  
> [philippe.berard@jalios.com](mailto:philippe.berard@jalios.com) wrote:
> 
> > I don't know if I do it correctly, but here's my answer.
> > 
> > Just do it that way :  
> > chef\_gem 'treetop'  
> > require 'treetop'
> > 
> > It will be installed at the right time before being loaded by the recipe.
> > 
> > Regards,
> > 
> > -- Philippe Bérard
> > 
> > -----Message d'origine-----  
> > De : Sam Darwin [[mailto:samuel.d.darwin@gmail.com](mailto:samuel.d.darwin@gmail.com)]  
> > Envoyé : mardi 5 mars 2013 10:29  
> > À : [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
> > Objet : [chef] library gem dependencies
> > 
> > Let's say a cookbook library requires a gem called treetop (it could  
> > be any gem actually), and so it has this line:
> > 
> > require 'treetop'
> > 
> > I want to get that installed as a pre-requisite, and so I add this  
> > code into the default recipe:
> > 
> > r = chef\_gem "treetop" do  
> > action :nothing  
> > end  
> > r.run\_action(:install)
> > 
> > But apparently it won't get called soon enough. The library has errors  
> > about  
> > treetop missing.
> > 
> > As another attempt, I create a different cookbook called  
> > pre-requisites. I add the code "r = chef\_gem "treetop" do" into the  
> > pre-requisites cookbook, and  
> > have that as a cookbook dependency. Nope. This also doesn't get called  
> > soon  
> > enough. treetop is missing. Installing the treetop gem by hand will  
> > fix  
> > it.
> > 
> > The issue seems to be with a library and not with a recipe file, that  
> > is the twist.

---

<div class="post-metadata">

**Author:** ![Sam\_Darwin1](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Sam\_Darwin1](https://discourse.chef.io/u/Sam_Darwin1)\
**Post date:** [March 6, 2013, 7:29am UTC](https://discourse.chef.io/t/library-gem-dependencies/3820/7 "2013-03-06T07:29:05Z")

</div>

Very clever Greg! That looks like it would work.
