Now we will continue with learning the details of Vagrantfile. In previous post we just finished to create our initial development environment in “Getting Started With Vagrant“.
For every development environment it makes sense to create a project folder and a Vagrantfile. The file created includes the properties of each machine in the environment. For example you can setup the details of the machine like:
- Shared folders
- What softwares to be installed
Here is an our first basic Vagrantfile. In this example most of the comments are removed and this is the simplest format.
The Vagrantfile is a simple Ruby code. Do not get afraid, there won’t be any need to learn Ruby at all. This file only contains the definitions. Now lets tell the details line by line:
VAGRANTFILE_API_VERSION = "2"
This is a variable that keeps the version of the configuration. We will use version two which is the newer one.
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
This line starts a configuration block. We will use “
config” variable for the definitions.
config.vm.box = "precise64"
This line sets the box name of the virtual machine.
Ends the config block.
We finished telling the simplest format. Now lets continue with basics.
By default the folder that Vagrantfile stays, is also a shared folder in the created virtual machine. Lets check it and see. Below is the content of the vagrant project folder:
Now use “
vagrant up” and start the virtual machine.
Connect it using “
/vagrant folder. and check the contents. You can see the details below:
As you can see from the folder content it is shared correctly. To share an extra folder you can use the format below:
config.vm.synced_folder "hostfolder", "guestfolder"
And here is an example. As you can see relative path is possible. “
./test” means that the folder is in the project folder:
config.vm.synced_folder "./test", "/testfolder"
Below is the details for setting the hostname of a virtual machine.
config.vm.hostname = "hostname"
After these settings here is how our Vagrantfile looks like (Do not forget to create a folder named
test under project folder.)
If your virtual machine is running first destroy it and lets use the new settings and see what happens.
Above I removed some of the lines but at the end of the messages we can see that it is setting the hostname and also making the sharing config. After that we are connecting with ssh and checking the hostname and can see that it is set. Also checking the shared folder
testfolder gives a successful result.