Provisioning a Virtual Machine in Vagrant

Samuel Theophilus
Backend Developers
Published in
5 min readDec 20, 2023
Vagrant-VirtualBox Architecture

Provisioning in the context of a virtual machine is the process of setting up configured development environments for a certain purpose eg — a web server, database, etc.

Vagrant, an automation tool allows you to create, manage, and provision virtual machines(VM). It simplifies the repeated tedious and time-consuming process of configuring virtual machines through its automated provisioning features.

Characteristics of provisioning

  • Configuration and customization for specific use cases: provisioning focuses on tailoring the VM for specific needs. For example, A virtual machine can be provisioned with web server software eg Apache, Nginx
  • Installation of additional software and applications
  • Setting up user accounts and permissions

Provisioning in Vagrant

Provisioning in Vagrant makes it possible to install packages and change configurations as part of the vagrant up process. Vagrant boxes are not built for specific use cases, hence before provisioning existed you would have to SSH into your guest machine and install the packages you need.

In the next section, you will learn how to automatically setup Apache2 on Ubuntu virtual machine using Vagrant.

Automated Apache2 setup on Ubuntu VM

Prerequisites

  • Install the latest version of Vagrant.
  • Install Oracle VirtualBox.
  • Basic understanding of Linux.
  • You’ll need access to the terminal, On Windows, use Git Bash. On Linux, you can use your default terminal.
  • A code editor of your choice. I recommend Visual Studio Code.

STEPS

In your terminal create a directory named webserver.

$ mkdir /f/vagrant-vms/webserver
$ cd /f/vagrant-vms/webserver

Step 1: Initialize virtual machine

$ vagrant init ubuntu/jammy64
  • vagrant init : This command initiates a new VM.
  • ubuntu/jammy64: This is the name of the Vagrant box you want to use.

This command creates a Vagrantfile in the project directory.

Step 2: Configuring the virtual machine

The Vagrantfile is a Ruby script used to configure and set up the Virtual Machine. We’ll be making adjustments and removing informational comments in several sections of the file using a text editor. Remember to save after all changes have been made.

  1. Open Visual Studio Code in the same directory.
$ code .
Visual Studio Code — A code editor.
  • Add config.vm.box_download_insecure = true. This solves a recent download bug associated with Vagrant Ubuntu boxes.
Vagrant.configure("2") do |config|
# ... other configuration

config.vm.box = "ubuntu/jammy64"
config.vm.box_download_insecure = true

Step 3: Configuring private network access

# ... other configuration

# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.56.14"
  • By specifying the private_network, the Vagrant assigns a host-only network. This means the network is only accessible by the host machine.

Step 4: Configuring the public network access

# ... other configuration

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
config.vm.network "public_network"
  • This connects the virtual machine to your physical network i.e. your router IP.

Step 5: Configuring VM resources

# ... other configuration

config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
vb.memory = "1600"
vb.cpus = "2"
end
  • vb.memory = “1600”: This line allocates 1600MB of RAM to your virtual machine.
  • cb.cpus = “2”: This line allocates 2 CPU cores to your VM. You can increase this value based on the specifications of your host machine.

Step 6: Provisioning the VM

# ... other configuration

# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2
SHELL
end

The VM is provisioned using a shell script as it is more beginner-friendly and easier to understand.

  • config.vm.provision "shell", inline: <<-SHELL: This line tells Vagrant to use a shell script for provisioning. inline indicates the script is provided in the Vagrantfile itself instead of referencing an external shell script.
  • apt-get update && apt-get install -y apache2: This is the actual shell script that gets executed on the VM. apt-get update downloads the recent package list available. apt-get install -y apache2 installs Apache with the -y flag to answer yes to any prompts that may occur during installation.

That concludes the configuration and provisioning of the virtual machine. You can download the complete Vagrantfile here.

Step 7: Booting the VM

  1. Use vagrant up to create the virtual machine
$ vagrant up

Vagrant automatically provisions the virtual machine on the execution of this command.

2. SSH into the guest machine.

$ vagrant ssh

3. Switch to the root user and get the IP address of your VM.

$ sudo -i
$ ip addr show

Copy the IP address. On your host machine, use your preferred browser and paste the copied IP address.

Note: The IP address is the same as the private network IP you entered in your Vagrantfile.

Apache2 Default Page

This is the Apache2 Default Page. This indicates that your web server has been automatically set up on the virtual machine.

Conclusion

In this article, you learned about automatically provisioning virtual machines with Vagrant. Provisioning is a great concept in DevOps as it deals with fast automation and configuration of virtual machines. It saves time and allows you to focus on other components of the delivery process. By following similar steps you can set up more than a web server, even a complete application.

If you enjoyed this article, let’s connect on Linked-In.

--

--