Vagrant is a command-line tool for easily building and managing virtual machines. It is commonly used by developers to create test and production environments within a very short time. Vagrant requires a hypervisor to provision VMs on top of it and Virtualbox is the default provider for vagrant. It can use other hypervisors and providers such as VMware, Hyper-V, Docker, KVM and AWS.

In this guide, we are going to look at how to install and use Vagrant on Ubuntu 20.04 using VirtualBox as the provider.

Step 1: Install Virtualbox

If you do not have Virtualbox already installed on your Ubuntu 20.04, run the below commands to install

sudo apt-get update
sudo apt-get install virtualbox

Step 2: Install Vagrant on Ubuntu 20.04

Vagrant is already available in Ubuntu 20.04 repository but since it is not regularly update, we are going to download and install the latest version with wget. You may have to check the latest release in the link before downloading.

wget https://releases.hashicorp.com/vagrant/2.2.14/vagrant_2.2.14_linux_amd64.zip

Unzip the downloaded file and copy vagrant to a directory included in system PATH

unzip vagrant_2.2.14_linux_amd64.zip
sudo cp vagrant /usr/local/bin/

Confirm the installation by checking on the version

$ vagrant --version
Vagrant 2.2.14

I also had to install another dependency for Vargant boxes to run.

sudo apt-get install libarchive

Step 3: Using Vagrant with VirtualBox on Ubuntu 20.04

To start using vagrant, create a working directory to place all your vagrant content

mkdir Vagrant

For a start, you can download a test Vagrant Box. Let’s go for Ubuntu 20.04

$ cd Vagrant
$ vagrant box add generic/ubuntu2004
==> box: Loading metadata for box 'generic/ubuntu2004'
    box: URL: https://vagrantcloud.com/generic/ubuntu2004
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) libvirt
3) parallels
4) virtualbox
5) vmware_desktop

Enter your choice: 4
==> box: Adding box 'generic/ubuntu2004' (v3.2.10) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/generic/boxes/ubuntu2004/versions/3.2.10/providers/virtualbox.box
Download redirected to host: vagrantcloud-files-production.s3-accelerate.amazonaws.com
    box: Calculating and comparing box checksum...
==> box: Successfully added box 'generic/ubuntu2004' (v3.2.10) for 'virtualbox'!

Once downloaded, you need to initialize to create a Vagrant file that will be used for running the virtual machine

$ vagrant init generic/ubuntu2004
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.

Now run ‘vagrant up’ to create your virtual environment

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'generic/ubuntu2004'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'generic/ubuntu2004' version '3.2.10' is up to date...
==> default: Setting the name of the VM: Vagrant_default_1616158622148_36131
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM…

You should notice the VM created and running in your virtualbox

You can access the VM through Virtualbox or ssh using Vagrant as below:

$ vagrant ssh 
[email protected]:~$ 

You can create your own Vagrantfiles to run virtual machines of your choice. Check example below

vim Vagrantfile

Add the below content

# -*- mode: ruby -*-
# vi: set ft=ruby :

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'

Vagrant.configure("2") do |config|
  ##### DEFINE VM #####
  config.vm.define "centos-01" do |config|
  config.vm.hostname = "centos-01"
  config.vm.box = "centos/7"
  config.vm.box_check_update = true
  end
end

Next, run ‘vagrant up’

$ vagrant up
Bringing machine 'centos-01' up with 'virtualbox' provider...
==> centos-01: Box 'centos/7' could not be found. Attempting to find and install...
    centos-01: Box Provider: virtualbox
    centos-01: Box Version: >= 0
==> centos-01: Loading metadata for box 'centos/7'
    centos-01: URL: https://vagrantcloud.com/centos/7
==> centos-01: Adding box 'centos/7' (v2004.01) for provider: virtualbox

That’s it. You have successfully installed Vagrant on Ubuntu 20.04 and created your first virtual environments. I hope the guide has been useful and enjoy using Vagrant! Check more interesting guides below: