Vagrantfile — Overview
The Vagrantfile is the heart of every Vagrant project. It is written in Ruby DSL and defines everything about your VM — the base box, CPU, memory, networking, shared folders, and provisioning scripts. It lives in the root of your project directory and is meant to be committed to version control.
Full Vagrantfile Structure
Ruby — Full Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
# ── BOX ──────────────────────────────────────────────
config.vm.box = "generic/oracle8"
config.vm.box_version = ">= 4.0.0"
config.vm.hostname = "oel8-db"
# ── NETWORK ──────────────────────────────────────────
# Port forwarding (host:guest)
config.vm.network "forwarded_port", guest: 3306, host: 13306, id: "mysql"
config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh"
# Private network (host-only)
config.vm.network "private_network", ip: "192.168.56.10"
# Public network (bridged)
# config.vm.network "public_network"
# ── SYNCED FOLDERS ───────────────────────────────────
config.vm.synced_folder ".", "/vagrant"
config.vm.synced_folder "./scripts", "/home/vagrant/scripts"
config.vm.synced_folder "./data", "/data", owner: "mysql", group: "mysql"
# Disable default synced folder
# config.vm.synced_folder ".", "/vagrant", disabled: true
# ── VIRTUALBOX PROVIDER ──────────────────────────────
config.vm.provider "virtualbox" do |vb|
vb.name = "OEL8-Database"
vb.memory = 4096
vb.cpus = 2
vb.gui = false
# Performance tuning
vb.customize ["modifyvm", :id, "--ioapic", "on"]
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
# ── PARALLELS PROVIDER ───────────────────────────────
config.vm.provider "parallels" do |prl|
prl.name = "OEL8-Database"
prl.memory = 4096
prl.cpus = 2
prl.update_guest_tools = false
prl.optimize_power_consumption = false
end
# ── PROVISIONING ─────────────────────────────────────
# Inline shell
config.vm.provision "shell", inline: <<-SHELL
dnf update -y
dnf install -y wget curl vim
SHELL
# External shell script
config.vm.provision "shell", path: "scripts/install_mysql.sh"
# Shell with arguments
config.vm.provision "shell",
path: "scripts/setup.sh",
args: ["mysql", "root", "password"]
# Run provisioner every time (not just first boot)
config.vm.provision "shell",
inline: "echo 'VM is up'",
run: "always"
end
Key Vagrantfile Settings
Box Settings
| Setting | Description | Example |
|---|---|---|
| config.vm.box | Box name to use | "generic/oracle8" |
| config.vm.box_version | Box version constraint | ">= 4.0.0" |
| config.vm.box_url | Custom box URL | "https://example.com/box" |
| config.vm.hostname | VM hostname | "oel8-db" |
| config.vm.box_check_update | Check for box updates | false |
SSH Settings
Ruby — SSH Settings
# Custom SSH settings
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
config.ssh.insert_key = true
config.ssh.private_key_path = ["~/.vagrant.d/insecure_private_key"]
config.ssh.forward_agent = true
config.ssh.forward_x11 = false
config.ssh.connect_timeout = 30
Provisioner Run Options
| run value | When provisioner executes |
|---|---|
| once (default) | Only on first vagrant up |
| always | Every vagrant up and vagrant reload |
| never | Only when explicitly called with vagrant provision |
Environment Variables in Vagrantfile
Ruby — Environment Variables
Vagrant.configure("2") do |config|
# Read from environment variables
DB_MEMORY = ENV["DB_MEMORY"] || "2048"
DB_CPUS = ENV["DB_CPUS"] || "2"
DB_IP = ENV["DB_IP"] || "192.168.56.10"
config.vm.network "private_network", ip: DB_IP
config.vm.provider "virtualbox" do |vb|
vb.memory = DB_MEMORY
vb.cpus = DB_CPUS.to_i
end
end
# Usage:
# DB_MEMORY=8192 DB_CPUS=4 vagrant up
💡 Note: The Vagrantfile is plain Ruby — you can use loops, conditionals, environment variables and any Ruby logic to make it dynamic.