Home Use Vagrant with VirtualBox
Post
Cancel

Use Vagrant with VirtualBox

Use Vagrant with VirtualBox

What is Vagrant?

Single workflow to build and manage virtual machine environments - Vagrant is designed for everyone as the simplest and fastest way to create a virtualized environment.

From Vagent

What is VirtualBox?

VirtualBox is a powerful x86 and AMD64/Intel64 virtualization product for enterprise as well as home use. Not only is VirtualBox an extremely feature rich, high performance product for enterprise customers, it is also the only professional solution that is freely available as Open Source Software under the terms of the GNU General Public License (GPL) version 3.

From VirtualBox

Download and Install Vagrant and VirtualBox

  1. Download and Install Vagrant - download link

  2. Download and Install VirtualBox - download link

How to use Vagrant

Install and Specify a Box

Instead of building a virtual machine from scratch, which would be a slow and tedious process, Vagrant uses a base image to quickly clone a virtual machine. These base images are known as “boxes” in Vagrant, and specifying the box to use for your Vagrant environment is always the first step after creating a new Vagrantfile.

Install a Box

If you ran the commands in the last tutorial you do not need to add a box; Vagrant installed one when you initialized your project. Sometimes you may want to install a box without creating a new Vagrantfile. For this you would use the box add subcommand.

You can add a box to Vagrant with vagrant box add. This stores the box under a specific name so that multiple Vagrant environments can re-use it. If you have not added a box yet, do so now. Vagrant will prompt you to select a provider. Type 2 and press Enter to select Virtualbox.

1
vagrant box add generic/ubuntu2004

This will download the box named generic/ubuntu2004 from HashiCorp’s Vagrant Cloud box catalog, where you can find and host boxes.

Use a Box

Now you’ve added a box to Vagrant either by initializing or adding it explicitly, you need to configure your project to use it as a base. Open the Vagrantfile and replace the contents with the following.

1
2
3
Vagrant.configure("2") do |config|
  config.vm.box = "generic/ubuntu2004"
end

Bring up a virtual machine

Run the following from your terminal:

1
vagrant up

In less than a minute, this command will finish and you will have a virtual machine running Ubuntu.

Destroy a virtual machine

Run the following from your terminal:

1
vagrant destroy XXXXXXX

Reference: Vagrant Commands (CLI) (https://developer.hashicorp.com/vagrant/docs/cli)

Prepared Vagrantfile List

About Myself

Please reach out to connect with me via Linkedin.

This post is licensed under CC BY 4.0 by the author.