You are currently viewing Learn How to Set Vagrant Default Provider with 3 Easy Methods

Learn How to Set Vagrant Default Provider with 3 Easy Methods

Vagrant is a powerful tool for creating and managing virtual machine (VM) environments. It streamlines the process of setting up development environments, ensuring consistency across different platforms, and eliminating the dreaded “works on my machine” issue.

Vagrant Default Provider

Vagrant ships with built-in support for several virtualization providers, including:

  • VirtualBox: A popular open-source virtualization platform.
  • Hyper-V: A Microsoft hypervisor technology.
  • Docker: A containerization platform.

However, Vagrant is not limited to these providers. It can also manage other types of machines by using alternate providers. The default provider for vagrant is virtualbox.

Virtualbox is an open-source, cross-platform virtualisation solution from Oracle. Even without vagrant setting up virtual machines with virtualbox is pretty simple. Virtualbox is also a cross-platform solution available in Windows, Linux, and Mac operating systems.

All you have to do is install vagrant and virtualbox to spin up your virtual machine and no extra plugins are required. Let’s test the default provider by spinning up a virtual machine.

NOTE: Before using the vagrant box make sure the vagrant box is available with the particular provider. In my case I am using ‘fedora/39-cloud-base’ vagrant box.

$ mkdir ubuntu_vm
$ cd ubuntu vm
$ vagrant init -m "fedora/39-cloud-base"

The above commands will create a new directory and create a minimal vagrant file for the ‘‘fedora/39-cloud-base’ virtual machine. The following command will spin up the virtual machine with virtualbox as the provider.

$ vagrant up

Run the ‘vagrant ssh’ command to connect to the virtual machine or open the virtual machine directly in the virtualbox interface and log in.

$ vagrant ssh

If you run the ‘vagrant box list’ command it will show you the box name along with the provider name.

$ vagrant box list
fedora/39-cloud-base (virtualbox, 39.20231031.1, (amd64))

Now you have a virtual machine up and running with the default virtualbox provider without any explicit configuration.

Vagrant Default Provider – Command Line Argument

When running the vagrant up command you can pass the ‘–provider’ flag and pass the provider name as the argument. This takes higher precedence even if you have specified a different provider inside the vagrant file.

$ vagrant up --provider-virtualbox

As foretold, there are no explicit configurations required for virtualbox unless a different provider is set as the default provider. Below are some of the examples with the –provider flag.

NOTE: To work with other providers, required provider plugins should be installed.

# vmware fusion #
$ vagrant up --provision=vmware_fusion

# libvirt #
$ vagrant up --provider=libvirt

# Hyper-V #
$ vagrant up --provider=hyperv

Setting Vagrant Default Provider Environment Variable

You can set the default provider by setting the ‘VAGRANT_DEFAULT_PROVIDER’ either inside the vagrant file or as a shell environmental variable. This will take preference when the –provider flag is not used.

Add the following as the first line in your vagrant file.

ENV['VAGRANT_DEFAULT_PROVIDER'] = "libvirt"

Below is the sample Vagrant file to spin up the virtual machine with the libvirt provider.

ENV['VAGRANT_DEFAULT_PROVIDER'] = "libvirt" 
Vagrant.configure('2') do |config|
  config.vm.box "fedora/39-cloud-base"
end

You can also set the environmental variable directly in the shell config files. Here the example is shown for bash, zsh, and fish shell with libvirt as default provider.

# Bash shell #
$ echo "export VAGRANT_DEFAULT_PROVIDER=libvirt" >> ~/.bashrc
$ source ~/.bashrc

# ZSH shell #
$ echo "export VAGRANT_DEFAULT_PROVIDER=libvirt" >> ~/.zshenv
$ source ~/.zshenv

# Fish shell #
$ set -Ux VAGRANT_DEFAULT_PROVIDER libvirt

Vagrant Default Provider With Configuration

You can use the ‘config.vm.provider’ configuration to set the default provider. This configuration is mostly used to set provider-specific parameters.

Vagrant.configure("2") do config 
  config.vm.provider "libvirt"   # Add as the first line inside the config block #
  config.vm.box = "fedora/39-cloud-base"
end

The ‘config.vm.provider’ is mostly used when you want to modify or set provider-specific configuration. For example, here I am using the configuration to set memory and CPU for the virtual machine.

Vagrant.configure("2") do config
  config.vm.box = "fedora/39-cloud-base"
  config.vm.provider "libvirt" do |lv|
    lv.memory=1024
    lv.cpus=2
  end
end

You can also add multiple providers with ‘config.vm.provider’ in a single Vagrantfile. The first provider that the vagrant finds will be used.

Vagrant.configure("2") do config
  config.vm.provider "libvirt"  #Vagrant will read this parameter first and set it as the provider #
  config.vm.provider "virtualbox" # This will be ignored #
  config.vm.box = "fedora/39-cloud-base"
end

Wrap Up

In this article, we have seen three different ways to set the vagrant default provider. My choice is, if I am not using virtualbox then will add ENV[‘VAGRANT_DEFAULT_PROVIDER’] parameter in the vagrantfile.

Leave a Reply

nineteen − nineteen =