How To Create an On-Demand VM using Vagrant and VirtualBox
For today’s How-To, we will be learning how to create on-demand testing and development virtual machines using freely available, industry-standard tools. Now, I know nearly everyone reading this post is already familiar with how to manually set up a VM, but the primary benefits of using a tool such as Vagrant are its integration with automation/configuration management tools and its online repository of pre-configured images. Prepare to be amazed when you realize that you can literally run one command to download, configure, and spin up a VM running any OS of your choice… It really is pretty incredible and has a wide variety of applications, both for at home testing and training, as well as enterprise level application development, evaluation, and compatibility testing. Please note that this guide is intended to be a primer/introduction to Vagrant and will not cover more advanced topics such as customizing the configuration files, automating the provisioning process, or uploading a Vagrant Box for others to use. For more information on those features, please check out the Vagrant knowledge base at https://www.vagrantup.com/docs!
Before We Get Started
Please keep in mind that in order to follow along with this guide, you will need the following prerequisites in place:
Knowledge of basic computing/networking concepts, including virtualization
A computer with specifications meeting basic virtualization requirements (processor that supports virtualization, at least 8Gb of RAM, suitable disk space)
Installation files for VirtualBox: https://www.virtualbox.org/
Installation files for Vagrant: https://www.vagrantup.com/downloads.html
If you’re already familiar with VirtualBox and Vagrant and just looking for a quick refresher, please see the following quick steps:
Download and install Virtualbox.
Download and install Vagrant.
Find the Vagrant Box of your choice: https://app.vagrantup.com/boxes/search
From an elevated command prompt or terminal, run ‘vagrant init <name of vagrant box>’. E.g., ‘vagrant init ubuntu/trusty64’.
From an elevated command prompt or terminal, run ‘vagrant up’ to download and start the VM.
Connect to your new VM via SSH on port 2222 or RDP (if it’s a Windows box) using the local address of 127.0.0.1.
The default credentials are vagrant and vagrant. Be sure to change them ASAP!
Use ‘vagrant halt’ to gracefully shut down the VM; use ‘vagrant destroy’ to delete the VM.
Alright, so now that we have that out of the way, let’s get right to it! Complete the following steps to quickly and easily create, configure, and start a virtual machine with the Operating System of your choice. While this guide will be walking through how to create a VM running Ubuntu, please take advantage of the thousands of community-created Vagrant Boxes available.
Download and install Virtualbox from https://www.virtualbox.org/. Click the bright green download button and install the version that fits your Operating System. The default installation should do the trick, so let’s see how quickly you can click Next, Next, Next, Finish!
2. Download and install Vagrant from https://www.vagrantup.com/downloads.html. Be sure to select the correct version for your Operating System. Again, go ahead and take the installation defaults.
3. Find a Vagrant Box from https://app.vagrantup.com/boxes/search. Vagrant offers a multitude of community curated ‘Vagrant Boxes’. These boxes contain evaluation copies of OSs, applications, and configurations for nearly any situation you can dream up. Need a Linux box pre-configured for Apache? It’s there. Want to play with Windows Server 2016 to prepare for a certification exam? Done. Tired of busting out your 2012 Macbook Air to test an app? Just spin up a Vagrant Box with a test copy of Mojave. For this example, we will be selecting ubuntu/trusty64.
4. Click on your desired Vagrant Box and navigate to the code excerpt under the ‘New’ tab. This section contains the commands you will need to run in a command line or terminal to download and start your VM.
5. Open an elevated command prompt or terminal window and type the first command listed under the New code excerpt referenced above: vagrant init <name of vagrant box>. This command will create a ‘vagrantfile’ referencing the name of the Vagrant Box you would like to create and start.
6. From the elevated command prompt or terminal, type the next command: vagrant up. Vagrant will now download the image, build the VM in VirtualBox, and start it in headless mode when ready. Please note that if this is the first time starting this Vagrant Box, it may take some time for the files to download.
6. And that’s it! Once the command completes, the VM will be ready to use! Depending on what kind of Vagrant Box you spun up, you can interact with it in different ways, including SSH, RDP, and direct console via VirtualBox.
Please note that the VM will be accessible using a local IP address of 127.0.0.1 with the following default credentials:
As you can see below, in this example I connected to my Linux box using PuTTY and SSH.
6. Once logged in, please make sure to change the default username and password to something more secure (hopefully I didn’t need to tell you that, but better safe than sorry!)
7. Once finished with the VM, spin it down by running the ‘vagrant halt’ command. This will execute a graceful shutdown of the guest OS and power down the VM, saving any changes.
8. If completely finished with the VM and ready to remove it (thus deleting any associated files), simply run the ‘vagrant destroy’ command.
Final Thoughts and wrap-up
To wrap things up, I would like to point out the following:
As this guide is intended to act as a starter/primer for using Vagrant, please review the documentation here for more in-depth tutorials, best practices, and usage cases: https://www.vagrantup.com/docs/index.html
While spinning up a VM using the ‘vagrant up’ command is cool, the true power of Vagrant is that way it can leveraged by automation and configuration management tools such as Ansible, Chef, Salt, Puppet, etc. It really opens the door to automating daily devops tasks as it provides a CLI interface for managing virtual machines. Again, this is outside the scope of this guide, but I encourage everyone to explore this, especially those of you working as IT professionals.
Please keep Vagrant in mind when preparing for your technical certification exams and/or building evaluation home labs. It really is helpful to spin up an on-demand environment with a command, SSH to it for practice purposes, and then delete the files to free up resources when finished.
As always, please ask any questions in the comments section and share this guide with your friends!