Installing Your First Vagrant Box
Vagrant is one of those tools that you may have heard about before but just have not used yet, but once you decide to use it, it will probably end up in your tool belt.
The motivation behind using Vagrant is creating reproducible, portable virtual environments and can quickly be brought up and disposed of at will. Now, that sounds like something that developers would love and they certainly do, but IT professionals can leverage Vagrant too, especially in DevOps. For instance, have you ever wanted to quickly bring up a test VM to test out a script on? Well with Vagrant you can do that with a few commands. Even better, Vagrant can create your virtual machines on Virtualbox, VMware, Hyper-V, AWS and more.
In this article, I will walk you through installing your first Vagrant box from Vagrant cloud, which is a public repository of boxes you can install for free. I will demonstrate installing the box on a Mac, but you can use Vagrant with Windows and Linux as well as your host. Since Vagrant boxes are virtual machines, your local platform actually does not matter at all. The intentions of this article is to get your feet a little wet with Vagrant, but as you dive deeper into the tool, you will find you can do so much more.
Installing Vagrant
You can install Vagrant from its official site here, but I prefer to use homebrew on my Mac to install packages when possible. If you have not used homebrew before, I highly recommend.
Dans-MacBook-Pro:CentOS7 dan$ brew install caskroom/cask/vagrant brew cask install caskroom/cask/vagrant Satisfying dependencies Downloading https://releases.hashicorp.com/vagrant/2.0.1/vagrant_2.0.1_x86_64.dmg ######################################################################## 100.0% Verifying checksum for Cask vagrant Installing Cask vagrant Running installer for vagrant; your password may be necessary. Package installers may write to any location; options such as --appdir are ignored. Password: installer: Package name is Vagrant installer: Upgrading at base path / installer: The upgrade was successful. vagrant was successfully installed!
Wow that was easy! Now we just need to install Virtualbox, which we also can do with homebrew:
Dans-MacBook-Pro:~ dan$ brew install caskroom/cask/virtualbox Updating Homebrew... brew cask install caskroom/cask/virtualbox Satisfying dependencies Downloading http://download.virtualbox.org/virtualbox/5.2.0/VirtualBox-5.2.0 ######################################################################## 100.0% Verifying checksum for Cask virtualbox Installing Cask virtualbox Running installer for virtualbox; your password may be necessary. Package installers may write to any location; options such as --appdir are i installer: Package name is Oracle VM VirtualBox installer: Installing at base path / installer: The install was successful. virtualbox was successfully installed!</pre
At this point Vagrant and Virtualbox are installed and we are ready to download and install some boxes.
Installing a CentOS Vagrant Box
Now it is time for some Vagrant magic. Here, I want to install a CentOS virtual machine. A quick search for “CentOS” on Vagrant Cloud leads me to the box here. For the sake of simplicity, I will show you a very quick way to bring up a Vagrant box. First, I create a directory that will hold the Vagrant configuration data. To interact with this particular box with Vagrant commands, you must either use the id or we must be in this directory. I prefer to just work in the directory.
Dans-MacBook-Pro:~ dan$ mkdir CentOS Dans-MacBook-Pro:~ dan$ cd CentOS/
Now, I can initialize the box with the command vagrant init:
vagrant init centos/7
This command creates a file in my working directory called “Vagrantfile”. Inside this Vagrantfile there are just three uncommented lines but keep in mind you can configure many other options such as networking, providers and memory:
Vagrant.configure("2") do |config| config.vm.box = "centos/7" end
This is the quick and dirty way to bring up a box in Vagrant without adding any additional configuration to it.
Now it is time to download and install the box, for this we need just one command vagrant up.