The VagrantFile

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“.

The Vagrantfile

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:

  • RAM
  • CPU
  • Name
  • Hostname
  • Network
  • 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.

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| = "precise64"

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:

    This is a variable that keeps the version of the configuration. We will use version two which is the newer one.
  2. Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    This line starts a configuration block. We will use “config” variable for the definitions.
  3. = "precise64"
    This line sets the box name of the virtual machine.
  4. end
    Ends the config block.

We finished telling the simplest format. Now lets continue with basics.

Share folder:

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:

$ ls -l
total 16
-rw-r--r--  1 fuatu  staff  4819  5 Eki 16:36 Vagrantfile
-rw-r--r--  1 fuatu  staff     0  6 Eki 01:28 testfile.txt

Now use “vagrant up” and start the virtual machine.
Connect it using “vagrant ssh“.
Goto /vagrant folder. and check the contents. You can see the details below:

$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)
  * Documentation:
  New release '14.04.1 LTS' available.
  Run 'do-release-upgrade' to upgrade to it.
  Welcome to your Vagrant-built virtual machine.
  Last login: Fri Sep 14 06:23:18 2012 from
[email protected]:~$ cd /vagrant
[email protected]:/vagrant$ ls -l
total 8
-rw-r--r-- 1 vagrant vagrant    0 Oct  5 22:28 testfile.txt
-rw-r--r-- 1 vagrant vagrant 4819 Oct  5 13:36 Vagrantfile
[email protected]:/vagrant$

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.)

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| = "precise64"
   config.vm.synced_folder  "./test", "/testfolder
   config.vm.hostname = "ubuntutest"

If your virtual machine is running first destroy it and lets use the new settings and see what happens.

$ vagrant up
==> default: Setting hostname...
==> default: Mounting shared folders...
default: /vagrant => /Users/fuatu/vagrant
default: /testfolder => /Users/fuatu/vagrant/test$ vagrant ssh
[email protected]:~$ hostname
[email protected]:~$ cd /testfolder/
[email protected]:/testfolder$

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.

Next post: Vagrant ? Defining Multiple Virtual Machines