Day 21 – Provisioning with Shell Scripts in Vagrant

As I continue my journey with Vagrant, today I unlocked one of its most powerful features: Provisioning. This is the secret sauce that turns your virtual machine from a blank slate into a fully configured development environment — all with just one command.


⚙️ What is Provisioning?

Provisioning is how you automate the setup of your virtual machine. Instead of logging in and manually installing packages or setting configurations every time, you can write scripts that Vagrant runs as soon as the machine is created.


✅ I Tried Two Methods:

🔹 1. Inline Shell Provisioning

This is the quickest way to include shell commands directly inside your Vagrantfile.

config.vm.provision "shell", inline: <<-SHELL
  sudo apt update
  sudo apt install -y nginx
SHELL

💡 Great for simple or one-off setups.


🔹 2. External Shell Script

For more complex setups, or to keep things clean and modular, you can point to an external .sh script:

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

📝 setup.sh content:

#!/bin/bash
sudo apt update
sudo apt install -y git curl

📁 This approach is easier to maintain and reuse across multiple Vagrant projects.


🔁 Re-running Provisioning

You can reapply your provisioning scripts anytime without destroying the VM:

vagrant provision

Perfect for tweaking your setup without starting over.


🚀 Takeaway

Provisioning is an absolute game-changer. With just a few lines of code, you can automate your entire environment setup and ensure consistency across machines and team members.
No more “it works on my machine” excuses!


🧠 What’s Next?

I expore the multi-stage vms and vagrant plugins


💬 Have you used provisioning with tools like Ansible or Puppet? Or do you prefer shell scripts for simple setups?

0
Subscribe to my newsletter

Read articles from Shaharyar Shakir directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shaharyar Shakir
Shaharyar Shakir