In a previous blog - You Are Missing a Lot By Not Having a Virtual Lab, I mentioned that I personally use Virtualbox + Vagrant for my own virtual lab. Today I’m going to talk about how to install these two applications. But first, let’s describe what Virtualbox and Vagrant is.
Virtualbox is a virtualization software. You install it on top of your OS/Operating System (Virtualbox can be installed on Windows/Mac/Linux). From there you then can install other Operating Systems on a virtual machine created through Virtualbox and you’re essentially running an OS on top of your existing OS. This is how virtualization works, but to be specific this is how virtualization using a Type 2 Hypervisor works (a Type 2 Hypervisor like Virtualbox runs on top of an existing OS while Type 1 runs directly on top of hardware). Your existing OS will be the host OS and the one running on top of it will be the guest OS. You can create as many virtual machines and install as many guest OS as you want as long as your computer resources can handle it.
Vagrant is a tool you can use to build and manage virtual machine environments. It can help you provision virtual machines in Virtualbox without ever touching Virtualbox (Virtualbox needs to be running of course). Vagrant supports other virtualization platforms such as VMware and cloud providers such as AWS. These are all called providers in Vagrant terms. Vagrant gives you a single workflow to deal with to set up your virtual environment. You use a single configuration file to define all your virtual machines. You can bring them up, restart or delete them all from Vagrant. You can also use provisioning tools to automate the installation of any required software or initial configuration for the guest OS after they are brought up. This helps you achieve a consistent and repeatable environment that you can deploy anywhere. All you need is the Vagrant configuration file and you can spin up your virtual environment again, even if you move it to a different computer.
That’s it for our Virtualbox and Vagrant primer. Let’s now proceed to how we can install them.
Step 1. First, let’s download Virtualbox as it will be our prerequisite before we can use Vagrant. Go to the Virtualbox downloads page here. Choose the download option based on the Operating System you are using right now. At the time of this writing, the current Virtualbox version is 5.2.18.
Step 2. After your download is finished then install Virtualbox. It’s usually a typical wizard-based install (except Linux) so it should be straightforward and easy. Just keep on clicking next until it’s finished. Try to launch Virtualbox after the installation to test if it has been properly installed.
Step 3. Then let’s download Vagrant. Go to the Vagrant downloads page here. Again, choose the download option based on the Operating System you are using right now. At the time of this writing, the current Vagrant version is 2.1.5.
Step 4. After your download is finished then install Vagrant. It’s also usually a typical wizard-based install (except Linux) so it should straightforward and easy.
Now that everything is installed, let’s try to launch our first virtual machine through Vagrant.
Step 5. Create a folder where we will place our Vagrant configuration file. Basically, this is where our little virtual environment project will live. Name it whatever you want, it could be anything, it’s just a simple folder. In my case, I’ll be naming it virtual-lab.
NOTE: I'm on a Mac OS X using the terminal so I'm using the commands such as mkdir (applicable to Linux/Unix type Operating Systems). If you are on Windows or using the GUI then create a folder in the same way you normally do.
Step 6. On the command line change your directory into your newly created folder then let’s initialize a Vagrant file.
cd virtual-lab vagrant init ubuntu/xenial64
The vagrant init command creates a Vagrant file in your current folder. This Vagrant file contains your Vagrant configuration. Vagrant files contain information about your virtual machine/s and you declare other specific details and provisioning steps that you would want for your virtual machines. For now, our Vagrant file is simple and we will keep it as it is. The ubuntu/xenial64 in the command simply tells vagrant to create the Vagrant file with this Vagrant box. It's a virtual machine packaged into a vagrant box which we could just easily download to our machine. It’s ready to use and all we need to do is launch it from Vagrant.
NOTE: The ubuntu/xenial64 vagrant box is the official vagrant box release for Ubuntu Linux 16.04 LTS (named Xenial Xerus).
You can view the contents of the newly created Vagrant file and you should see a bunch of stuff (mostly comments at this point) but the most important parts would the lines the same as below.
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/xenial64” end
Step 7. Run vagrant up to launch the virtual environment. Since this is the first time you’ll be using the ubuntu/xenial64 vagrant box it will first download it from vagrantcloud and once done it will immediately bring up the box.
Step 8. Once the box is up you can now login to it using vagrant ssh.
You should now be logged in and it will look similar to this.
Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-116-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage Get cloud support with Ubuntu Advantage Cloud Guest: http://www.ubuntu.com/business/services/cloud 0 packages can be updated. 0 updates are security updates. New release '18.04.1 LTS' available. Run 'do-release-upgrade' to upgrade to it. vagrant@ubuntu-xenial:~$
You can check the status of the virtual machine using vagrant status.
vagrant status Current machine states: default running (virtualbox)
You can reload the virtual machine using vagrant reload.
You can shut down the virtual machine using vagrant halt.
Finally you can delete the virtual machine using vagrant destroy.
That’s it! You have successfully installed and tested Virtualbox + Vagrant. From this point you should be able to play around and test out other Vagrant boxes. To look for other Vagrant box options available you can check it out here.
Here's another example for Centos 7 Linux. Create another folder and perform the same steps as we did above.
vagrant init centos/7 vagrant up
Technically you can just create a file named Vagrantfile as long as it is inside your intended folder and edit it to have the contents as below.
Vagrant.configure("2") do |config| config.vm.box = "centos/7" end
The config.vm.box line defines which vagrant box you'll be using. As you can see it's using the same vagrant box name "centos/7" as we did in the vagrant init command.