DevOps

Vagrant Boxes Create Virtual Machines in Seconds on VirtualBox, Hyper-V, and VMware

Vagrant boxes act as image of your virtual machine. Instantly spin up your development environment with Vagrant in VirtualBox, Hyper-V, or VMware.

Virtualization technologies are front and center in software development, testing, and deployment. One of the best tools for spinning up quick and easy development environments is a tool from Hashicorp, called Vagrant. Let’s look at how, using Vagrant boxes, we can create and manage virtual machines (VMs) in mere seconds in VirtualBox, Hyper-V, and VMware.

What are Vagrant boxes?

Vagrant boxes are pre-packaged development environments containing all the necessary virtual machine components and packages to spin up your server. You can think of the Vagrant boxes as blueprints for your virtual machines or an image. They provide the foundation for VMs running on VirtualBox, Hyper-V, or VMware.

Vagrant boxes save time by eliminating the need to install and configure an operating system and other software from scratch by hand. Using Vagrant, these provide automation for deploying your VMs. On top of that, Hashicorp and the community provides a catalog of Vagrant boxes that are ready to use, much like containers found in Docker hub. The catalog allows you to search for boxes you want to deploy for your infrastructure.

There are many Linux Vagrant boxes provided by the community. These are open-source distros and include Ubuntu, CentOS, Fedora, etc. However, there are also Windows Vagrant boxes on there as well. You can look at the description and documentation for each box and decide which one you want to use.

Understanding the Vagrant file

The Vagrantfile is written in Ruby. It provides instructions on how each virtual machine environment should be create and how the VM should be managed. Also, as part of the vagrant init command, the Vagrant file is created for you.

When you run the vagrant init command with the vagrant box file you pass in as a parameter, it automatically creates the default vagrant file needed. Once this is created, you can edit the vagrant file with additional parameters. However, it will work perfectly fine without modification.

Key Characteristics of the Vagrantfile:

  • Configuration Script: The Vagrantfile is a script where you specify the parameters for your VMs, such as which base box to use, network settings, synced folders, and provisioning scripts.
  • Ruby Syntax: Despite being a Ruby script, you donโ€™t need deep knowledge of Ruby to use it. The syntax is straightforward and mainly involves modifying predefined variables that are fairly well-documented for each provider.
  • Version Control Friendly: The Vagrantfile is meant to be checked into version control. This way, anyone working on the project can replicate the VM setup. This way, developers can have consistency across different development environments.

What Can You Configure in a Vagrantfile?

  1. Box Selection: Specify the base box (like ‘ubuntu/bionic64’) that serves as the starting point for your VM.
  2. Provider Configuration: Customize settings for providers like VirtualBox, Hyper-V, or VMware. This can include allocating memory or setting up network configurations.
  3. Network Settings: Configure network options, such as forwarded ports, private networks, or public networks. This configuration defines how the VM connects to the Internet and other machines.
  4. Synced Folders: Define folders that are synchronized between the host machine and the VM, enabling you to work on files on your host machine and have those changes reflected in the VM. This setting is great for developers who are developing locally.
  5. Provisioning: Automate the setup process with scripts that install and configure software within the VM when itโ€™s created or provisioned.

Example of a Basic Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.synced_folder "./host_folder", "/vm_folder"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
  end
end

What are Vagrant providers?

Those familiar with Hashicorp tools will quickly understand the Vagrant provider. It is similar to Terraform providers, etc.

A Vagrant provider is a plugin that allows Vagrant to control and interact with different virtualization platforms. When you run a Vagrant command to manage a VM, the provider translates this command into actions specific to the hypervisor being used. The choice of provider depends on which virtualization solution you are using.

Common Vagrant Providers

  • VirtualBox: The default provider for Vagrant that is popular due to its open-source nature and compatibility with multiple operating systems. It is ideal for development and testing environments.
  • Hyper-V: If you have a Windows desktop Pro license, you have access to client Hyper-V. It provides better integration and performance on Windows systems than VirtualBox.
  • VMware: VMware is usually preferred in production environments. The VMware provider for Vagrant is a separate plugin you must download along with a VMware utility. It offers high performance and advanced features for configuring your virtual machines.

Setting Up a Provider in Vagrant

To use a specific provider, you need to specify it in your Vagrantfile or when you bring up a VM. For instance, to use VirtualBox, the Vagrantfile may include the following configuration:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
  end
end

You can also specify specific providers when bringing up a guest machine, using the –provider flag:

vagrant up --provider=virtualbox

Customizing VMs with Provider-Specific Settings

Each provider allows for certain customizations to the VM. For example, with VirtualBox, you can alter network settings, allocate memory, and set up shared folders. These settings are adjusted in the Vagrantfile, allowing you to tailor the VM to your specific needs.

Why Choose a Specific Provider?

Your choice of provider may be based on the specific environment and hypervisor you have access to. Consider factors like:

  • Compatibility: Ensure the provider supports the OS you’re using.
  • Performance: Some providers offer better performance, like Hyper-V and VMware. However, these are paid solutions (Hyper-V requires Windows Pro license, VMware is a paid license)
  • Features: Different providers come with unique features that may benefit your project.
  • Ecosystem: Using a provider that aligns with your existing tech stack can lead to smoother integration and fewer compatibility issues.

Vagrant Box Commands

The true power of Vagrant lies in its commands. Let’s look at the most common vagrant commands containing core functionality:

  1. vagrant init
    • Initializes a new Vagrant environment. The process creates a new vagrant file which is ready for the vagrant up command. After the file is created, it can be customized.
  2. vagrant up
    • Starts and provisions the Vagrant environment as defined in the Vagrantfile. It can also resume a suspended machine.
  3. vagrant suspend
    • Suspends the machine, saving the current running state and stopping it. This is useful for temporarily pausing your work.
  4. vagrant halt
    • Gracefully shuts down the running machine. Unlike suspend, this turns off the machine and will require a full boot when started again.
  5. vagrant reload
    • Restarts the machine, loading new Vagrantfile configuration changes without having to halt and up the machine again.
  6. vagrant destroy
    • Stops the running machine and destroys all resources that were created during the machine creation process.
  7. vagrant status
    • Displays the current status of the machine, such as running, suspended, or not created.
  8. vagrant ssh
    • Connects to the machine via SSH. It allows admins to connect to the SSH port with the machine’s command line.
  9. vagrant provision
    • Provisions the machine according to the configuration specified in the Vagrantfile. This is typically used to apply changes in the provisioning scripts.
  10. vagrant box add
    • Adds a box to your Vagrant installation, making it available for use in your environments.
  11. vagrant box remove
    • Removes an existing box from your Vagrant installation.
  12. vagrant box update
    • Updates the boxes to the latest version, respecting the constraints in the Vagrantfile.
  13. vagrant box list
    • Lists all the boxes installed on your system.
  14. vagrant share
    • Shares your Vagrant environment with anyone in the world, typically used for collaboration.
  15. vagrant plugin install
    • Installs a Vagrant plugin to extend Vagrant’s functionality.
  16. vagrant plugin uninstall
    • Uninstalls a previously installed Vagrant plugin.
  17. vagrant plugin list
    • Lists all the plugins currently installed in your Vagrant environment.
  18. vagrant push
    • Deploys code in your Vagrant managed machine to a configured destination, useful for deployment workflows.
  19. vagrant snapshot push
    • Takes a snapshot of the current state of the machine. This command allows reverting to the point in time snap later.
  20. vagrant snapshot pop
    • Restores the state of the machine to the last pushed snapshot.
  21. vagrant global-status
    • Provides a global status of all active Vagrant machines across all projects.

VMware provider requires a plugin and utility

To use Vagrant with VMware Fusion or VMware Workstation, you will need to install a couple of software packages. These include:

  • The vmware-desktop plugin
  • The Vagrant VMware Utility

Installing the vmware-desktop plugin

The process to install the vmware-desktop plugin is straightforward.

vagrant plugin install vagrant-vmware-desktop
Install the vagrant vmware desktop plugin
Install the vagrant vmware desktop plugin

Installing the Vagrant VMware Utility

The Vagrant VMware Utility is a separate download you need to download and install. If you don’t install it, in addition to the vmware-desktop plugin, you will see the following error:

Vagrant vmware desktop error without the vagrant vmware utility
Vagrant vmware desktop error without the vagrant vmware utility

You can download the Vagrant VMware Utility here: Install | Vagrant | HashiCorp Developer. Once downloaded, the installation process is a simple “next, next, finish” type install.

Installing the vagrant vmware utility
Installing the vagrant vmware utility

Creating your first VM in seconds with Vagrant init and Vagrant Up

Let’s look at creating your first virtual machine in just a few seconds, using the two most important commands with vagrant, vagrant init and vagrant up.

First, we need to run the vagrant init command. This creates the vagrant file, which is the blueprint for the virtual machine.

In the example below, we are running the command:

vagrant init hashicorp/bionic64
Running the vagrant init command
Running the vagrant init command

After running the vagrant init command, we run the vagrant up command. This command starts the virtual machine, including pulling down files and starting it. If you want to specify a specific provider, you can do that using the –provider flag.

Once you use the command, it will create a new folder with the virtual machine files and other Vagrant-specific files. Not the following commands:

vagrant up --provider vmware_desktop
Running the vagrant up command to bring up the virtual machine 1
Running the vagrant up command to bring up the virtual machine 1

Below, we are logging into the new Vagrant VM, using the default vagrant/vagrant credentials in the VMware Workstation window.

Logging into the ubuntu environment with the vagrant user
Logging into the ubuntu environment with the vagrant user

See the virtual machine in the GUI

If you are using VirtualBox or VMware, you won’t see the VM displayed in the GUI of the solution by default. They run as a headless VM. However, there is a bit of configuration you can add to the vagrant file for this.

Vagrant.configure("2") do |config|
  config.vm.box = "my-box"
  config.vm.provider "vmware_desktop" do |v|
    v.gui = true
  end
end

After adding the configuration, the VM appears in the VMware Workstation GUI after we issue the vagrant up command.

After adding the gui configuration we can see the vagrant vm in the vmware workstation gui
After adding the gui configuration we can see the vagrant vm in the vmware workstation gui

You can also see the status of your Vagrant VM using the command:

vagrant status
Checking the status of your vagrant vm
Checking the status of your vagrant vm

At this point, you can also create an SSH session to your Vagrant box and test connectivity.

Destroying a virtual machine

If you want to get rid of the virtual machine after you are done, you can issue the following command:

vagrant destroy
Destroying a vagrant vm
Destroying a vagrant vm

If you want to get rid of the Vagrant boxes, you can use the commands below:

#Remove the box file for a specific provider
vagrant box remove <box name> --provider <provider name>
#Remove the box file for all providers
vagrant box remove <box name> --all-providers
Removing vagrant boxes for specific providers or all providers
Removing vagrant boxes for specific providers or all providers

Another tool for VM automation

Wrapping up Vagrant Boxes

Vagrant makes the process of creating a virtual machine and management much easier. It is a great tool for developers. DevOps engineers, and home labbers can also benefit from using the tool. Since it can be used with VirtualBox, Hyper-V, and VMware, most users will find the main hypervisors they use for client virtualization are supported. Vagrant boxes are definitely worth trying as they greatly simplify and speed up deploying test instances in your environment.

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.