Vagrant
What is Vagrant
Vagrant (by HashiCorp) is a tool that lets you manage and build virtual machine environments in a simple and easy way.
It lowers the development setup time, increases production parity and completely eliminates the situations where something works on one machine but not on the others.
It uses virtual machine providers like VMware, VirtualBox or AWS to name a few, and provisioning tools to install and configure the software on the virtual machine.
For that, it uses a Vagrantfile, which describes the type of machine and the steps to configure it and provision it.
For provisioning the virtual machines, boxes are used. If you know what Docker is, boxes are like Docker images, that you can download from here and use for free.
It allows and makes easy to do cool stuff like folder sharing between the host and the virtual machines.
For more detailed information, you can check the official documentation.
Installation
Check the official webpage.
Examples
For more examples, check this out.
Initialize a virtual machine
The following command will create the Vagrantfile, which contains a default configuration for an Ubuntu bionic system.
vagrant init hashicorp/bionic64
If you have a project with a Vagrantfile, you can initialize it by running:
vagrant init
Start the virtual machine
vagrant up
Suspend the virtual machine
vagrant suspend
Destroy the virtual machine
vagrant destroy
SSH into the machine
vagrant ssh
Install a box to your Vagrantfile
vagrant box add ubuntu/trusty64
List the downloaded boxes
vagrant box list
Remove a downloaded box
vagrant box remove ubuntu/trusty64
Folder sharing
By default, Vagrant shares your project directory to the /vagrant
directory on the guest machine.
Provision a virtual machine
You can write the scripts that will be launched when provisioning the virtual machine. For example, the following is a script that will install the Apache web server in the virtual machine and start serving the files on the project directory:
bootstrap.sh:
#!/usr/bin/env bash
apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
rm -rf /var/www
ln -fs /vagrant /var/www
fi
Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/bionic64"
config.vm.provision :shell, path: "bootstrap.sh"
end
Reload the virtual machine (for updates to the Vagrantfile)
vagrant reload --provision