Out of the box…

… or how to set up your development environment with Vagrant.

I use a Macbook Pro, so first step is to download the newest Vagrant.dmg and install it on the Mac.

We need a virtual box vm that later will be packaged as base box. See here for more info. There are VirtualBox vms around that can be used as base boxes for vagrant, but it’s more fun to make one.

A “box” is the base image used to create a virtual environment with Vagrant. It is meant to be a portable file which can be used by others on any platform that Vagrant runs in order to bring up a running virtual environment.

A command line only vm is enough. Usually the base box is later downloaded from somewhere, so better keep it small. Download ubuntu-11.10-alternate-i386.iso and set up a VirtualBox vm:

  • 8GB VMDK disk
  • 512 MB Ram
  • port forwarding from host port 2222 to vm port 22
  • add a cdrom for ubuntu-11.10-alternate-i386.iso

Start the VM, when the installation screen pops up, press f4 and select command line only installation.
Also see http://wiki.ubuntuusers.de/Alternate_Installation.

Follow the installation steps. The vm gets the name vagrant-ubuntu, user name/pwd is vagrant. For now we stick partly to the conventions, but in principle you can chose whatever names you like.
After installation is complete, log in to console and call su to become root. In case you get an authentication error, try to do ‘sudo passwd root’ first.

Call ‘visudo’, set the admin group to no password (add/modify %admin ALL=NOPASSWD: ALL), also add a line ‘Defaults env_keep = “SSH_AUTH_SOCK”‘.

Add an admin group in case it does not yet exist (addgroup admin) and add our user vagrant to this group (usermod -G admin -a vagrant). Restart sudo with ‘/etc/init.d/sudo restart’. Exit out of the root account and call ‘sudo which sudo’. This should not ask for a password and simply spit out something like /usr/bin/sudo.

Now install (as sudo user):

  • ruby
  • rubygems
  • chef (do ‘gem install chef’)
  • ssh

Finally install the guest additions for virtual box. Do

  • sudo mount /dev/sr0 /media/cdrom
  • sudo /media/cdrom/VBoxLinuxAdditions.run

It will complain that the vm has no graphical interface installed but according to friend google you can ignore the complaints. And voila, with ssh -p2222 vagrant@localhost we can log in to the vm.

One hint found: set UseDNS to no in /etc/ssh/sshd_config for speeding up ssh when the host is not connected to the internet.

To be able to log in to the vm without a password, we need to configure ssh authentication with a public key.
On the Mac run

  • ‘ssh-keygen -t rsa -f vagrant’ to generate a key pair named vagrant
  • cat ~/.ssh/vagrant.pub | ssh -p2222 vagrant@localhost ‘umask 077; cat >>.ssh/authorized_keys

Calling ‘ssh -p2222 -i ~/.ssh/vagrant vagrant@localhost’ works, no password requested.

Now it’s time to box the vm.

On the host create a directory named Vagrant and inside it one called vagrant_box.
Change into vagrant_box and do

  • vagrant init
  • vagrant package –base vagrant-ubuntu-1110 –output Dev2013Master.box

to package the box. Dev2013Master.box is less than 500 MB in size.

Time to set up a project. In folder Vagrant create another subfolder named vagrant_test and change into it. Inside add another folder ‘myproject’ and in there some file dummy.txt. Do

  • vagrant init
  • vagrant box add dev2013test ../vagrant_box/Dev2013Master.box

to create the Vagrantfile and import the box.

Add the following lines to the Vagrantfile.

  • config.vm.box = ‘dev2013test’
  • config.ssh.private_key_path = “~/.ssh/vagrant”
  • config.ssh.username = “vagrant”
  • config.vm.forward_port 80, 8080
  • config.vm.share_folder “myproject”, “/myproject”, “./myproject”

The default shared folder is /vagrant in the vm, we add another one that will be found in /myproject on the vm.

Now we only need to call ‘vagrant up’ and can log into the vm with ‘vagrant ssh’. In /myproject on the vm we find the expected dummy.txt file.

To destroy the complete vm (dev2013test) call ‘vargant destroy’. ‘vagrant reload’ restarts the vm after e.g. changing the Vagrantfile. ‘vargant suspend’ suspends the vm, ‘vagrant resume’ starts it again. For more commands call ‘vagrant -h’.

… to be continued.
Next step will be to get a web project up and running.

This entry was posted in howto, virtual machine and tagged , , . Bookmark the permalink.

Comments are closed.