11 分钟 阅读

Host环境: Yosemite, 使用的虚拟机VirtualBox 3.1.16

###SetUp Vagrant




  • vagrant-vbguest: 把host机器上的VirtualBox Guest Additions自动安装到虚拟机上
  • vagrant-librarian-chef: 自动跑Chef
vagrant plugin install vagrant-vbguest
vagrant plugin install vagrant-librarian-chef


由于国内的网速比较慢,所以还是先把ubuntu镜像单独下载下来,再安装! vagrantbox下载,假设下载到~/downloads/, 在Terminal里输入

vagrant box add ubuntu14 ~/downloads/



创建一个项目目录,比如~/documents/workspace/rails-vagrant, 进入目录,使用’ubuntu14’这个box

vagrant init ubuntu14
vagrant up

等待启动完成后, vagrant ssh 登录虚拟机了!



在目录下建立一个文件Cheffile, 然后编辑

site ""

cookbook 'apt'
cookbook 'build-essential'
cookbook 'mysql', '5.5.3'
cookbook 'ruby_build'
cookbook 'nodejs', git: ''
cookbook 'rbenv', git: ''
cookbook 'vim'
cookbook 'postgresql', '~> 3.4.10'




# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at

  # Every Vagrant virtual environment requires a box to build off of. = "ubuntu-14"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine. "forwarded_port", guest: 80, host: 8080 "forwarded_port", guest: 3000, host: 3000

  # Create a private network, which allows host-only access to the machine
  # using a specific IP. "private_network", ip: ""

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  config.vm.synced_folder "./codes", "/vagrant_data", :nfs => true

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  config.vm.provider "virtualbox" do |vb|
      # Don't boot with headless mode
      vb.gui = false
      # Use VBoxManage to customize the VM. For example to change memory:
    vb.customize ["modifyvm", :id, "--memory", "2048"]

  # Enable provisioning with Puppet stand alone.  Puppet manifests
  # are contained in a directory path relative to this Vagrantfile.
  # You will need to create the manifests directory and a manifest in
  # the file default.pp in the manifests_path directory.
  # config.vm.provision "puppet" do |puppet|
  #   puppet.manifests_path = "manifests"
  #   puppet.manifest_file  = "default.pp"
  # end

  # Enable provisioning with chef solo, specifying a cookbooks path, roles
  # path, and data_bags path (all relative to this Vagrantfile), and adding
  # some recipes and/or roles.
  # Use Chef Solo to provision our virtual machine
  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = ["cookbooks", "site-cookbooks"]

    chef.add_recipe "apt"
    chef.add_recipe "nodejs"
    chef.add_recipe "ruby_build"
    chef.add_recipe "rbenv::user"
    chef.add_recipe "rbenv::vagrant"
    chef.add_recipe "vim"
    chef.add_recipe "mysql::server"
    chef.add_recipe "mysql::client"
    chef.add_recipe "postgresql::server"
    chef.add_recipe "postgresql::client"
    chef.add_recipe "postgresql::ruby"

    # Install Ruby 2.1.2 and Bundler
    # Set an empty root password for MySQL to make things simple
    chef.json = {
      rbenv: {
        user_installs: [{
          user: 'vagrant',
          rubies: ["2.1.2"],
          global: "2.1.2",
          gems: {
            "2.1.2" => [
              { name: "bundler" }
      mysql: {
        server_root_password: ''
      postgresql: {
        password: {
          postgres: ""
      run_list: ["recipe[postgresql::server]"]

修改了Cheffile和Vagrantfile后,需要执行vagrant provision,修改才会生效。 执行时间可能比较长,等执行完之后,依旧执行vagrant ssh登录虚拟机。



* *