How to Install, Deploy, and Uninstall Vagrant on Ubuntu 22.04

Introduction
Vagrant is a software application that usesĀ virtualization technology to create an operating system environment. From the CLI, Vagrant loads, prepares, and launches a virtual VagrantBox environment.
This guide will show you how to install Vagrant on Ubuntu 22.04.


Prerequisites
- Ubuntu 22.04 (this tutorial works for version 20.04 as well).
- Sudo privileges.
- VirtualBox installed.
Install VirtualBox
To install Vagrant, first get aĀ cloud (virtualization) hypervisor. Any virtual machine, such asĀ KVM,Ā Docker,Ā VMware, or VirtualBox, is able to fulfill this purpose. This example uses VirtualBox.
VirtualBox is a virtualization toolĀ that allows users to create and manipulate several OSs as virtual machines. Since Vagrant creates virtual operating systems, it needs a tool like VirtualBox to manage them.Ā
To install VirtualBox, run:
sudo apt install virtualbox -y


Install Vagrant
There are several ways to installĀ VagrantĀ on Ubuntu 22.04. The following text explains each option.Ā
Note:Ā Run sudo apt update && sudo apt upgrade before installation.
Option 1: Install Vagrant with apt
The simplest way to install Vagrant is with theĀ apt package manager. Run the following:
sudo apt install vagrant


Verify the installation with:
vagrant --version


Option 2: Install Vagrant With the Binary PackageĀ
Sometimes, the software version in the official repositories is not as recent as on the developerās website. Find the latest version by checking theĀ developerās download page.
At the time of the writing of this article, the Vagrant version is 2.3.7. To download and install the latest version:
1. Run theĀ wgetĀ command:
wget https://releases.hashicorp.com/vagrant/2.3.7/vagrant_2.3.7-1_amd64.deb


The command downloads the 2.3.7-1_amd64.deb package.Ā
Note: Before Vagrant 2.3.0, the Debian package file was named x86_64.deb. This changed to amd64.deb starting with Vagrant 2.3.0. This aligns with the commonly used architecture label amd64 for 64-bit systems.
2. Navigate to the directory Vagrant is downloaded in and runĀ dpkg:
sudo dpkg -i vagrant_2.3.7-1_amd64.deb


3. Verify the version:
vagrant --version


Deploy Vagrant On Ubuntu 22.04
The next part is to deploy Vagrant. Follow these steps:
1. Create a projectĀ directoryĀ using the following syntax:
mkdir ~/project_name
For example, create a vagrant-ubuntu project
mkdir ~/vagrant-ubuntu
The command has no output
2. UseĀ cdĀ to navigate to the created directory:
cd ~/vagrant-ubuntu


The command has no output but shows the terminal is working from the new directory.
3. Visit theĀ Vagrant official website and choose a VagrantBox. For example, select and copy the ubuntu/trusty64 box name.
4. Initialize the process with the init command:
vagrant init ubuntu/trusty64


The init command initializes a Vagrant project. It also creates a Vagrant configuration file, Vagrantfile, which contains the ubuntu/trusty64 box configuration.
5. Next, create and configure the virtual machine according to the Vagrantfile with:
vagrant up


The command downloads the box (if not already downloaded) and creates a virtual machine based on it. It appends the selected box to the VirtualBox environment. The output shows the new virtual machineās default SSH address, username, and authentication method.
6. To SSH into a running Vagrant virtual machine, run:
vagrant ssh


The command establishes an SSH session into the running virtual machine, giving users access to the shell.
Ā Another option is to access the VirtualBox on the system.


Other Vagrant Commands
Once connected, multiple commands work with Vagrant
For instance, to stop the virtual machine, run:
vagrant halt


To delete the virtual machine, execute:
vagrant destroy


The command destroys any work inside the virtual OS. To verify the success, run:
vagrant status:


Note: To learn what else Vagrant does, check our complete guide to starting with Vagrant.
Uninstall Vagrant on Ubuntu 22.04
ToĀ uninstallĀ Vagrant, run the remove command:
sudo apt remove vagrant


Use autoremove to remove both the software and its dependencies:
sudo apt autoremove vagrant


To remove Vagrant, its dependencies, and configuration files, use autoremove and purge:
sudo apt autoremove --purge vagrant


Conclusion
After reading this article, you know how to install and configure Vagrant on your Ubuntu system.
Next, learn how to do the same on CentOS.



