Getting Started with Vagrant

Posted by 4Aiur on 2014/01/12 in SysAdmin |

Getting Started with Vagrant


Install VirtualBox or VMware first, and download the latest from Vagrant download page.

Launch a instance

cd ~/.vagrant.d/tmp/
vagrant box add CentOS-6.4-x86_64
mkdir -p ~/.vagrant.d/init/myvm
cd ~/.vagrant.d/init/myvm
vagrant init CentOS-6.4-x86_64
vagrant up
vagrant ssh


# install rvm
\curl -sSL | bash -s stable
source "$HOME/.rvm/scripts/rvm
gem install chef
mkdir -p ~/.vagrant.d/init/my-recipes/cookbooks
cd ~/.vagrant.d/init/my-recipes/cookbooks
for cookbook in yum mysql apache2
    knife cookbook site download $cookbook
# untar the cookbook tar file


config.vm.provision :chef_solo do |chef|
  chef.cookbooks_path = "../my-recipes/cookbooks"

  chef.add_recipe "yum"
  chef.add_recipe "yum::epel"
  chef.add_recipe "mysql"
  chef.add_recipe "apache2"

  # You may also specify custom JSON attributes:
    :mysql => {
      "server_root_password" => "yourmysqlpassword",
      "server_repl_password" => "yourmysqlpassword",
      "server_debian_password" => "yourmysqlpassword"
    :apache2 => {
      "listen_address" => ""

config.vm.provision :shell, :path => ""

#!/usr/bin/env bash

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
sudo yum -y update

Run a specific provisioner

vagrant provision --provision-with chef_solo

Run chef-solo on VM

sudo chef-solo -c /tmp/vagrant-chef-1/solo.rb -j /vagrant/chef_solo.json


  "mysql": {
    "server_root_password": "yourmysqlpassword",
    "server_repl_password": "yourmysqlpassword",
    "server_debian_password": "yourmysqlpassword"
  "apache2": {
    "listen_address": ""
  "run_list": [


Creating the Vagrantfile

Name the file Vagrantfile.pkg do |config|
  # Forward apache
  config.vm.forward_port 80, 8080

Packaging the Project

$ vagrant package --vagrantfile Vagrantfile.pkg

Distributing the Box

$ vagrant box add my_box /path/to/the/
$ vagrant init my_box
$ vagrant up


Enable debug:

VAGRANT_LOG=info vagrant up
set VAGRANT_LOG=info
vagrant up
VAGRANT_LOG=debug vagrant provision

Insecure world writable dir /Applications in PATH, mode 040777

Vagrant::Errors::VMBootBadState: The guest machine entered an invalid state while waiting for it
to boot. Valid states are 'starting, running'. The machine is in the
'poweroff' state. Please verify everything is configured
properly and try again.

If the provider you're using has a GUI that comes with it,
it is often helpful to open that and watch the machine, since the
GUI often has more helpful error messages than Vagrant can retrieve.
For example, if you're using VirtualBox, run <code>vagrant up</code> while the
VirtualBox GUI is open.

Run this to fix it:

sudo chmod 755 /Applications/

Mount error on v-root: /vagrant

When you start up your guest, you may get the following message unexpectedly:

[default] -- v-root: /vagrant
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

mount -t vboxsf -o uid=<code>id -u vagrant</code>,gid=<code>id -g vagrant</code> v-root /vagrant

This is usually a result of the guest’s package manager upgrading the kernel without rebuilding the VirtualBox Guest Additions. To double-check that this is the issue, connect to the guest and issue the following command:

lsmod | grep vboxsf

If that command does not return any output, it means that the VirtualBox Guest Additions are not loaded. If the VirtualBox Guest Additions were previously installed on the machine, you will more than likely be able to rebuild them for the new kernel through the vboxadd initscript, like so:

sudo /etc/init.d/vboxadd setup

Upgrade VirtualBox Guest Additions

[default] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.

Guest Additions Version: 4.2.8
VirtualBox Version: 4.3

Attach VBoxGuestAdditions.iso(can be found /Applications/ in VirtualBox manually.

For Ubuntu should run next commands:

sudo apt-get update -q
sudo apt-get install -q -y dkms
# Install linux-headers
sudo apt-get install -q -y linux-headers-3.2.0-23-generic
sudo mount -o loop,ro /dev/sr0 /mnt

For CentOS just need to run next command:

sudo mount -t iso9660 -o loop,ro /dev/cdrom /mnt/

Upgrade VirtualBox Guest Additions:

sudo sh /mnt/ --nox11
vagrant reload

Fix Vagrant Box Hanging at Boot(grub menu)

Use VirtualBox to boot this VM, and run command: sudo update-grub

Related links:

Tags: ,

Copyright © 2010-2017 4Aiur All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from