6.3 And now the easy way
We just set up a Debian server manually using the VirtualBox CLI. This is useful to learn the nuts and bolts behind setting up a virtual machine and using a CLI means we could put these commands into a script file and run them whenever we wanted to create a new machine.
We have not discussed how to automate the installation of Debian itself, this is a topic dealt with in Packer from Scratch[Boo20c], but we don’t need to consider this now as we’re going to turn our attention to the use of Vagrant.
6.3.1 Introduction to Vagrant
Vagrant is a command line tool for managing virtual machines6.
Hang on? Isn’t that what VirtualBox does?
No. VirtualBox provides the facility to run virtual machines, Vagrant manages virtual machines.
Vagrant is capable of managing virtual machines running on several different virtualisation tools; VirtualBox, VMWare, and Hyper-V being the main ones used to run virtual machines on a local host. In Vagrant terminology these virtualisation tools are ‘providers’. Vagrant has a plugin architecture, so developers are free to create their own provider if the existing ones are unsuitable.
Another key concept to Vagrant is the ‘box’. A box is a packaged up base virtual machine that is used by Vagrant to provide one or more virtual machines to your project.
Let’s start defining a Vagrant system. This system will consist of a single virtual machine. We base our virtual machine on a box supplied by the bento project7. Since we started out with a Debian server above let’s continue and set up a Debian server using Vagrant.
To create a Vagrant managed virtual machine we need to write a Vagrantfile. A Vagrantfile is a configuration file used by Vagrant, written in Ruby (a fact that we will exploit later when defining more complex Vagrant setups).
Vagrant can create a Vagrantfile for us as a starting point. This is helpful when starting out but I’m sure you will soon find this template unnecessary, even irritating because of all the comment lines it contains. That said, let’s create our first Vagrantfile using Vagrant.
1mkdir myvagrant 2cd myvagrant 3vagrant init bento/debian-10
Simple as that, the init command instructs Vagrant to initialise a Vagrantfile such that it specifies a virtual machine based on the bento/debian-10 box.
Where does Vagrant find these boxes? By default they are hosted on the HashiCorp Vagrant Cloud server.
Before taking a look at the Vagrantfile let’s start our virtual machine.
1vagrant up
You should now see Vagrant downloading the bento/debian-10 base box, make a clone copy for our project, and start up the new virtual machine. At the end of all this we have a running machine but we are returned to our command line prompt.
As with our manually constructed virtual machine the Vagrant machine is started headless8. We access our new virtual machine using SSH, fortunately Vagrant makes this trivial by supplying the vagrant ssh command.
1vagrant ssh
You should be immediately connected to the Debian virtual machine we just created. Your command line prompt while on this virtual server will be vagrant@debian-10: $
To leave the server (but have it remain running) simply logout.
1exit
You will be returned to your host computer’s command prompt.
6We are using Vagrant as a tool in this book but I have written another book dealing with Vargant in more detail, Vagrant from Scratch[Boo20e]
7The bento project is run by the people who develop the Chef configuration management system. I have found these boxes to be quite sound for my needs.
8Virtual machines can be started with a display, but we almost exclusively want headless servers, so Vagrant’s default behaviour is ideal.