Introduction

This post will cover the process of installing Vagrant with the libvirt provider on Ubuntu 1804.

For reference the following software will be used in this post.

  • Ubuntu - 1804
  • Vagrant - 2.2.9
  • Vagrant-libvirt - 0.1.2

System Prep

Before we begin, lets ensure the host is updated.

cmd

sudo apt update -y && sudo apt upgrade -y
    

Installation

Download Vagrant.

cmd

wget https://releases.hashicorp.com/vagrant/2.2.9/vagrant_2.2.9_x86_64.deb

# output
--2020-08-15 00:37:08--  https://releases.hashicorp.com/vagrant/2.2.9/vagrant_2.2.9_x86_64.deb
Resolving releases.hashicorp.com (releases.hashicorp.com)... 151.101.97.183, 2a04:4e42:7::439
Connecting to releases.hashicorp.com (releases.hashicorp.com)|151.101.97.183|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 42978828 (41M) [application/x-debian-package]
Saving to: ‘vagrant_2.2.9_x86_64.deb’

vagrant_2.2.9_x86_64.deb      100%[=================================================>]  40.99M  11.2MB/s    in 3.7s

2020-08-15 00:37:12 (11.1 MB/s) - ‘vagrant_2.2.9_x86_64.deb’ saved [42978828/42978828]
    

Install Vagrant.

cmd

sudo dpkg -i vagrant_2.2.9_x86_64.deb

# output
Selecting previously unselected package vagrant.
(Reading database ... 67128 files and directories currently installed.)
Preparing to unpack vagrant_2.2.9_x86_64.deb ...
Unpacking vagrant (1:2.2.9) ...
Setting up vagrant (1:2.2.9) ...
    

Verify Vagrant installation.

cmd

which vagrant
# output
/usr/bin/vagrant

vagrant --version
# output
Vagrant 2.2.9
    

Install the vagrant-libvirt dependencies

cmd

sudo apt install -y qemu libvirt-bin ebtables dnsmasq-base
sudo apt install -y libxslt-dev libxml2-dev libvirt-dev zlib1g-dev ruby-dev
    

Install the vagrant-libvirt plugin

cmd

vagrant plugin install vagrant-libvirt

# output
Installing the 'vagrant-libvirt' plugin. This can take a few minutes...
Fetching excon-0.76.0.gem
Fetching formatador-0.2.5.gem
Fetching fog-core-2.2.0.gem
Fetching fog-json-1.2.0.gem
Fetching mini_portile2-2.4.0.gem
Fetching nokogiri-1.10.10.gem
Building native extensions. This could take a while...
Fetching fog-xml-0.1.3.gem
Fetching ruby-libvirt-0.7.1.gem
Building native extensions. This could take a while...
Fetching fog-libvirt-0.7.0.gem
Fetching vagrant-libvirt-0.1.2.gem
Installed the plugin 'vagrant-libvirt (0.1.2)'!
    

Configuration

Add your user to the libvirt group.

cmd

sudo usermod -aG libvirt $USER
    

Important

Re-login to have the group applied to your user.

Create a vagrant-libvirt network using an xml file named vagrant-libvirt.xml with the below contents.

file

<network connections='1'>
  <name>vagrant-libvirt</name>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr1' stp='on' delay='0'/>
  <ip address='192.168.121.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.121.2' end='192.168.121.254'/>
    </dhcp>
  </ip>
</network>
    

Define and start the network.

cmd

virsh net-define vagrant-libvirt.xml
virsh net-start vagrant-libvirt
virsh net-autostart vagrant-libvirt
    

Testing

Create a test folder and Vagrantfile

cmd

mkdir -p ~/vagrant/centos-test && cd ~/vagrant/centos-test && vagrant init centos/7
  
# output
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.
    

The vagrant init command will create the following Vagrantfile

file

# Comments removed for berevity.
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
end
    

Now confirm that you can build a Vagrant box with the vagrant up command.

cmd

vagrant up

# output
Bringing machine 'default' up with 'libvirt' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
    default: Box Provider: libvirt
    default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
    default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v2004.01) for provider: libvirt
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/libvirt.box
Download redirected to host: cloud.centos.org
    default: Calculating and comparing box checksum...
==> default: Successfully added box 'centos/7' (v2004.01) for 'libvirt'!
==> default: Uploading base box image as volume into Libvirt storage...
==> default: Creating image (snapshot of base box volume).
==> default: Creating domain with the following settings...
==> default:  -- Name:              centos-test_default
==> default:  -- Domain type:       kvm
==> default:  -- Cpus:              1
==> default:  -- Feature:           acpi
==> default:  -- Feature:           apic
==> default:  -- Feature:           pae
==> default:  -- Memory:            512M
==> default:  -- Management MAC:
==> default:  -- Loader:
==> default:  -- Nvram:
==> default:  -- Base box:          centos/7
==> default:  -- Storage pool:      default
==> default:  -- Image:             /var/lib/libvirt/images/centos-test_default.img (41G)
==> default:  -- Volume Cache:      default
==> default:  -- Kernel:
==> default:  -- Initrd:
==> default:  -- Graphics Type:     vnc
==> default:  -- Graphics Port:     -1
==> default:  -- Graphics IP:       127.0.0.1
==> default:  -- Graphics Password: Not defined
==> default:  -- Video Type:        cirrus
==> default:  -- Video VRAM:        9216
==> default:  -- Sound Type:
==> default:  -- Keymap:            en-us
==> default:  -- TPM Path:
==> default:  -- INPUT:             type=mouse, bus=ps2
==> default: Creating shared folders metadata...
==> default: Starting domain.
==> default: Waiting for domain to get an IP address...
==> default: Waiting for SSH to become available...
    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: Rsyncing folder: /home/bradmin/vagrant/centos-test/ => /vagrant

# SUCCESS !!!!
    

Clean Up

Kill the Vagrant box

cmd

vagrant destroy -f

# output  
==> default: Removing domain...
    

Delete Vagrant .deb file.

cmd

rm ~/vagrant_2.2.9_x86_64.deb
    

Summary

If you followed along, Vagrant and the vagrant-libvirt plugin are now installed in your Ubuntu 1804 host and you can successfully build Vagrant boxes.

vagrant linux