Vagrant tip #3 Provision your VM with scripts

This quick howto teaches you how to use shell commands to provision your VM with Vagrant

The easiest way to provision a Vagrant base box is by means of shell script commands which will be executed inside the virtual machine. In order to do that, you need to specify the “shell” provision type in your Vagrantfile:

config.vm.provision “shell” do |s|
s.path “files/bootstrap.sh”
end

The file “bootstrap.sh” will contains the script to be executed when provisioning the VM, for example:

apt-get update

# Install Apache & PHP
# ————————————–
apt-get install -y apache2
apt-get install -y php5
apt-get install -y libapache2-mod-php5
apt-get install -y php5-mysql php5-mysqlnd php5-curl php5-xdebug php5-gd php5-intl php-pear php5-imap php5-mcrypt php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php-soap

# install MySQL quietly
apt-get -q -y install mysql-server-5.5

Now, run vagrant up and it will output Provisioning virtual machine… on the screen. Note that Vagrant will provision the virtual machine only once on the first run, any subsequent provisioning must be executed with the –provision flag either vagrant up –provision or vagrant reload –provision. The provisioning will re-run also if you destroy the VM and rebuild it with vagrant destroy and vagrant up.