Saturday, November 16, 2024
Google search engine
HomeGuest BlogsHow To Install Vagrant on Ubuntu 22.04|20.04|18.04

How To Install Vagrant on Ubuntu 22.04|20.04|18.04

In this guide, I’ll take you through different ways to install the latest release of Vagrant on Ubuntu 22.04/20.04/18.04 Linux distributions. Vagrant is an open source tool for building and managing virtual machine environments in an easy-to-use single workflow. Vagrant focuses on automation, lowering development environment setup time and increasing production parity.

Vagrant works with VirtualBox, KVM, Hyper-V, Docker containers, VMware, and AWS. The software is written in Ruby and actively developed by HashiCorp.

Setup Pre-requisite

Note that Vagrant depends on existing hypervisor on your system, this
can be VirtualBox, KVM, or VMware. We have the following guides to help
you with the installation of these hypervisors

Install Vagrant on Ubuntu 22.04|20.04|18.04

The other method of installing Vagrant on Debian and its derivatives is from an apt repository.

Install repository addition dependencies:

sudo apt update
sudo apt -y install apt-transport-https ca-certificates curl software-properties-common

Import repository GPG keys:

wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg

Add the official Vagrant APT repository to your system:

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list

Once the repo is added, proceed to install vagrant:

sudo apt update
sudo apt install vagrant

Installation should take few seconds to complete:

Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease
Get:2 http://mirrors.digitalocean.com/ubuntu focal InRelease [265 kB]
Hit:3 https://apt.releases.hashicorp.com focal InRelease
Hit:4 http://mirrors.digitalocean.com/ubuntu focal-updates InRelease
Hit:5 http://mirrors.digitalocean.com/ubuntu focal-backports InRelease
Fetched 265 kB in 1s (340 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  vagrant
0 upgraded, 1 newly installed, 0 to remove and 7 not upgraded.
Need to get 107 MB of archives.
After this operation, 258 MB of additional disk space will be used.
Get:1 https://apt.releases.hashicorp.com jammy/main amd64 vagrant amd64 2.3.4 [107 MB]
Fetched 107 MB in 3min 11s (561 kB/s)
Selecting previously unselected package vagrant.
(Reading database ... 272897 files and directories currently installed.)
Preparing to unpack .../vagrant_2.3.4_amd64.deb ...
Unpacking vagrant (2.3.4) ...
Setting up vagrant (2.3.4) ...
Scanning processes...
Scanning candidates...
Scanning processor microcode...
Scanning linux images...

Using Vagrant on Ubuntu 22.04|20.04|18.04

After the installation, you can check the version:

$ vagrant --version
Vagrant 2.3.4

Download a test Vagrant Box. In this example, I’ll download Kali Linux:

$ vagrant box add offensive-security/kali-linux

To Download Ubuntu 22.04 Vagrant image, use:

$ vagrant box add generic/ubuntu2204

Choose hypervisor to download the box

==> box: Loading metadata for box 'generic/ubuntu2204'
    box: URL: https://vagrantcloud.com/generic/ubuntu2204
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) libvirt
3) parallels
4) virtualbox
5) vmware_desktop

Enter your choice: 4

To launch a VM using Vagrant, you’ll need to create a Vagrantfile. The primary function of the Vagrantfile is to describe the type of machine required for a project, and how to configure and provision a Virtual Machine.

mkdir boxes && cd boxes
touch Vagrantfile

Below is an example of Vagrantfile content

# -*- mode: ruby -*-
# vi: set ft=ruby :

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'

Vagrant.configure("2") do |config|
  ##### DEFINE VM #####
  config.vm.define "ubuntu-01" do |config|
  config.vm.hostname = "ubuntu-01"
  config.vm.box = "generic/ubuntu2204"
  config.vm.box_check_update = true
  end
end

Bring up the VM by running:

$ vagrant up

Then ssh to the instance with

$ vagrant ssh

To shutdown VM, use:

$ vagrant halt

Hibernate VM

$ vagrant suspend

Set VM to initial state by cleaning all data

$ vagrant destroy

That’s all, enjoy using Vagrant and read more on Vagrant Documentation link.

Also read:

RELATED ARTICLES

Most Popular

Recent Comments