Catalog / Vagrant Cheatsheet

Vagrant Cheatsheet

A comprehensive cheat sheet for Vagrant, covering essential commands, Vagrantfile configuration, and best practices to streamline your virtual environment development.

Getting Started with Vagrant

Installation and Setup

Install Vagrant:

# Example for Debian/Ubuntu
sudo apt-get update
sudo apt-get install vagrant

# Example for macOS (using Homebrew)
brew install vagrant

Verify Installation:

vagrant --version

Install VirtualBox (if not already installed):

Vagrant relies on a provider like VirtualBox or VMware. VirtualBox is a common open-source option.

# Example for Debian/Ubuntu
sudo apt-get install virtualbox

# Example for macOS (using Homebrew)
brew install virtualbox

Basic Vagrant Commands

vagrant init

Initializes a new Vagrant environment by creating a Vagrantfile in the current directory.

Example: vagrant init hashicorp/precise64

vagrant up

Starts the Vagrant virtual machine. It reads the Vagrantfile and provisions the VM accordingly.

Example: vagrant up

vagrant ssh

Connects to the Vagrant virtual machine via SSH.

Example: vagrant ssh

vagrant halt

Stops the running Vagrant virtual machine gracefully.

Example: vagrant halt

vagrant suspend

Suspends the Vagrant virtual machine, saving its current state to disk.

Example: vagrant suspend

vagrant resume

Resumes a suspended Vagrant virtual machine.

Example: vagrant resume

Adding a Box

Adding a box is how Vagrant knows what OS template to use. Official boxes can be found on HashiCorp’s Atlas.

vagrant box add <box_name> <url>

Example:
vagrant box add precise64 http://files.vagrantup.com/precise64.box

Vagrantfile Configuration

Vagrantfile Basics

The Vagrantfile is a Ruby script that describes the configuration of your virtual machine.
It is located in the root directory of your Vagrant project.

Basic Vagrantfile structure:

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
end

Networking

Port Forwarding

Forwards a port from the host machine to the guest machine.

config.vm.network "forwarded_port", guest: 80, host: 8080

Private Network (Static IP)

Configures a static IP address for the guest machine on a private network.

config.vm.network "private_network", ip: "192.168.33.10"

Public Network (Bridged)

Bridges the guest machine to your host’s network, giving it an IP address on your local network.

config.vm.network "public_network"

Provisioning

Shell Provisioning

Runs a shell script to automate the setup of the guest machine.

config.vm.provision "shell", path: "script.sh"

Ansible Provisioning

Uses Ansible to provision the guest machine.

config.vm.provision "ansible" do |ansible|
  ansible.playbook = "playbook.yml"
end

Puppet Provisioning

Uses Puppet to provision the guest machine.

config.vm.provision "puppet" do |puppet|
  puppet.manifests_path = "manifests"
  puppet.module_path = "modules"
end

Advanced Vagrant Features

Synced Folders

Synced folders allow you to share files between your host machine and the Vagrant guest machine. By default, the Vagrantfile directory is synced to /vagrant in the guest.

Configuring synced folders:

config.vm.synced_folder "./data", "/var/data"

NFS Synced Folders:

For better performance, especially on macOS, you can use NFS synced folders.

config.vm.synced_folder "./data", "/var/data", type: "nfs"

Multiple Machines

Vagrant allows you to define and manage multiple virtual machines within a single Vagrantfile.

Defining multiple machines:

Vagrant.configure("2") do |config|
  config.vm.define "web" do |web_config|
    web_config.vm.box = "hashicorp/precise64"
    web_config.vm.network "forwarded_port", guest: 80, host: 8080
  end

  config.vm.define "db" do |db_config|
    db_config.vm.box = "hashicorp/precise64"
    db_config.vm.network "private_network", ip: "192.168.33.20"
  end
end

Accessing specific machines:

vagrant ssh web
vagrant halt db

Box Management

vagrant box list

Lists all installed boxes.

Example: vagrant box list

vagrant box remove

Removes a specified box from your system.

Example: vagrant box remove hashicorp/precise64

vagrant box update

Checks for updates for installed boxes and installs them.

Example: vagrant box update

Troubleshooting and Tips

Common Issues

Networking Conflicts:
Ensure that the ports you are forwarding are not already in use on your host machine. Change the host port in your Vagrantfile.

Provider Issues:
Make sure your provider (VirtualBox, VMware) is correctly installed and configured. Check the Vagrant documentation for provider-specific troubleshooting steps.

Synced Folder Permissions:
Sometimes, file permission issues can prevent proper syncing. Ensure that the user running Vagrant has the necessary permissions to read and write to the synced folders.

Vagrant Plugins

vagrant plugin install

Installs a Vagrant plugin.

Example: vagrant plugin install vagrant-vbguest

vagrant plugin list

Lists installed Vagrant plugins.

Example: vagrant plugin list

vagrant plugin uninstall

Uninstalls a Vagrant plugin.

Example: vagrant plugin uninstall vagrant-vbguest

Tips and Tricks

Use a Version Control System:
Keep your Vagrantfile under version control (e.g., Git) to track changes and collaborate effectively.

Customize Guest Machine Hostname:
Set a custom hostname for your guest machine for easier identification.

config.vm.hostname = "dev.example.com"

Optimize Performance:
Use SSDs, allocate sufficient RAM, and consider using NFS synced folders for better performance.