How To Install Vagrant on Ubuntu 20.04

In this tutorial we learn how to install Vagrant on Ubuntu 20.04. We will also create a Vagrantfile, start, access and destroy the VM.

Introduction

In this tutorial, we learn how to install Vagrant on Ubuntu 20.04. Vagrant is a command line utility for managing the life cycle of virtual machine. We can use vagrant to provision virtual machine on our computer / laptop using virtualization like VirtualBox, VMWare, Hyper-V or Libvirt (KVM) and also on cloud providers like Amazon Web Services (AWS). For this tutorial we will use Vagrant with VirtualBox

Prerequisites

Install Vagrant on Ubuntu 20.04

First, let’s add Hashicorp GPG key. This key will be used by apt to verify that the package we’re downloading is the Vagrant package created and published by Hashicorp.

curl -fsSL http<span class="hljs-variable">s:</span>//apt.releases.hashicorp.<span class="hljs-keyword">com</span>/gpg | sudo apt-key <span class="hljs-built_in">add</span> -

It should provide output OK. If it provides any error message you have to fix the error first before continue to the next step.

Secondly, add the vagrant apt repository using the command below.

sudo apt-<span class="hljs-keyword">add-repository <span class="hljs-string">"deb [arch=amd64] https://apt.releases.hashicorp.com <span class="hljs-variable">$(lsb_release -cs)</span> main"</span></span>

After adding repository configuration, we need to update apt database using the command below.

sudo apt-<span class="hljs-built_in">get</span> <span class="hljs-keyword">update</span>

Now we can install Vagrant using the command below.

sudo apt-<span class="hljs-keyword">get</span> install vagrant

The output will be similar to below.


Reading package lists<span class="hljs-params">...</span> Done
Building dependency tree       
Reading state information<span class="hljs-params">...</span> Done
The following <span class="hljs-literal">NEW</span> packages will be installed:
  vagrant
<span class="hljs-number">0</span> upgraded, <span class="hljs-number">1</span> newly installed, <span class="hljs-number">0</span> <span class="hljs-keyword">to</span> remove <span class="hljs-literal">and</span> <span class="hljs-number">129</span> <span class="hljs-literal">not</span> upgraded.
Need <span class="hljs-keyword">to</span> get <span class="hljs-number">43</span>,<span class="hljs-number">9</span> MB of archives.
After this operation, <span class="hljs-number">123</span> MB of additional disk space will be used.
Get:<span class="hljs-number">1</span> https:<span class="hljs-comment">//apt.releases.hashicorp.com focal/main amd64 vagrant amd64 2.2.17 [43,9 MB]</span>
Fetched <span class="hljs-number">43</span>,<span class="hljs-number">9</span> MB <span class="hljs-keyword">in</span> <span class="hljs-number">13</span>s (<span class="hljs-number">3.459</span> kB/s)                                                                                                    
Selecting previously unselected package vagrant.
(Reading database <span class="hljs-params">...</span> <span class="hljs-number">175165</span> files <span class="hljs-literal">and</span> directories currently installed.)
Preparing <span class="hljs-keyword">to</span> unpack <span class="hljs-params">...</span>/vagrant_2<span class="hljs-number">.2</span><span class="hljs-number">.17</span>_amd64.deb <span class="hljs-params">...</span>
Unpacking vagrant (<span class="hljs-number">2.2</span><span class="hljs-number">.17</span>) <span class="hljs-params">...</span>
Setting up vagrant (<span class="hljs-number">2.2</span><span class="hljs-number">.17</span>) <span class="hljs-params">...</span>

Check Vagrant version

Now vagrant is installed we can check installed vagrant version using the command below.

$ vagrant <span class="hljs-built_in">version</span>
Installed Version: <span class="hljs-number">2.2</span><span class="hljs-number">.17</span>
Latest Version: <span class="hljs-number">2.2</span><span class="hljs-number">.17</span>

You're <span class="hljs-built_in">running</span> an up-<span class="hljs-keyword">to</span>-<span class="hljs-built_in">date</span> <span class="hljs-built_in">version</span> <span class="hljs-keyword">of</span> Vagrant!

Using Vagrant

In this section we will use the installed Vagrant to spawn Ubuntu 20.06 virtual machine.

<span class="hljs-built_in">mkdir</span> focal64
<span class="hljs-built_in">cd</span> focal

Creating Vagrantfile for Ubuntu 20.04

To create new Vagrantfile for Ubuntu 20.04 we use vagrant init command.

<span class="hljs-attribute">vagrant</span> init ubuntu/focal64

The output will be similar to below.

A `Vagrantfile` has been placed <span class="hljs-keyword">in</span> this <span class="hljs-built_in">directory</span>. You are now
ready <span class="hljs-built_in">to</span> `vagrant up` your <span class="hljs-keyword">first</span> virtual environment! Please <span class="hljs-built_in">read</span>
<span class="hljs-keyword">the</span> comments <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> Vagrantfile <span class="hljs-keyword">as</span> well <span class="hljs-keyword">as</span> documentation <span class="hljs-keyword">on</span>
`vagrantup.com` <span class="hljs-keyword">for</span> more information <span class="hljs-keyword">on</span> <span class="hljs-title">using</span> <span class="hljs-title">Vagrant</span>.

Starting VirtualBox virtual machine with Vagrant

To start the virtualmachine, we can use the command below

<span class="hljs-attribute">vagrant up</span>

Since this is the first time we start Ubuntu 20.04 virtual machine, we will get output similar to output below:

Bringing machine 'default' up with 'virtualbox' provider...
=<span class="ruby">=> <span class="hljs-symbol">default:</span> Box <span class="hljs-string">'ubuntu/focal64'</span> could not be found. Attempting to find and install...
</span>    default: Box Provider: virtualbox
    default: Box Version: >= 0
=<span class="ruby">=> <span class="hljs-symbol">default:</span> Loading metadata for box <span class="hljs-string">'ubuntu/focal64'</span>
</span>    default: URL: https://vagrantcloud.com/ubuntu/focal64
=<span class="ruby">=> <span class="hljs-symbol">default:</span> Adding box <span class="hljs-string">'ubuntu/focal64'</span> (v20210709.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>) for <span class="hljs-symbol">provider:</span> virtualbox
</span>    default: Downloading: https://vagrantcloud.com/ubuntu/boxes/focal64/versions/20210709.0.0/providers/virtualbox.box
Download redirected to host: cloud-images.ubuntu.com
=<span class="ruby">=> <span class="hljs-symbol">default:</span> Successfully added box <span class="hljs-string">'ubuntu/focal64'</span> (v20210709.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>) for <span class="hljs-string">'virtualbox'</span>!
</span>=<span class="ruby">=> <span class="hljs-symbol">default:</span> Importing base box <span class="hljs-string">'ubuntu/focal64'</span>...
</span>=<span class="ruby">=> <span class="hljs-symbol">default:</span> Matching MAC address for NAT networking...
</span>=<span class="ruby">=> <span class="hljs-symbol">default:</span> Checking if box <span class="hljs-string">'ubuntu/focal64'</span> version <span class="hljs-string">'20210709.0.0'</span> is up to date...
</span>=<span class="ruby">=> <span class="hljs-symbol">default:</span> Setting the name of the <span class="hljs-symbol">VM:</span> focal64_default_1626598171361_67481
</span>=<span class="ruby">=> <span class="hljs-symbol">default:</span> Clearing any previously set network interfaces...
</span>=<span class="ruby">=> <span class="hljs-symbol">default:</span> Preparing network interfaces based on configuration...
</span>    default: Adapter 1: nat
=<span class="ruby">=> <span class="hljs-symbol">default:</span> Forwarding ports...
</span>    default: 22 (guest) => 2222 (host) (adapter 1)
=<span class="ruby">=> <span class="hljs-symbol">default:</span> Running <span class="hljs-string">'pre-boot'</span> VM customizations...
</span>=<span class="ruby">=> <span class="hljs-symbol">default:</span> Booting VM...
</span>=<span class="ruby">=> <span class="hljs-symbol">default:</span> Waiting for machine to boot. This may take a few minutes...
</span>    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    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...
=<span class="ruby">=> <span class="hljs-symbol">default:</span> Machine booted and ready!
</span>=<span class="ruby">=> <span class="hljs-symbol">default:</span> Checking for guest additions in VM...
</span>=<span class="ruby">=> <span class="hljs-symbol">default:</span> Mounting shared folders...
</span>    default: /vagrant => /home/howtodojo/focal64

SSH to vagrant box

To access the vagrant box we can use the command below:

<span class="hljs-attribute">vagrant ssh</span>

We will be greeted as if we are accessing an Ubuntu machine via SSH

$ vagrant ssh
Welcome to Ubuntu <span class="hljs-number">20.04</span><span class="hljs-number">.2</span> LTS (GNU/Linux <span class="hljs-number">5.4</span><span class="hljs-number">.0</span><span class="hljs-number">-77</span>-generic x86_64)

 * <span class="hljs-string">Documentation:</span>  <span class="hljs-string">https:</span><span class="hljs-comment">//help.ubuntu.com</span>
 * <span class="hljs-string">Management:</span>     <span class="hljs-string">https:</span><span class="hljs-comment">//landscape.canonical.com</span>
 * <span class="hljs-string">Support:</span>        <span class="hljs-string">https:</span><span class="hljs-comment">//ubuntu.com/advantage</span>

  System information <span class="hljs-keyword">as</span> of Sun Jul <span class="hljs-number">18</span> <span class="hljs-number">09</span>:<span class="hljs-number">27</span>:<span class="hljs-number">51</span> UTC <span class="hljs-number">2021</span>

  System <span class="hljs-string">load:</span>  <span class="hljs-number">0.0</span>               <span class="hljs-string">Processes:</span>               <span class="hljs-number">111</span>
  Usage of /:   <span class="hljs-number">3.3</span>% of <span class="hljs-number">38.71</span>GB   Users logged <span class="hljs-string">in:</span>         <span class="hljs-number">0</span>
  Memory <span class="hljs-string">usage:</span> <span class="hljs-number">19</span>%               IPv4 address <span class="hljs-keyword">for</span> <span class="hljs-string">enp0s3:</span> <span class="hljs-number">10.0</span><span class="hljs-number">.2</span><span class="hljs-number">.15</span>
  Swap <span class="hljs-string">usage:</span>   <span class="hljs-number">0</span>%


<span class="hljs-number">1</span> update can be applied immediately.
To see these additional updates <span class="hljs-string">run:</span> apt list --upgradable


vagrant<span class="hljs-meta">@ubuntu</span>-<span class="hljs-string">focal:</span>~$

To exit from the virtual machine you can type exit or press CTRL+D.

Destroying Vagrant Virtual Machine

To destroy the Vagrant managed virtual machine, we can use the command below

<span class="hljs-attribute">vagrant destroy</span>

Vagrant will prompt to ensure we want to destroy the virtual machine.

   <span class="hljs-keyword">default</span>: Are you sure you want <span class="hljs-keyword">to</span> destroy the <span class="hljs-string">'default'</span> VM? [y/N]

Enter Y

Summary

In this tutorial we learn how to install Vagrant on Ubuntu 20.04. We also learn the basic usage of Vagrant, from creating a Vagrantfile, starting up the instance, accessing the instance through SSH and destroying the instance.