Setting up a Vagrant Box on Mac
This year I’ll be working with several different operating systems for school. I want to run each of them as natively as I can on my production machine without having to un-boot my production Mac environment. Virtualization is the obvious answer.
If you’re anything like me you’ve always been turned off by the graphical interfaces for Virtualbox and VMWare. Personally, I’ve always preferred to work in a command-line environment anyway. I’m going to use something called vagrant
to boot a pre-configured VM and get me SSH’ed into it.
Vagrant’s typical use it to deploy pre-configured VM environments for development and production virtualized environments. I’ve used it before to get pre-built dev environments for weird platforms set up. Definitely check out their site to learn more about what this can do for you.
Basically, vagrant uses the virtualbox or VMWare engine to run the virtual machines like normal. However it all runs silently in the background. Theoretically you could use this to run whole servers on your machine invisibly, no matter the OS. Vagrant just gives you a uniform interface for these virtual machines (an SSH shell).
Installation and Configuration
- Install
brew
if you haven’t already - (Optional) Hashicorp’s own website has plenty of vanilla virtual environments that you can download and build by default. If you download a virtual machine image from another source, make sure you only get virtual machines from sources you trust.
- Open your favorite terminal and run
brew cask install virtualbox
This will install the command line dependencies of virtualbox. I prefer virtualbox over VMWare personally, but if you want you can install the VMWare dependencies for Vagrant and it should work just as fine. - Once this finishes, go ahead and run
brew cask install vagrant
- Once that nonsense is finished then we need to initialize a new vagrantfile. Make sure you
cd
into the directory where you want your vagrant configuration files stored. They’re invisible (.vagrant) so I just kept mine in my home directory. - To initialize a virtual machine from one of Hashicorp’s preconfigured vanilla boxes, just use the identifier. For now I need an Ubuntu box, so I’ll use
vagrant init ubuntu/xenial64
- Upon starting up the virtual machine for the first time the VM will need to be initialized. This will happen automatically when we start it up for the first time. To fire up the virtual machine, run
vagrant up
- If you are using one from the Hashicorp site, you’ll have to wait for it to download the virtualbox file before it launches. If you installed VMWare instead, it should be downloading the VMWare compatible virtual machine.
- Once the virtual machine is up, if it has been configured with the usual vagrant:vagrant credentials, you should be able to log right in with
vagrant ssh
- From there, you will be SSH’ed in like normal, and can close and open the connection like a normal remote session. Make sure that you use
vagrant down
to shut down your vagrant box when you are done using it.
After that you should be good to go! This tutorial is not comprehensive by any means, so if you have any questions please let me know in the comments and I’ll see what I can do to help!