This tutorial assumes you have a basic knowledge of the terminal of your respective system.
Vagrant is a powerful tool for building and maintaining and sharing virtual enviroments e.g. for VirtualBox, Hyper-V, Docker containers, VMware, and AWS. The native language for Vagrant is Ruby but it can be used to run shell scripts and other provision tools during your builds.
To download: https://www.vagrantup.com/downloads.html.
A much more in depth tutorial for Vagrant is available here: thttps://www.vagrantup.com/intro/getting-started/
This will be a, hopefully, quick tutorial on getting started with getting your first Vagrant build running.
Install:
During the install Vagrant should get added to your computer's enviroment variables, if it's not sometimes logging out and logging back in will fix the issue.
To verify that it is installed typing "vagrant" in the terminal should return its usage.
First Project:
Make a new directory and navigate to that directory in the terminal, once inside typing the command "vagrant init" will place a Vagrantfile in your directory which shows many options that can be used to create your VM. You are free to use this default file created by init if so skip to Running Your Build, the next steps will show you some basics on how to create your own Vagrantfile with a couple additional configuration steps: set up a shell script to install some tools on your system and do network configurations. If you are planning on following all of these steps in creating your own file I would still strongly recommend looking through the file created by the init command for some of the configuration options available to you.
Creating a Vagrantfile:
Choose your favorite text editor and create a new file called "Vagrantfile", there is no file extension but it does use ruby syntax highlighting. For this tutorial we will be building a VM with Ubuntu 18 as the OS, Vagrant keeps the OS images on their servers and they can all be found here https://app.vagrantup.com/boxes/search, these contain the basic Vagrantfiles for building these different boxes
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/bionic64" end
Above is all that is needed to create a 64 bit Ubuntu version 18 VM, version 18 is called Bionic Beaver hence bionic64.
Additional Provisioning:
Vagrant allows you to run additional provisioning through many tools, shell scripts, ansible playbooks, docker images, ... https://www.vagrantup.com/docs/provisioning/, for a list of all available.
For the purposes of this tutorial we are going to focus on the simplest which is running shell scripts, which can be very useful created enviroments that can be replicated, ie you can create a Vagrantfile with shells scripts that will allow another person to run your codebase the exact same way as you through the use of a few console commands.
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/bionic64" config.vm.provision "shell", inline: "echo Installing Python" config.vm.provision "shell", inline: "sudo apt install python3" end
Running Scripts:
#!/bin/bash echo Installing Python sudo apt install python3
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/bionic64" config.vm.provision "shell", path: "script.sh" end
Both of the above will do the same thing, after your vagrant build installs your Ubuntu os it will echo Installing Python and then install python 3.
Running Your Build:
To start your virtual machine run the "vagrant up" command, the first time you do this, and any time you do this after running destroy will take a bit of time as it needs to install the image of the OS. Then once it is finished installing vagrant ssh will log you into the default box, more than one VM can be created with the same Vagrantfile to long into specific a specific box use "vagrant ssh VM_NAME", where VM_NAME is the name of the box you want to log into.
Below are some additional commands that can be used. Note these will not be run inside the VM, to exit the VM back into the directory where it was created use the "exit" command.
Basic Vagrant Commands: (should be run within the directory containing the Vagrantfile)
$ vagrant ssh
SSH into virtual machine.
$ vagrant up
Start virtual machine.
$ vagrant halt
Halt virtual machine.
$ vagrant destroy
Destroy your virtual machine. The source code and the content of the data directory will remain unchanged. Only the VirtualBox machine instance will be destroyed. You can build your machine again with the 'vagrant up' command. This command is useful if you want to save disk space.
$ vagrant provision
Reconfigure the virtual machine after a source code change.
$ vagrant reload
Reload the virtual machine. Useful when you need to change network or synced folder settings.
Vagrant cheat sheet: https://gist.github.com/wpscholar/a49594e2e2b918f4d0c4
Additional tutorials available on Vagrant website: https://www.vagrantup.com/intro/getting-started/index.html
Additional Vagrant documentation: https://www.vagrantup.com/docs/