How to Install Vagrant and use it with VirtualBox on Ubuntu 20.04
Vagrant is a command-line tool for building and managing virtual machines. To use Vagrant, you must have a virtualization engine like VirtualBox, Hyper-V, or Docker installed on your system. The Vagrant plugin system also provides support for KVM and VMware. Vagrant is mainly used to easily set- up development environments. Vagrant is available for Linux platforms and users can install it into their system for further usage.
In this article, I will show you how to install Vagrant on an Ubuntu 20.04 machine and use VirtualBox as vairtualization engine. The we will create a VirtualBox virtual machine with CentOS 8 as operating system inside the VM using Vagrant.
Installing Vagrant on Ubuntu
To install Vagrant on Ubuntu 20.04, we will use the command line/ terminal window of our system. At first to we will install the VirtualBox in our system so, for that open up the terminal. Use the Ctl+Alt+T shortcut to open it up or go to ApplicationsàTerminal to access the terminal window.
Once opened, you need to update the apt repository of the system. For that, you required to type the affixed command in the terminal window of the system:
$ sudo apt update
Once you type the command and press the <enter> key, the system will ask for the password of the logged-in user. Provide it in the field and then press the <enter> key.
As soon as you press enter, the updates available in the system currently will begin to process. The process of getting updates might take some time. Make sure to stay in the terminal window during this process. Once done, you need to type the affixed command in order to install the VirtualBox in your system.
$ sudo apt install virtualbox
The installation will begin as shown in the figure below. After some time, the system will prompt and ask user for their permission, type Y and then press enter to continue with the process of installation of VirtualBox in your system.
After sometime, VirtualBox will be installed in your system. You can verify this by searching VirtualBox in the installed Applications of the Ubuntu system. If the application is installed in the system, then you will see an output similar to the one displayed below:
Now, you have installed VirtualBox successfully so, we can proceed further the with installation of Vagrant in the system.
The next step is in this process is to install Vagrant in your system. This is a convenient process and can be done using the single-line command shown below. Type the appended below command in the terminal window to start the installation of Vagrant in your Ubuntu 20.04 system.
$ sudo apt install vagrant
The system will prompt and might ask for the user’s permission during this installation process before proceeding any further. Type Y and then you need to press the <enter> key from the keyboard to continue the process of installation.
The installation phase will be accomplished after a while. Once you have installed Vagrant in your system, type the cited below command in the terminal window to confirm the process of Vagrant installation:
$ vagrant --version
You can see an output that is somewhat comparable to that displayed in this figure. The version of Vagrant installed in the system will be displayed as shown in the figure below:
Create a CentOS 8 VM using Vagrant
Now, we will check out the method of creating a Vagrant project. To do so, we will make a directory for our new vagrant project. Type the affixed command in the command line:
$ mkdir ~/my-vagrant-project
As soon as you press enter, the project directory will be created. Once done, go to the newly created directory by using the cd command. Here, we will be using the command as shown below, you can replace the project name and access the project based on the name you chose for your project. Type the following command:
$ cd ~/my-vagrant-project
You will now enter the project directory. Here is an example, we will be using the vagrant init command to initialize vagrant, along with the box name you want to use. All publicly available Vagrant Boxes can be found on the official catalog page of Vagrant.
$ vagrant init centos/8
The output to the command used in the previous step will be similar to the one shown below:
Now, run the following command in the terminal window:
$ vagrant up
An output similar to the one shown below will appear:
Vagrant will then mount the project directory at /vagrant path in your system’s virtual machine.
Now, we will try to run ssh command in the virtual machine, use the cited below command in the terminal window:
$ vagrant ssh
To stop the virtual machine, type the appended below command in the terminal:
$ vagrant halt
You can also destroy all resources created during the entire process. Type the affixed command in the terminal window:
$ vagrant destroy
This way we can access Vagrant and configure the VirtualBox. Users can destroy and halt the resources as well by using the commands discussed in the previous section.
To uninstall Vagrant from the Ubuntu 20.04 system, type the appended below command in the command line of your system:
$ sudo apt-get remove –auto-remove vagrant
Here, the system might ask for the password of the logged-in user/sudo user. Vagrant will begin to uninstall. If the system prompts for the confirmation of the user, then you need to type Y and then press enter to continue. The uninstallation will be done after a while.
In this article, we went through the steps to install VirtualBox as virtualization engine for Vagrant. We also created a basic project for Vagrant and shared some useful commands to apply to the virtual machine. At last, we talked about the process of uninstalling Vagrant from the system. Users can easily uninstall Vagrant from their systems when they need to.