Ask the vagrantfile vagrantfile plugin?

The intended implementation of Vagrantfile requires a special Vagrant plugin. So basically, what you need to do is

 $ vagrant plugin install foobar-plugin $ vagrant up 

If you skip the first step, vagrant up will fail.

Is there an option in Vagrant to automatically install the plugin? In other words: is it possible to specify inside Vagrantfile which plugins to install automatically before creating and loading the machine?

+70
vagrant
Oct 21 '13 at 11:01
source share
12 answers

2019 Update: Vagrant now has a feature requiring plugins through Vagrantfile via:

 Vagrant.configure("2") do |config| config.vagrant.plugins = "vagrant-some-plugin" # or as array: config.vagrant.plugins = ["vagrant-some-plugin", "vagrant-some-other-plugin"] # or as hash config.vagrant.plugins = {"vagrant-some-plugin" => {"version" => "1.0.0"}} end 

See https://www.vagrantup.com/docs/vagrantfile/vagrant_settings.html

+3
Aug 29 '19 at 10:27
source share

UPDATE August 31, 2018: see @Starx hotfix below for later versions of Vagrant (1.8 and higher)

Here is a version based on Louis St. Amour, along with Rob Kignon's comment on restarting, if a new plugin was installed, I successfully use it in my own installation:

 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 
+67
Mar 02 '15 at 1:10
source share

Since I am a Ruby developer, and Bindler is no longer supported, I find it most natural to just write the code at the top of my Vagrantfile to install the necessary plugins if it is missing (for example, before the Vagrant.configure line)

The following works for me:

 required_plugins = %w( vagrant-hostmanager vagrant-someotherplugin ) required_plugins.each do |plugin| system "vagrant plugin install #{plugin}" unless Vagrant.has_plugin? plugin end 

system , unlike using backticks, there will be an echo command on stdout, as well as executing the command yourself. And so I don’t need another weirdly named plugin or system to keep track of the necessary plugins that can be updated by Vagrant anyway.

+50
Sep 18 '14 at 16:44
source share

As I said in the answer to your other question , you can use bindler to install a set of plugins specific to a project with one command.

If bindler is installed, but the required plugin is not, bindler will throw an error and abort the process. There is also an open problem related to the automatic installation of plugins on vagrant up but so far no one has subscribed to it.

If you do not want to use bindler, can you use Vagrant.has_plugin? (available at 1.3. 0+) at the top of your Vagrantfile and Vagrantfile error if the required plugin is not installed.

Something like:

 unless Vagrant.has_plugin?("vagrant-some-plugin") raise 'some-plugin is not installed!' end Vagrant.configure("2") do |config| config.vm.box = "box-name" end 



UPDATE : Bindler is no longer supported and equivalent functionality has not been provided by the Vagrant kernel since May 11, 2015.

+48
Oct 22 '13 at 1:30
source share

Note that with Gemfile Vagrant 1.5 you can specify your dependencies in your Gemfile . According to the blog post about the update :

Now Vagrant 1.5 will automatically upload all the gems to the plugins group in your Gemfile. For example, here is the Gemfile for the vagrant-bar plugin:

 source "https://rubygems.org" group :development do gem "vagrant", git: "https://github.com/mitchellh/vagrant.git" end group :plugins do gem "vagrant-foo", gem "vagrant-bar", path: "." end 
+10
Dec 11 '14 at 21:18
source share

Failed to add a comment to Louis St-Amour, but I wanted to post it just in case someone needed help expanding his solution.

 # Check for missing plugins required_plugins = %w(vagrant-list) plugin_installed = false required_plugins.each do |plugin| unless Vagrant.has_plugin?(plugin) system "vagrant plugin install #{plugin}" plugin_installed = true end end # If new plugins installed, restart Vagrant process if plugin_installed === true exec "vagrant #{ARGV.join' '}" end 
+6
Mar 18 '15 at 15:16
source share

In the new version of Vagrant, @Amos Shapira's answer is stuck in an endless loop. The reason for this is that every call to the vagrant plugin install Vagrantfile vagrant plugin install also processes the Vagrantfile and, when processing, executes code related to installing the plugin again and again, and so on.

Here is my solution that avoids loops.

 # Plugins # # Check if the first argument to the vagrant # command is plugin or not to avoid the loop if ARGV[0] != 'plugin' # Define the plugins in an array format required_plugins = [ 'vagrant-vbguest', 'vagrant-hostmanager', 'vagrant-disksize' ] 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 end 
+3
Aug 20 '18 at 6:17
source share

I just noticed the http://docs.vagrantup.com/v2/plugins/packaging.html instruction here

 Vagrant.require_plugin "vagrant-aws" 

which does the same as desgibed fgrehm: quickly raises an error if the plugin is not installed.

As far as I know, there is no way to automatically install plugins

+1
Feb 13 '14 at 13:57
source share

My answer is very close to the Louis St-Amour answer , but instead of automatically installing the plugins, it simply causes an error message, so the user must install the plugin manually.

I would prefer that users be aware of any plugins that are installed because they apply globally to all Vagrant instances, not just the current Vagrantfile.

Put one line at the top of the Vagrantfile similar to this for each plugin, in this example vagrant-vbguest :

  raise "vagrant-vbguest plugin must be installed" unless Vagrant.has_plugin? "vagrant-vbguest" 
+1
Apr 29 '15 at 9:29
source share

You can use this project (I am the author): https://github.com/DevNIX/Vagrant-dependency-manager

It is based on similar answers, but tries to be more complete and stable. The big advantage of this idea is that you can distribute your Vagrantfile and run on each computer, regardless of the installed plugins in this environment.

Easy to use:

  • Copy the dependency_manager.rb parameter next to your Vagrantfile
  • Turn it on and call check_plugins , passing your dependencies as an array

    Example:

     # -*- mode: ruby -*- # vi: set ft=ruby : require File.dirname(__FILE__)+"./dependency_manager" check_plugins ["vagrant-exec", "vagrant-hostsupdater", "vagrant-cachier", "vagrant-triggers"] Vagrant.configure(2) do |config| config.vm.box = "base" end 
  • ???

  • Profit!

I would like to combine pull requests, fix any problem I might have, and get ideas for new features. I'm currently thinking of updating the dependency manager itself and requiring specific versions of the plugin: D

Hello!

+1
Aug 26 '16 at 9:09
source share

I am having a problem with a new installation of Vagrant where the .vagrant.d directory has not been created yet. I was able to make a snippet from the work of Luis Saint Amour, catching the exception.

 required_plugins = %w(vagrant-share vagrant-vbguest) begin 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 rescue exec "vagrant #{ARGV.join(' ')}" end 
0
Mar 16 '16 at 17:15
source share

Here I am using Vagrant 1.8 and it works fine. Put this somewhere before the configure block in your Vagrantfile.

 # install required plugins if necessary if ARGV[0] == 'up' # add required plugins here required_plugins = %w( plugin1 plugin2 plugin3 ) missing_plugins = [] required_plugins.each do |plugin| missing_plugins.push(plugin) unless Vagrant.has_plugin? plugin end if ! missing_plugins.empty? install_these = missing_plugins.join(' ') puts "Found missing plugins: #{install_these}. Installing using sudo..." exec "sudo vagrant plugin install #{install_these}; vagrant up" end end 
0
Jun 15 '16 at 20:29
source share



All Articles