如果你还在手动创建大量的虚拟机然后安装系统, 或者经常出现开发环境不一致引发的问题("works on my machine" bugs), 那么你应该试下Vagrant[1]这个工具。

Vagrant是一个利用虚拟化技术提供的API(如VirtualBox),来部署易用的、一致性的开发环境的工具。 同时,Vagrant提供的box仓库使得部署一些环境变得极为便利,因为我们可以直接复用别人提供的通用box。


Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases development/production parity, and makes the "works on my machine" excuse a relic of the past.


本文根据官网的上手文档[3],按照自己的理解整理而成的使用笔记, 很多都是英文直接摘抄过来的。 如果中英混用给你造成不便,建议阅读astaxie的《Go in Action》一书中的介绍。[4]



boxes are base images that are used to quickly clone a virtual machine.

boxes are namespaced. Boxes are broken down into two parts - the username and the box name - separated by a slash.

Each project uses a box as an initial image to clone from, and never modifies the actual base image.

You can find boxes in HashiCorp's Atlas box catalog.[5]





vagrant box add             # add box from HashiCorp's Atlas box catalog, local files or urls.
vagrant init
vagrant up                  # power up
vagrant ssh                 # you're in /home/vagrant when done.
vagrant reload              # reload Vagrantfile
vagrant reload --provision  # only run privisioners
vagrant destroy             # remove all of the guest disks, data will be lost.
vagrant box remove          # completely remove a box
vagrant help


shared folders

/vagrant of guest machine is a shared folder, which is mapped to the folder containing Vagrantfile of host machine.


the provision script is automatically run when box is vagrant up'ed, so it may be slow and resource-wasted.

configure provision in Vagrantfile, like:

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


port forward

forward data received on a port of host machine to a port of guest machine.

see complete docs of networking here[7].

Vagrant Share

make the vagrant box environment public to the world of Internet.

It aims to speed up development, not for production use.

Usage steps:

  1. an account of HashiCorp's Atlas

  2. vagrant login to sign in HashiCorp's Atlas

  3. vagrant share

  4. hit CTRL+C to end sharing.

tear down

  1. suspend

    vagrant suspend

    super fast.

    The downside is that the virtual machine still eats up your disk space, and requires even more disk space to store all the state of the virtual machine RAM on disk.

  2. halt

    vagrant halt will gracefully shut down the guest operating system and power down the guest machine.

  3. destroy

    vagrant destroy will stop the guest machine, power it down, and remove all of the guest hard disks.


When you has upgraded the kernel, box may not be brought up

$ vagrant up
The provider 'virtualbox' that was requested to back the machine
'default' is reporting that it isn't usable on this system. The
reason is shown below:

VirtualBox is complaining that the kernel module is not loaded. Please
run `VBoxManage --version` or open the VirtualBox GUI to see the error
message which should contain instructions on how to fix this error.

Open the GUI and follow the hints, most often it suggests you to rebuild kernel modules:

# /etc/init.d/vboxdrv setup


[1] Vagrant by HashiCorp



[4] docs of Vagrantfile

[5] box catalog

[6] astaxie的《Go in Action》一书

[7] docs of networking

packaging custom boxes