This article shows you how to provision your vagrant. We will start with a very simple dumb box and add features incrementally.
You need to have virtualbox and vagrant installed on your system before following this article.
Lets begin by creating the most basic vagrant box.
- create a directory somewhere e.g. mkdir ~/meanbox
- cd into the directory and run the command "vagrant init"
- You should now see a file called VagrantFile. Replace its contents with the following
- Run vagrant up
Vagrant.configure(2) do |config| # ubuntu 14.04 config.vm.box = "ubuntu/trusty64" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. config.vm.provider "virtualbox" do |vb| vb.memory = "2048" end end
config.vm.box defines the box you want to install. Do you want to install Ubuntu 12.04, 14.04, CentOS, or other custom boxes. You can find the list of boxes at https://atlas.hashicorp.com/boxes. In this example, I chose ubuntu/trusty64 which is Ubuntu 14.04.
config.vm.provider specifies the virtual machine software and allocated memory. In this example, we are have specified that we are using virtualbox and that we have allocated 2Gb memory to this virtual machine.
Once the box is running, you can run "vagrant ssh" command to log into the shell of the system.
We can call a shell script or Puppet, Chef, Ansible, or Docker code for provisioning. In this example, we will specify a private network, create a directory shared between host and guest machines, and provision from a shell script.
Replace the contents of Vagrantfile with the following
Vagrant.configure(2) do |config| # ubuntu 14.04 config.vm.box = "ubuntu/trusty64" # check box for updates config.vm.box_check_update = true # shared folder between host and guest os config.vm.synced_folder "data", "/var/www", :mount_options => ["dmode=777", "fmode=666"] # forwarded port mapping config.vm.network "private_network", ip: "192.168.20.20" # host name config.vm.hostname = "meanbox1" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. config.vm.provider "virtualbox" do |vb| vb.memory = "2048" end # Enable provisioning with a shell script. Additional provisioners such as # Puppet, Chef, Ansible, Salt, and Docker are also available. config.vm.provision "shell", path: "provision.sh" end
Create an empty directory called "data".
config.vm.synced_folder maps a directory between guest and host OS. The data directory can be access by your machine's operating system so you can use all your software. Without mapping, you will be limited to software you have installed on your virtual machine and you will not have any nice GUI software.
In this example, config.vm.network defines a private network with IP 192.168.20.20. Once you install Apache on your virtual machine, you will be able to access a site installed on it from the address in the browser.
config.vm.provision is specifying that we will be using a shell script for provisioning and the file is located at provision.sh
Create a file provision.sh and the following code to it. Set it to chmod 755.
#!/bin/bash echo "Provisioning VM...\n" sudo apt-get install git -y
This code will install git on the virtual machine. Run one of the following commands
vagrant up --provision or vagrant reload --provision
So far we have only looked a simple examples. To build a powerful and useful box, you would need to do a lot more installation and configuration in your shell script. I would strongly recommend that you start from the script found at https://gist.github.com/rrosiek/8190550. It installs Apache, MySQL, PHP, phpMyAdmin, and many other useful tools. It is will help you get started faster and the code is a good example for beginners.