Vagrant is a tool to manage the set up and the configuration of virtual machines, quite useful if you are a developer in Ruby on Rails. Vagrant allows you to use a set of hypervisors to run your virtual machines, such as VMware or AWS. On its default configuration it uses VirtualBox, a free tool from Oracle available on Windows platforms, Linux and Mac OS X.
Installation and configuration of Vagrant
The first step is to install Vagrant on your development platform. They are installers for this, so choose one of them. The installation is straightforward so you should not find any trouble on this.
Keep going and install VirtualBox as your hypervisor to run the virtual machines. Same as before, choose the right installer for your platform.
Once Vagrant and VirtualBox installed you need to configure the full package. The virtual machine’s configuration is stored on a file called Vagrantfile. This file also stores the root file for our project. Vagrantfile should be stored with the code that we develop, and it should also be kept on our version control system, Git, Subversion or whatever you use. This has many advantages for us as developers:
- it allows you to associate the configuration of the development environment with the software. This is a nice feature when the platform evolves as the software.
- it allows you to share with other developers your developer platform, preventing the “this works on my machine” effect, so typical on distributed developers teams.
Let’s go. The first step is to create the project’s root folder and to initialise Vagrant. Something like this:
$ mkdir myproject $ cd myproject $ vagrant init
The last command has created the vagrantfile that we would add later to our version control system.
We would need to install the Box – the virtual machine – from the Vagrant Cloud, the Vagrant repository. There are a huge amount of boxes, and I am going to use a box already configured for Ruby development; this box is called outnorth/debian-7.4RubyRailsDev.
I run from the command line:
$ vagrant box add outnorth/debian-7.4RubyRailsDev
I should indicate that this box will be the base for my project, so inside the Vagrantfile I substitute the line
config.vm.box = base
by this one containing the name of the box that I want to use.
config.vm.box = outnorth/debian-7.4RubyRailsDev
On the Vagrantfile we can also indicate the resources that the virtual host will use. I can increment the amount of memory and the number of CPUs, for instance, just modifying one line on this file. For doing so I shall uncomment and substitute this line:
vb.customize ["modifyvm", :id, "--memory", "1024"]
by this one:
vb.customize ["modifyvm", :id, "--memory", "2048", "--cpus", "4"]
Once modified I save the file, start up the virtual host and connect it through SSH.
$ vagrant up $ vagrant ssh
And this is when the magic starts. Vagrant is using shared folders between our development platform and the virtual machine. I can develop on my platform and the code is visible on the Vagrant virtual machine, where the code will be executed. These folders are called synced folders. The synced folder in this case is /vagrant.
$ vagrant ssh $ ls /vagrant Vagrantfile
The file Vagrantfile is the same file that we have created on our development platform.
I modify the Vagrant file to configure the networking on the virtual machine. I would like to redirect the local port 3000 to the port 3000 on the virtual machine. As you probably know, the port 3000 is used by ruby on rails. I uncomment and modify the line as following:
config.vm.network :forwarded_port, host: 3000, guest: 3000
With this the virtual host is ready to develop on it.
To stop the virtual machine I can use suspend, halt or destroy:
- suspend: the virtual machine and it current status is saved on disk. The start up is very fast.
- halt: the virtual machine is stopped and it is kept on disk. The start up is slower than the previous suspend status.
- destroy: the virtual machine is deleted and your job disappear in the blink of an eye. Use it carefully.
$ vagrant suspend $ vagrant halt $ vagrant destroy
On the next post I will show the funny part of it… how can I develop a rails application on this environment?
Something more to read?
- https://gorails.com/episodes/setup-vagrant-for-rails-development