macOS Sierra: Vagrant Quick Start Guide

This weekend I needed to get a test Linux environment up and running with minimal fuss. After not using a Vagrant managed virtual machine in some time, I decided to take a renewed approach in installing my dependencies via Homebrew.

Open Terminal and follow along!

1. Install Homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Once installed:

brew doctor
brew update

2. Install Virtual Box Cask

brew cask install virtualbox

3. Install Vagrant Cask

brew cask install vagrant

4. Create a Vagrant File

I like to keep my virtual machines in a seperate folder within my home directory, aptly titled “vms”:

mkdir ~/vms

From there, I create a folder per project (replace <project-name> with the actual name of the project:

mkdir ~/vms/<project-name>

Xenial / Vagrant SSH Annoyance Work Around

In the project directory, add a custom Vagrantfile to get around the annoying “ubuntu” SSH issue that is currently happening in 16.04 with Vagrant and to also make the server available on whichever port of your choosing via http://localhost / http://127.0.0.1 (great when using ngrok to expose localhost via a secure tunnel to the Internet):

touch ~/vms/<project-name>/Vagrantfile

In your text editor of choice, open the newly created Vagrantfile and make appropriate edits to your port numbers on both your host and guest machines:

# -*- mode: ruby -*-
# vi: set ft=ruby :
VM_BOX  = 'ubuntu/xenial64'
NETWORK = 'forwarded_port'
GUEST_PORT = 80
HOST_PORT = 9000
Vagrant.configure(2) do |config|
config.vm.box = VM_BOX
config.vm.network NETWORK, guest: GUEST_PORT, host: HOST_PORT
config.vm.provider "virtualbox" do |vb|
vb.memory = 1024
end
config.vm.provision 'shell', inline: <<-SHELL
echo 'ubuntu:ubuntu' | sudo chpasswd
SHELL
end

Also take note that the password for the default ‘ubuntu’ user is changed to ‘ubuntu’ in a less than secure fashion (via this config file) using the shell script provisioning option. This is just a quick and simple way to get around the annoying SSH issue that seems to be impacting the official “ubuntu/xenial64” box. There are other advanced and more secure methods out there using SSH keys, so feel free to investigate as needed.

5. Vagrant Up and SSH

Now with the config file in place, navigate to the virtual machine project directory and run vagrant up

cd ~/vms/<project-name>
vagrant up

After a few minutes the virtual machine will be provisioned and will be accessible using either vagrant ssh (preferred method for access) or over usual ssh via ssh ubuntu@127.0.0.1 -p 2222 with ubuntu/ubuntu as the credentials.

Enjoy what you’ve read?

Be sure to like and share below, or even buy me a coffee ☕️ !

John Foderaro is a JavaScript fanatic located in New Jersey.

Like what you read? Give John Foderaro a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.