Hello everyone!
Not long ago I wrote a review of
Vagrant: Up and Running. This time, I'd like to post some tutorial on using Vagrant for those, who are totally new to it. Let me just remind you that Vagrant is a useful tool for managing virtual machines and their settings of resources used, network and others. In most cases, people use to create VMs for VirtualBox, but there is also possibility to set up Amazon EC2 machines directly from Vagrant.
Setting up
After installing Vagrant (and VirtualBox), to create new virtual machine, you just need to type in your terminal:
This means creating new settings for a VM that will be based on ubuntu 12.04. In your current directory new file will be created: Vagrantfile. It's a text file with all the settings of your VM. Actually, it contains just a Ruby source code, but don't think you need to know Ruby to use Vagrant efficiently.
Initially created Vagrantfile contains some default settings and huge amount of settings that are commented out, just to give you some idea about what else can be configured here. But for know let's just keep the defaults.
Starting the machine
To start your newly defined virtual machine you just need to type:
If it's the first time you run it, the VM will be created. If base image for a vm (clean ubuntu) is needed then it will be downloaded automatically from url specified in "init" command.
To actually use the machine you need to use ssh:
Finishing work
After you finished your work, you would usually stop it by:
Or you can destroy the whole machine, so that all the resources are freed (including hard disk space).
More resources
If you need high-performance on the virtual machine, then you probably need to adjust the resources it is using. When using Vagrant with VirtualBox you need to add some additional settings in Vagrantfile. These settings are specified in format of VBox's "modifyvm" command:
Sharing a folder
When you work on guest virtual mashine, there is often need for sharing a folder between host and guest operating system. Nice thing in Vagrant is that you can just specify a single configuration file to set up this shared folder (and mount it on guest OS).
After next "vagrant up", you will be able to use the folder on guest.
Port forwarding
When you run some kind of a (web?) service on guest, you need to somehow be able to connect to it. In Vagrant, it is just another one-liner!
This line specifies that when you try connect from host to "localhost:9090", you will actually connect to the guest machine on port 9000. It's as simple as that. This way you can easily test web application running on guest, using your web browser from host.
Additional software - provisioning
Managing of software inside a virtual machine is called provisioning. There are few mechanisms available in Vagrant for this job. Here I will describe only the most basic one: shell provisioning.
Shell provisioning is just a set of shell "tasks" to be executed after machine boot. You can write exactly shell commands in Vagrant file, or point to a shell script that should be executed.
In clean ubuntu you should start with running "apt-get -f install", just to be able to install additional software using apt-get package managing tool.
To do it after each machine boot, just put following line in your Vagrantfile:
If you want to run a script, you can specify path to it, ex:
I assume that this script has instructions for installing git in the system. I'd like to point out that when using Vagrant, you should use option "apt-get install -y {package_name}", which makes apt-get assume that you answer positively on any "y/N" question.
You might wonder, what you can do to make some scripts run only once (on first boot), rather than on every "vagrant up". The simple trick is to make inside a script an if statement for presence of some file (let's say you keep logs of installing git in it):
Then only the first time you start the vm the script will be run. If you want to run it again, you need to delete particular file.
FAQ
Any GUI? If you set a flag: "vb.gui = false" inside a virtualbox configuration (in Vagrantfile), you will have your GUI. But you would probably also need to install packages like Gnome to make real use of that.
More provisioning techniques? There are also ways to use provisioning tools like Puppet of Chef, but I'm not an expert on those, so you need to find something about it on your own:)
Custom base images? No problem, just look on "how to create your own box" in Vagrant documentation.
More network settings? You can do a lot - actually anything (I think) that is possible with typical VirtualBox.
More machines defined inside a Vagrantfile? Yes, that's doable. Normally you use only a single Vagrantfile per project, even if you need more virtual machines.
Examples
You can find an example definition of a Vagrant environment on my
github. There is a single-machine definition of vm for scala+mongodb+play (actually +sbt) development.
Note: It's common for Vagrant users to run developed code on VM, but edit the code in your favourite IDE on host OS.