Vagrant
100%
Course progress
0 of 1 topics done
Keep going, you are close!
1 topic in a row
0 of 1 topics done
CoursesdevopsVagrantVagrant Fundamentals
Lesson·1 of 1

Vagrant Fundamentals

40 min1 lessons
1

Vagrant Fundamentals

Vagrant Fundamentals

Vagrant is a tool for building and managing virtual machine environments in a single workflow. It simplifies development environment setup.

What is Vagrant?

Vagrant is an open-source software product for building and maintaining portable virtual software development environments. It works with VirtualBox, VMware, AWS, and other providers.

Installing Vagrant

bash
# Download from https://www.vagrantup.com/downloads
# Or using package managers

# macOS
brew install vagrant

# Ubuntu/Debian
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
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
sudo apt update && sudo apt install vagrant

Vagrantfile

The Vagrantfile is the core configuration file:

ruby
# Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"
  
  config.vm.network "forwarded_port", guest: 80, host: 8080
  
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = 2
  end
end

Basic Commands

bash
# Initialize a new Vagrant environment
vagrant init ubuntu/focal64

# Start the virtual machine
vagrant up

# SSH into the machine
vagrant ssh

# Stop the machine
vagrant halt

# Destroy the machine
vagrant destroy

# Check status
vagrant status

Networking

Port Forwarding

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

Private Network

ruby
config.vm.network "private_network", ip: "192.168.56.10"

Public Network

ruby
config.vm.network "public_network"

Provisioning

Shell Provisioner

ruby
config.vm.provision "shell", inline: <<-SHELL
  apt-get update
  apt-get install -y apache2
SHELL

File Provisioner

ruby
config.vm.provision "file", source: "./config.txt", destination: "/tmp/config.txt"

Ansible Provisioner

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

Multi-Machine Setup

ruby
Vagrant.configure("2") do |config|
  config.vm.define "web" do |web|
    web.vm.box = "ubuntu/focal64"
    web.vm.network "forwarded_port", guest: 80, host: 8080
  end
  
  config.vm.define "db" do |db|
    db.vm.box = "ubuntu/focal64"
    db.vm.network "private_network", ip: "192.168.56.11"
  end
end

Synced Folders

ruby
# Default synced folder
config.vm.synced_folder ".", "/vagrant"

# Custom synced folder
config.vm.synced_folder "./src", "/opt/www"

# NFS folder (faster)
config.vm.synced_folder "./src", "/opt/www", type: "nfs"

Best Practices

  1. Use version control for Vagrantfile
  2. Keep boxes updated
  3. Use snapshots for quick recovery
  4. Document your environment setup
  5. Use provisioners for automation
  6. Clean up unused boxes with vagrant box prune