Vagrant is a popular command-line tool that enables you to easily build and manage virtual machines on Linux, Windows, and macOS. It is commonly used by developers to create test and production environments within a very short time. Vagrant requires a hypervisor to provision VMs on top of it and Virtualbox is the default provider for vagrant. It can use other hypervisors and providers such as VMware, Hyper-V, Docker, KVM and AWS.
Some of the key features of Vagrant include:
- Virtual machine management: Vagrant creates and manage virtual machines using virtualization software such as VirtualBox, VMware, and Hyper-V.
- Simple configuration: Vagrant uses a single configuration file (Vagrantfile) to define and manage the virtual environment.
- Support for multiple platforms: Vagrant supports multiple operating systems such as Linux, Windows, and macOS.
- Provisioning and configuration: Vagrant allows developers to easily provision and configure their virtual environment with tools such as Ansible, Chef, and Puppet.
- Testing in multiple environments: As a Developer you can test your code in multiple environments, reducing the risk of compatibility issues using Vagrant.
- Version control integration: Vagrant integrates seamlessly with popular version control systems such as Git.
In this guide, we are going to look at how to install and use Vagrant on Ubuntu Linux system using VirtualBox as the provider.
How To Install Vagrant on Ubuntu
it is a straightforward process to install and configure Vagrant on Ubuntu Linux system. Just complete the following few simple steps and you’ll have Vagrant ready for use on your Ubuntu system.
But before we get started let’s update the system.
sudo apt update && sudo apt -y upgrade
[ -f /var/run/reboot-required ] && sudo reboot -f
Step 1: Install Virtualbox
Import GPG key for VirtualBox repository packages.
#Download
curl https://www.virtualbox.org/download/oracle_vbox_2016.asc | gpg --dearmor > oracle_vbox_2016.gpg
curl https://www.virtualbox.org/download/oracle_vbox.asc | gpg --dearmor > oracle_vbox.gpg
#Install on system
sudo install -o root -g root -m 644 oracle_vbox_2016.gpg /etc/apt/trusted.gpg.d/
sudo install -o root -g root -m 644 oracle_vbox.gpg /etc/apt/trusted.gpg.d/
Next we add the actual packages repository into the system.
echo "deb [arch=amd64] http://download.virtualbox.org/virtualbox/debian $(lsb_release -sc) contrib" | sudo tee /etc/apt/sources.list.d/virtualbox.list
If you do not have Virtualbox already installed on your Ubuntu, run the below commands to install
sudo apt update
sudo apt install linux-headers-$(uname -r) dkms
sudo apt install virtualbox-7.0
Step 2: Installing Vagrant
Vagrant packages are available on the OS official APT repository. Add the repo by running the following commands:
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
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
Then install Vagrant package using apt
package manager.
sudo apt update && sudo apt install vagrant
Confirm the installation by checking on the version
$ vagrant --version
Vagrant 2.3.7
I also had to install another dependency for Vargant boxes to run.
sudo apt install libarchive-dev libarchive-tools
Step 3: Using Vagrant with VirtualBox on Ubuntu
To start using vagrant, create a working directory to place all your vagrant content
mkdir Vagrant
cd Vagrant
For a start, you can download a test Vagrant Box. Let’s go for Ubuntu 24.04.
$ vagrant box add generic/ubuntu2404
==> box: Loading metadata for box 'generic/ubuntu2404'
box: URL: https://vagrantcloud.com/generic/ubuntu2404
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
==> box: Adding box 'generic/ubuntu2404' (v4.2.8) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/generic/boxes/ubuntu2404/versions/4.2.8/providers/virtualbox.box
box: Calculating and comparing box checksum...
==> box: Successfully added box 'generic/ubuntu2404' (v4.2.8) for 'virtualbox'!
Once downloaded, you need to initialize to create a Vagrant file that will be used for running the virtual machine
$ vagrant init generic/ubuntu2404
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.
Now run ‘vagrant up’ to create your virtual environment
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'generic/ubuntu2004'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'generic/ubuntu2404' version 'v4.2.8' is up to date...
==> default: Setting the name of the VM: Vagrant_default_1616158622148_36131
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM…
You should notice the VM created and running in your virtualbox
The VM can be accessed through Virtualbox or ssh using vagrant CLI as below:
$ vagrant ssh
vagrant@ubuntu2404:~$
You can create your own Vagrantfiles to run virtual machines of your choice. Check example below
vim Vagrantfile
Add the below content
# -*- 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 = true
end
end
Next, run ‘vagrant up’
$ vagrant up
Bringing machine 'centos-01' up with 'virtualbox' provider...
==> centos-01: Box 'centos/7' could not be found. Attempting to find and install...
centos-01: Box Provider: virtualbox
centos-01: Box Version: >= 0
==> centos-01: Loading metadata for box 'centos/7'
centos-01: URL: https://vagrantcloud.com/centos/7
==> centos-01: Adding box 'centos/7' (v2004.01) for provider: virtualbox
That’s it. You have successfully installed Vagrant on Ubuntu and created your first virtual environments. I hope the guide has been useful and enjoy using Vagrant!. Contact us for any support related inquiries and we will be happy to help.
Also check out: Running Docker Containers using Vagrant