Search code examples
rubywindowsrubygemsvagrantvagrantfile

How to install Ruby Gems for Vagrant, i.e. for usage in a Vagrantfile and executed on the host, on linux and windows?


I am using Vagrant for provisioning virtual machines. The Vagrantfile executes some custom Ruby code on the host. (This Ruby code is executed directly in the Vagrantfile, but could also be executed as part of a proper Vagrant plugin. It is the same situation.)

I would like to use the functionality of 3rdparty Ruby Gems in this very code, but I'm not sure how to properly install the Gems in the Vagrant context. 1) I don't know how Vagrant invokes Ruby. Is it some modified Ruby interpreter for Vagrant? Or is it a standard Ruby installation? 2) How do I, preferably on the command line (linux shell/PowerShell), install Ruby Gems that can be used from the Vagrantfile? Is there a special location for the Gems so that Vagrant can include them?

I would write about what I did so far at this point, but I just don't know where to start. This seems to be an uncommon use case, because I haven't found anything that goes into this direction. Maybe the answer is obvious, but I seem to lack proper understanding of what is required for Vagrant/Ruby to 'see' an installed Ruby Gem.


Solution

  • According to the documentation for Vagrant plugins:

    This installs a plugin with the given name or file path. If the name is not a path to a file, then the plugin is installed from remote repositories, usually RubyGems.

    # Installing a plugin from a known gem source
    $ vagrant plugin install my-plugin
    

    I don't use Vagrant so I can't confirm that it works, however a quick search shows that someone else wanted to accomplish the same goal and this worked for them:

    require 'inifile'
    settings = IniFile.load(CONFIGFILE)
    
    Message: LoadError: cannot load such file -- inifile
    

    I found that I can get these to work if I do:

    vagrant plugin install inifile