# Vagrant, no more MAMP?

Vagrant is a command line manager for VM (Virtual Machines). Vagrant allows developers to create and manage virtual machines using the command line, and most important of all, allows developers to specific say what they what to be included in that VM.

Vagrant was created by Mitchell Hashimoto (@mitchellh).

You can think of Vagrant as a “Virtual Machine Configurator and Manager”. Image that you go to a website and you can customise the car that you want to buy. You can choose the interior/exterior colours, type of engine, etc. This is what Vagrant does for you. You specify what you want to be included in the VM and Vagrant handles the rest.

That previous example was a little bit weird for a developer. Let me try to explain in another way. How many times, you had to setup one host so that you can work on a project? How much of that environment resembles the final production server where that project is going to be deployed? Probably you know that it uses LAMP, and you just fire up MAMP, and start coding.

How cool it would be that you could have an environment equal to the production server? With Vagrant you can. Just tell what you need and Vagrant creates that environment (VM) for you.

## How to get started?

First of all you need to have install VirtualBox.

Then you need to download and install Vagrant. Just click the latest version, and choose the correct file for your OS.

After downloading and installing Vagrant you just need to create an empty directory for your project. And inside of it run the command:

vagrant init


and you get something like this:

pedros-mbp-2:test narven$vagrant init  A Vagrantfile has been placed in this directory. You are now ready to vagrant up your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on vagrantup.com for more information on using Vagrant. if you try listing the directory you get something like this: total 16 drwxr-xr-x 3 narven staff 102 Oct 12 22:31 . drwxr-xr-x 5 narven staff 170 Oct 12 22:31 .. -rw-r–r– 1 narven staff 4606 Oct 12 22:31 Vagrantfile  This creates a simple .vagrant directory, and a Vagrantfile. This Vagrantfile is where we specify all of our configurations and specifications. The Vagrantfile looks something like this:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  # Vagrantfile API/syntax version. Don’t touch unless you know what you’re doing! VAGRANTFILE_API_VERSION = “2″ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = “base” # config.vm.box_url = “http://domain.com/path/to/above.box” # config.vm.network :forwarded_port, guest: 80, host: 8080 # config.vm.network :private_network, ip: “192.168.33.10″ # config.vm.network :public_network # config.ssh.forward_agent = true # config.vm.synced_folder “../data”, “/vagrant_data” end I’ve removed most of the comments in the file. Now if we run the command: vagrant up  you get something like this: Bringing machine ‘default’ up with ‘virtualbox’ provider… [default] Importing base box ‘base’… [default] Matching MAC address for NAT networking… [default] Setting the name of the VM… [default] Clearing any previously set forwarded ports… [default] Creating shared folders metadata… [default] Clearing any previously set network interfaces… [default] Preparing network interfaces based on configuration… [default] Forwarding ports… [default] — 22 => 2222 (adapter 1) [default] Booting VM… [default] Waiting for machine to boot. This may take a few minutes… [default] Machine booted and ready! [default] Mounting shared folders… [default] — /vagrant  This make take a while, but only for the first time. You can change and update all the configuration and run the command: vagrant reload  and Vagrant will update the VM to the specified changes. Soo now we have a VM at our disposal. Just run the command: vagrant ssh  and we get something like this: Linux lucid32 2.6.32-38-generic #83-Ubuntu SMP Wed Jan 4 11:13:04 UTC 2012 i686 GNU/Linux Ubuntu 10.04.4 LTS Welcome to Ubuntu! * Documentation: https://help.ubuntu.com/ New release ‘precise’ available. Run ‘do-release-upgrade’ to upgrade to it. Welcome to your Vagrant-built virtual machine. Last login: Fri Sep 14 07:26:29 2012 from 10.0.2.2 vagrant@lucid32:~$


…IT’SSSS ALIVE… now we are inside a Linux VM.

### Vagrant works with something called a “box”. What is a “box”?

Well a box, is a pre-made VM (bootstrap) for the VM that you want to build and customise. So every VM that you want create/modify to your needs, starts with another “clean” VM.

In this default example, since we did not specified any box, Vagrant uses a Linux Ubuntu box called lucid32. There are a lot of box’s that we can specify that we want to be our base box. Please check this link (http://www.vagrantbox.es/)for more information about it.

If we take a look at our Vagrantfile we have this line where we can specify the url of the bootstrap box that we want:

 8  config.vm.box_url = “http://domain.com/path/to/above.box”

just change it to something like:

 8  config.vm.box_url = “https://dl.dropbox.com/sh/9rldlpj3cmdtntc/56JW-DSK35/centos-62-32bit-puppet.box”

By making this simple change, instead of using the Linux Ubuntu Lucid32, we are using, CentOS 6.2 32bit (with puppet).

The Vagrantfile is very simple and easy to understand. For more information, please take a look at the documentation.

Conclusion

Vagrant has a lot of more configurations and providers (Puppet, Chef, etc) that help to configure everything that you want to be installed in the VM. And has the main ability, that you can share the Vagrantfile with team developer and he will get the same VM to work.

I’ll be posting some more information soon explaining how can you achieve your perfect VM.

comments powered by Disqus