Re: [vagrant-up] Require gem in Vagrantfile

1,536 views
Skip to first unread message

Mitchell Hashimoto

unread,
Jul 3, 2012, 3:06:54 PM7/3/12
to vagra...@googlegroups.com
Martin,

On Sun, Jul 1, 2012 at 8:51 PM, Martin Jöhren <m.jo...@gmail.com> wrote:
> I'm trying to add a locally installed gem into a Vagrantfile and fail:
>
> Vagrantfile:
>
> ...
>
> require 'fog'
> ...
>
>
> Error:
>
>
> `require': cannot load such file -- fog (LoadError)
>
> How do I fix that? I mean, I found out that Vagrant comes with its own
> embedded stuf, so I got it working by editing the vagrant.gemspec in the
> source code folder of vagrant and inserting s.add_dependency "fog", "~>
> 1.4.0" and rebuild everything, but there must be a better solution like
> linking to my locally installed gems. I'm quite new to ruby so please be
> gentle ;)

You're very right. Vagrant has its own isolated install so that your
rubygems don't show up in the Vagrantfile. The easiest way to do this
at the moment is to use `vagrant gem`:

`vagrant gem install fog`

This will install fog into Vagrant's private gem repo.

However, `vagrant gem` may disappear in the future, but I'll think of
a better way to provide this at some other time. For now, this works.

Best,
Mitchell

>
>

Martin Jöhren

unread,
Jul 4, 2012, 3:34:25 PM7/4/12
to vagra...@googlegroups.com
Hi,

got it working another way:

I added GEM_PATH=[path_to_my_gems] into my bashrc. This will also be used by the embedded gem in vagrant and everything is fine :)

Greetz and thx for your answer!

Luke Patterson

unread,
Jul 4, 2012, 3:50:07 PM7/4/12
to vagra...@googlegroups.com
On Wed, Jul 4, 2012 at 2:34 PM, Martin Jöhren <m.jo...@gmail.com> wrote:
got it working another way:

> I'm trying to add a locally installed gem into a Vagrantfile and fail: 


Some more trickery from a previous discussion:

-- To install/ensure from the Vagrantfile, and avoid the need for user to manually install --

librarian_name = 'librarian'
librarian_version = '0.0.23'
begin
  Gem::Specification.find_by_name(librarian_name, librarian_version)
rescue Gem::LoadError
  begin
    require 'vagrant/environment'
    env = Vagrant::Environment.new
    env.cli('gem', 'install', librarian_name, '--version', librarian_version)
  rescue SystemExit
  end
end


That's the basic pattern anyways, and I'm sure you could turn it into a method if you needed to load more gems.

Martin Jöhren

unread,
Jul 8, 2012, 2:27:33 AM7/8/12
to vagra...@googlegroups.com
Cool. This solved the problem of installing all required gems by hand on my colleagues machines :)
Thx!

shane knapp ☠

unread,
Jul 9, 2012, 12:33:27 AM7/9/12
to vagra...@googlegroups.com
ditto. i had *just* run in to this problem when the thread popped in
to my inbox!

cheers!

shane

On Sat, Jul 7, 2012 at 11:27 PM, Martin Jöhren <m.jo...@gmail.com> wrote:
> Cool. This solved the problem of installing all required gems by hand on my colleagues machines :)
> Thx!



--
shane knapp // dj shank // incomplete
Reply all
Reply to author
Forward
0 new messages