Command Palette

Search for a command to run...

Vagrant Installation Guide

Comprehensive guide for installing and using Vagrant, a tool for building and managing virtual machine environments in a single workflow, with support for VirtualBox, VMware, and other providers.

Vagrant is a tool for building and managing virtual machine environments in a single workflow. It provides easy to configure, reproducible, and portable work environments.

Prerequisites

  • 64-bit operating system
  • Virtualization enabled in BIOS/UEFI
  • Minimum 4GB RAM
  • One of these hypervisors:
    • VirtualBox (recommended for beginners)
    • VMware
    • Hyper-V
    • Docker

Installation Instructions

Ubuntu/Debian Installation

# Add HashiCorp GPG key
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
 
# Add HashiCorp repository
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
 
# Update and install Vagrant
sudo apt update
sudo apt install vagrant
 
# Install VirtualBox (if needed)
sudo apt install virtualbox

Basic Usage

1. Initialize a Project

# Create and enter project directory
mkdir my_vagrant_project
cd my_vagrant_project
 
# Initialize Vagrant project
vagrant init hashicorp/bionic64

2. Basic Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/bionic64"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = 2
  end
end

3. Common Commands

# Start VM
vagrant up
 
# SSH into VM
vagrant ssh
 
# Stop VM
vagrant halt
 
# Destroy VM
vagrant destroy
 
# Check status
vagrant status

Advanced Configuration

1. Multi-Machine Setup

Vagrant.configure("2") do |config|
  config.vm.define "web" do |web|
    web.vm.box = "ubuntu/focal64"
    web.vm.network "private_network", ip: "192.168.33.10"
  end
 
  config.vm.define "db" do |db|
    db.vm.box = "ubuntu/focal64"
    db.vm.network "private_network", ip: "192.168.33.11"
  end
end

2. Provisioning

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"
  
  # Shell provisioning
  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y nginx
  SHELL
  
  # Ansible provisioning
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
  end
end

3. Synced Folders

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"
  
  # Basic synced folder
  config.vm.synced_folder ".", "/vagrant"
  
  # NFS synced folder (better performance)
  config.vm.synced_folder ".", "/vagrant", type: "nfs"
end

Box Management

1. Working with Boxes

# Add a box
vagrant box add ubuntu/focal64
 
# List boxes
vagrant box list
 
# Update boxes
vagrant box update
 
# Remove boxes
vagrant box remove ubuntu/focal64

2. Creating Custom Boxes

# Package current VM as a box
vagrant package --output custom.box
 
# Add custom box
vagrant box add my-custom-box custom.box

Plugins

1. Managing Plugins

# List installed plugins
vagrant plugin list
 
# Install a plugin
vagrant plugin install vagrant-vbguest
 
# Update plugins
vagrant plugin update
 
# Uninstall plugin
vagrant plugin uninstall vagrant-vbguest

2. Useful Plugins

# VirtualBox Guest Additions
vagrant plugin install vagrant-vbguest
 
# AWS provider
vagrant plugin install vagrant-aws
 
# Automatic box updates
vagrant plugin install vagrant-vbguest

Troubleshooting

Common Issues

  1. Network Issues

    # Reset network
    vagrant reload
     
    # Check network status
    vagrant ssh -c "ip addr"
  2. Provider Issues

    # Check provider status
    vagrant status
     
    # Debug provider
    VAGRANT_LOG=debug vagrant up
  3. Synced Folder Issues

    # Reinstall VirtualBox Guest Additions
    vagrant plugin install vagrant-vbguest
    vagrant vbguest

Error Resolution

  1. SSH Issues

    • Check network configuration
    • Verify SSH key permissions
    • Ensure host-only network is configured
  2. Resource Issues

    • Adjust VM memory and CPU
    • Check host system resources
    • Verify virtualization support

Best Practices

  1. Project Organization

    • Use version control for Vagrantfiles
    • Document box requirements
    • Implement proper provisioning scripts
  2. Performance

    • Use appropriate provider settings
    • Optimize synced folder configuration
    • Clean up unused boxes and VMs
  3. Security

    • Keep boxes updated
    • Use private networking
    • Implement proper firewall rules

Additional Resources