Demand a Vagrant plugin within the Vagrantfile?

Golo Roden picture Golo Roden · Oct 21, 2013 · Viewed 22.3k times · Source

Supposed execution of a Vagrantfile requires a specific Vagrant plugin to be installed. So, basically what you need to do is

$ vagrant plugin install foobar-plugin
$ vagrant up

If you skip the first step, vagrant up fails.

Is there an option in Vagrant to make it install the plugin automatically? In other words: Is it possible to specify within a Vagrantfile which plugins to install automatically before creating and booting up the machine?

Answer

Amos Shapira picture Amos Shapira · Mar 2, 2015

UPDATE Aug 31, 2018: See @Starx's fix below for later versions of Vagrant (1.8 and above)

Here is version based on Louis St. Amour's solution together with Rob Kinyon's comment about re-exec if a new plugin was installeed, I use it successfully in my own setup:

required_plugins = %w(vagrant-share vagrant-vbguest...)

plugins_to_install = required_plugins.select { |plugin| not Vagrant.has_plugin? plugin }
if not plugins_to_install.empty?
  puts "Installing plugins: #{plugins_to_install.join(' ')}"
  if system "vagrant plugin install #{plugins_to_install.join(' ')}"
    exec "vagrant #{ARGV.join(' ')}"
  else
    abort "Installation of one or more plugins has failed. Aborting."
  end
end