30 Steps to Vagrant and VMWare Fusion Awesomeness

Vagrant is pretty cool, but of late I've been having real issues finding a Vagrant base box that works with VMware Fusion OOTB. My blogging workflow (used to) involve(s) using a Vagrant box with Jekyll to generate a static site on my local Mac, and then uploading to an S3 bucket. The Vagrant Jekyll boxes weren't up to scratch, so I've decided to figure out how to create my own.

This guide covers how to create the box and add it to your list of boxes, for use in specifying within a Vagrantfile. It doesn't cover Vagrant fundementals, so I assume that you'll have some basic knowledge in how it works before embarking on this magical quest.

  1. Open VMware Fusion

  2. Select “Install from disc or image” and click "Continue"

  3. Locate the ISO for the distribution you want to install, then select “Open”, and click "Continue".

  4. Unselect the checkbox for “Use Easy Install”

  5. Choose “Customize Settings”, and enter a name for the VMware file (name/OSarch, i.e. wegotoeleven/trusty64) and select “Save"

  6. Customise the VM’s settings

    1. Turn on Shared Folders
    2. Set memory to desired size, at least 1024MB
    3. Change the Networking to "Share with my Mac" (aka NAT)
    4. Change the disk size to 40 GB and deselect “Split into 2GB files”
    5. Turn off the sound card
    6. Expand "Advanced USB options" and select "Remove USB Controller"
    7. Untick "Share Mac printers with Linux"
  7. Boot the VM, install the Guest OS and customise the following settings when prompted:

  8. Host Name: Same as the second part of the name of the VMware file (i.e. trusty64)

  9. Full Name: vagrant

  10. User: vagrant

  11. Password: vagrant

  12. Once setup and at the command prompt, login and change the Root Password. When asked, set to vagrant

    $ sudo passwd root
    
  13. Update the OS because updates

    $ sudo apt update && sudo apt upgrade -y
    
  14. Update the Sudoers file with the following to allow the vagrant user sudo rights without having to authenticate with a password

    #Defaults !visiblepw
    Defaults env_keep="SSH_AUTH_SOCK"
    
  15. Test. Is the following command outputs the current directory instead of an error, all is good

    $ sudo pwd
    
  16. Restart VM

    $ sudo shutdown -r now
    
  17. Make ssh folder

    $ mkdir ~/.ssh
    
  18. Set permissions

    $ chmod 700 ~/.ssh
    
  19. Download Vagrant authorised keys

    $ wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O ~/.ssh/authorized_keys
    
  20. Set permissions on authorised keys file

    $ chmod 600 ~/.ssh/authorized_keys && chown -R vagrant ~/.ssh
    
  21. Install OpenSSH

    $ sudo apt install openssh-server -y
    
  22. Edit the SSH config file

    $ sudo nano /etc/ssh/sshd_config
    
  23. Restart SSH service

    $ sudo service ssh restart
    
  24. Install VMware Tools. Begin by mounting the VMware Tools by selecting Virtual Machines > Install VMware Tools from the menu bar menu

    $ sudo mkdir -p /mnt/cdrom
    $ sudo mount /dev/cdrom /mnt/cdrom
    $ tar xzvf /mnt/cdrom/VMwareTools-9.2.2-893683.tar.gz -C /tmp
    $ cd /tmp/vmware-tools-distrib/
    $ sudo ./vmware-install.pl\
    
  25. Wipe the free space on the VM to stop fragmentation

    $ sudo dd if=/dev/zero of=/EMPTY bs=1M
    $ sudo rm -f /EMPTY
    
  26. Shutdown the VM

    $ sudo shutdown -h now
    
  27. Navigate to the location of your vmwarevm. By default this location is ~/Virtual Machines/.

    $ cd ~/Virtual Machines/wegotoeleven:xenial64.vmwarevm
    

    Note: Any forward slashes (/) will be translated into colons if the name of the VMware file contains a forward slash

  28. Create a file named metadata.json and enter the following contents:

    {
      "provider": "vmware_fusion"
    }
    
  29. Create a file named Vagrantfile and enter the following contents.

    # -*- mode: ruby -*-
    # vi: set ft=ruby
    
    Vagrant.configure("2") do |config|
      config.vm.provider :vmware_fusion do |v, override|
        v.gui = false
      end
    end
    
  30. We next want to optimize the box to reduce it’s size:

    $ /Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager -d Virtual\ Disk.vmdk
    $ /Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager -k Virtual\ Disk.vmdk
    
  31. Finally compress the box:

    $ tar cvzf package.box ./
    
  32. Add the box to Vagrant

    $ vagrant box add wegotoeleven/xenial64 package.box
    

I couldn't have done this without the following blog posts:

And yeah. I know I said 30.

Show Comments