Vagrant is command line utility that is used for setting up virtual Linux boxes with Oracle's VirtualBox
Vagrant is useful if:
Why yes, it does. However, using it is optional. Vagrant is simply another way to use VirtualBox. It also makes it easy to install different flavors of Linux, as we will see shortly.
Go to the Vagrant website and download the latest version. Make sure you have VirtualBox, too. Once you've done those things, type these commands:
vagrant box add base http://files.vagrantup.com/lucid32.box vagrant init vagrant up
In further detail, vagrant box add downloads a "box file", which is used as a template when building a new machine. vagrant init crates a file in the current directory called VagrantFile, which holds configuration information for the virtual machine. Finally vagrant up starts the virtual machine. Starting up the virtual machine will produce output like this:
$ vagrant up [default] Importing base box 'base'... [default] Matching MAC address for NAT networking... [default] Clearing any previously set forwarded ports... [default] Forwarding ports... [default] -- 22 => 2222 (adapter 1) [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Booting VM... [default] Waiting for VM to boot. This can take a few minutes. [default] VM booted and ready for use! [default] Mounting shared folders... [default] -- v-root: /vagrant
Congratulations! You now have a running virtual machine! However, as of this writing the default Vagrant box is Ubuntu 10.04, which is a bit outdated. So we're going to destroy that box with vagrant destroy
$ vagrant destroy -f [default] Forcing shutdown of VM... [default] Destroying VM and associated drives...
That machine is done for. Be sure to remove the VagrantFile, since we'll be creating a new one shortly.
As of this writing, the newest Long Term Support version of Ubuntu Linux is 12.04, so we're going to with that. The website vagrantbox.es has several dozen pre-made box files that can be used to create Vagrant machines. For our purposes, we're going to install "Precise 64" and boot a machine from that.
$ vagrant box add precise64 http://files.vagrantup.com/precise64.box $ vagrant init precise64 $ vagrant up
Vagrant will download that file, and the box will boot up in a similar manner to before.
In order to get anything done with Vagrant, you'll need to SSH into your instance. That can be done with the vagrant ssh command:
$ vagrant ssh Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64) * Documentation: https://help.ubuntu.com/ Welcome to your Vagrant-built virtual machine. Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2 vagrant@precise64:~$ ls -l /vagrant/ total 4 -rw-r--r-- 1 vagrant vagrant 469 Dec 19 05:22 Vagrantfile vagrant@precise64:~$
While I was in there, I took a peak in the /vagrant directory. That is a special directory in each Vagrant instance which is mapped to the directory where Vagrant was launched from. It is a painless way to move files between your VM and host machine. The clever developer will keep their git repository in that directory so they don't lose their changes upon destruction of their Vagrant instance.
Just like any other Linux instance, you can use apt-get to install software. (it's always a good idea to run apt-get update first, though) To map port 80 of your Vagrant instance to a port on your host machine, try this line in your Vagrantfile:
config.vm.forward_port 80, 8080
Now any connection to port 8080 of your local instance will hit port 80 of your Vagrant instance.
Here's an advanced Vagrantfile. It will have a few new things which I will explain shortly:
Vagrant::Config.run do |config| config.vm.define :web do |config| config.vm.box = "precise64" config.vm.forward_port 80, 8080 config.vm.customize ["modifyvm", :id, "--memory", 128] config.vm.network :hostonly, "10.0.5.2" config.vm.provision :chef_solo do |chef| chef.cookbooks_path = "chef-101-introduction-to-chef/cookbooks" chef.add_recipe("hello::default") end end config.vm.define :db do |config| config.vm.box = "precise64" config.vm.customize ["modifyvm", :id, "--memory", 128] config.vm.network :hostonly, "10.0.5.3" end end
A few new things are going on in here. First, two machines are defined. Typing vagrant up will start up both machines in series. This could allow you to simulate a separate webserver and database server if you wanted to. Next, there's is a line with --memory in it. That changes the amount of RAM available to each box. The default is 360 MB of RAM for each Vagrant instance, which may be too little or too much depending on what you are doing. Further down, there is the "hostonly" version of networking. That will allow you to assign an IP address to each machine which only your machine (and each VM) can talk to.
The last part of the configuration for web loads a Chef cookbook and adds a recipe to its runlist. This particular cookbook can be obtained via my blog post about Chef. When that Vagrant instance is brought up, that Chef recipe will be run. In a work environment, Vagrantfiles and chef recipes could be distributed and each developer would have everything they need to build a working development instance.
Apologies for this article being so long, but as you can see, I had a lot of ground to cover. Don't hesitate to reach out to me or leave a comment below if you have any questions about Vagrant and/or Chef.