Installation of Vagrant on the latest Ubuntu

In this article, I’m going to guide you on the installation of Vagrant on Ubuntu.

A vagrant is open-source software that we can use to create and manage virtual machines. It mainly focuses on the automation of VM deployment. Vagrant takes very little time to make ready for the virtual machine. It is very easy to configure and reproduce.

I’m going to install Vagrant on Ubuntu 18.04.3/20.04 desktop. and I will install CentOS 7 on a virtual machine with the help of Vagrant.

Preconditions for Vagrant

  • A computer which Ubuntu 18.04 or 20.04 is running.
  • A user with sudo privilege or a root user
  • Internet connection.

Step 1: Installation of virtual box

As Vagrant depends on the hypervisors, we have to install anyone like Virtual Box, VMware, libvert, HyperV. For this tutorial example, we are going to install Virtual Box. I always suggest installing the latest version of Virtual Box. To download it, you can go to the virtual box download page by clicking on the link Virtual Box Linux Download page. and click on Ubuntu 18.04 / 18.10 / 19.04, or Ubuntu 19.10/20.04. It will start downloading. Once download will complete. Double-click on the downloaded package, it will bring a page where it gives the option to install. Click on install here and virtual box installation done.

Step 2: Installation of Vagrant

Now, this is the time to install Vagrant. I always prefer to download it from the vagrant website. To go on the download page, click on the link Vagrant Download Page. Here, select the version, like we are installing on Ubuntu, so click on Debian and select the release; it will start downloading it.

Once download will be completed. Go to the downloaded directive and run the below command.

sudo dpkg -i vagrant*.deb 

It may ask your user password, enter the password.

You can also download from a single command through apt. But it may be the older version of Vagrant. I don’t recommend it.

sudo apt install vagrant

It may ask your user password, and for confirmation so enter the password and press y and hit the Enter key.

Step 3: Deployment of VM

As we have set up the environment, this is the time to deploy the Centos 7 on Virtual Box. So let’s start it. Open the terminal again and type the below command.

vagrant box add centos/7 

So, it will ask to select the hypervisor, as we are working on virtual box, then it is 3 but you should check yours before pressing 3 or 4. then hit Enter to continue. You can also have a look at the picture below.

vagrant installation
Adding vagrant box centos

It will take some time, that depends on your Internet speed. After completing the successful installation, you will get the message for successfully added.

Creating a directory to create a vagrant file settings

Firstly, we are going to create a directory. So, you can use the below command to create the directory.

sudo mkdir ~/centos7 

Secondly, after creating the directory, let’s go inside this directory with the below command.

cd ~/centos7 

Creating the Vagrant file

So, now you have to create a file, I am using vi editor. But, you can use any other editors like nano, vim, or as per your choice. There is a simple command for that, you can find that command below.

sudo vi Vagrantfile

and copy the below lines and paste it in this file

# -*- 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 = false
config.vm.network "private_network", ip: "192.168.56.10"
end
end

Important Note: The subnet must be different; otherwise, you will get errors.

Turning on the Vagrant machine

This is the time to up the machine. So, you can use the below command to turn it on.

sudo vagrant up 

Also when you up the machine you will get the screen like the below picture.

Turning on vagrant
Turning on the vagrant

Vagrant ssh

As we have been installed and now you can ssh,

sudo vagrant ssh 

As a result, you can see the below screen.

ssh vagrant
ssh vagrant

Useful commands to manage Vagrant 

Below are the few commands which are helpful for Vagrant access.

Firstly, if you want to shutdown the VM then you can find the below command helpful.

sudo vagrant halt 

Secondly, if you want to hibernate the VM then you can use the below command.

sudo vagrant suspend 

Lastly, if you want to destroy the VM. So use the command below.

sudo vagrant destroy
destroy a machine
Destroying a vagrant instance

A list of the common Vagrant command you can see in the below picture

its argument
vagrant command and its argument

Conclusion

In this tutorial, you learned about the installation of Vagrant on Ubuntu. So, I hope you understand, but, if you have any questions, you can ask in the comment section.

Also, you can read about the below topics.

Leave a Reply

Your email address will not be published.