Building Your First VM With Vagrant

In this lesson, we'll learn about Vagrant Cloud and build our first VM with Vagrant.

Vagrant Cloud

Now that we are done with the installation, all we need to do is run a Vagrant VM. We need to find an image, which, using Vagrant, will run a Virtual Machine. We can do so by going to the Vagrant Cloud website: Vagrant Cloud.

Remember: we’ve installed VirtualBox as the virtualization software. So, we need an image, or boxes (that’s how Vagrant refers to their images), compatible with VirtualBox in order to build our first VM. We can see that there’s a ‘provider’ mentioned on the Vagrant Cloud website below. Use this to find images according to your needs.

Provider from the Vagrant Cloud
Provider from the Vagrant Cloud

How does this all work?

Let’s have a look at the image below to understand how Vagrant lets you create and run VMs within a couple of minutes.

Vagrant VM creation workflow
Vagrant VM creation workflow
  1. Vagrant pulls an image from the Vagrant Cloud. For example: ubuntu, centos, alpine, etc.

  2. Vagrant creates a Vagrantfile from an image and stores it in the local system. The Vagrantfile consists of the configuration of the VM, which is then used to boot up the VM.

  3. The user then runs the Vagrant VM with the help of the vagrant up command

Now, select the provider, which, in our case, is VirtualBox. After selecting the provider you’ll notice that all boxes compatible with VirtualBox are listed below. You can choose any box you want; however, in this lesson, we’ll be choosing ubuntu/xenial64.

Run your virtual machine

From this point onwards, we will be looking into Ubuntu-based commands only.

Open up your terminal and create your very first Vagrant VM.

  1. Create a Working Directory.
$ mkdir vagrant
  1. Move into the Directory.
$ cd vagrant
  1. Now, initialize your Vagrant box.
$ vagrant init ubuntu/xenial64

Output

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
  1. Spin up your VM
$ vagrant up

Output

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'ubuntu/xenial64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'ubuntu/xenial64'
    default: URL: https://vagrantcloud.com/ubuntu/xenial64
==> default: Adding box 'ubuntu/xenial64' (v20200822.0.0) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20200822.0.0/providers/virtualbox.box
    default: Download redirected to host: cloud-images.ubuntu.com
  1. Do the SSH
$ vagrant ssh

Output

Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-187-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage


0 packages can be updated.
0 updates are security updates.

New release '18.04.5 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


vagrant@ubuntu-xenial:~$ 

If you’re following along in the local environment, that’s great!

Give it a go!