The ease of creating and destroying the virtual machine instances on the fly makes it more prominent in many production environments. Because of the popularity of VMs, in this article, I am going to explain how to install Vagrant on Ubuntu 20.04.
What is Vagrant?
If you are a first time user, then you might want to know what Vagrant is, in the first place?
Vagrant is a command-line tool built to create and manage virtual machines. Out of the box, Vagrant supports VirtualBox, Hyper-V, and Docker. But, Vagrant has the ability to manage other types of machines as well, such as KVM (libvirt), VMware and AWS through Vagrant plugin system.
It has pre-configured boxes (the OS images, in simple word) a vagrantfile to configure a VM. More on this is out of the scope of this page but I will cover those in a vagrant specific page.
How to Install Vagrant on Ubuntu 20.04?
[alert color=”yellow”]I will show you by installing a CentOS 8 on top of Virtualbox. So, make sure that you have already installed VirtualBox on Ubuntu 20.04.[/alert]Vagrant can be installed from the Ubuntu’s repository but that is not recommended as has a very old version.
So, we will be downloading the latest Vagrant from the official download page.
Download Vagrant:
[alert color=”red”]As of writing this guide Vagrant 2.2.9 was the latest. But you make sure to visit the above download page to get the latest one![/alert]Download the latest Vagrant using wget or curl command:
wget https://releases.hashicorp.com/vagrant/2.2.9/vagrant_2.2.9_x86_64.deb
(or)
curl -O https://releases.hashicorp.com/vagrant/2.2.9/vagrant_2.2.9_x86_64.deb
Install Vagrant:
Once the file is downloaded, execute the below command to install it:
sudo apt install ./vagrant_2.2.9_x86_64.deb
Verify Vagrant Installation
To verify if it is installed successfully, execute the below command:
vagrant --version
Output:
Vagrant 2.2.9
Install Linux Distro using Vagrant on Ubuntu 20.04
It is recommended that you create a project directory and then work on that directory for any Vagrant installations etc.
Execute the below commands to create and cd to the project directory:
mkdir ~/vagrant-projects cd ~/vagrant-projects
Initialize Vagrantfile
Now, initialize a vagrantfile with a specific box that you want to use.
[alert color=”green”]Usually, Boxes are related to distro. VM and are provider specific. You can browse through all available Vagrant boxes.[/alert]In this demo I will use the CentOS 7 box to install:
vagrant init centos/7
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.
You can open the Vagrantfile, and re-configure it as per your project needs.
Now, execute the below command to create and configure the virtual machine:
vagrant up
Output:
==> default: Configuring and enabling network interfaces... default: SSH address: 192.168.10.56:22 default: SSH username: vagrant default: SSH auth method: private key ==> default: Rsyncing folder: /home/cpqlinux/Vagrant/vagrant-projects/ => /vagrant
Vagrant mounts the project directory on the host machine at /vagrant
in the newly created virtual machine. So, you do not need to create a shared directory for virtual machines to work on your VM project’s files on your host machine.
==> default: Rsyncing folder: /home/cpqlinux/Vagrant/vagrant-projects/ => /vagrant
Other Useful Vagrant commands
Execute the below to login to the VM via SSH:
vagrant ssh
To shutdown the virtual machine execute the below command:
vagrant halt
To destroy all resources created during the creation of the machine, execute below command:
vagrant destroy
That’s on installing Vagrant on Ubuntu 20.04 that helps in creating, managing and destroying virtual machines. For more information, you can always visit the official Vagrant docs page.