My vagrant setup on OSX¶

I said in using vagrant for developing on OSX: why? that I chose vagrant for setting up my development environment. Now it’s time for some specifics.

Vagrant box setup and location¶ Per virtual machine, vagrant uses a directory with a Vagrantfile in it. As a convention, I place my vagrant directories as subdirectory inside ~/vm/ . The name of the subdirectory (I’ll use “django” as an example in this blog post) is the name I use for the virtual machine. So ~/vm/django/Vagrantfile is the vagrant file for my “django” virtual machine. I have one other (“barebones”), but I already know I’m also going to make a “php” one for three mysql/php sites that I sometimes do a bit of work on. Those end up in ~/vm/php/ and so. Everything that’s not in git (/svn/hg/bzr) doesn’t exist, so I’ve versioned my ~/vm/ directory. I ignore ( .gitignore ) the django/php/barebones directories. The Vagrantfiles in those directories are symlinks to ~/vm/vagrantfile_django and so on. There’s a README.txt to help me set it all up when I move to a new computer. I also have a fabfile for setting up the (django) box automatically. Some of the things the fabfile does: Install a couple of extra debian packages.

Git clone my tools directory to set up some common tools I use everywhere.

Git clone my “dotfiles” directory with my settings and set them up. (See an older entry for info on dotfiles).

Source code location¶ I edit my code locally on OSX and I prefer to continue doing that. I also sometimes want to copy downloaded files directly to my source code. Or edit images in an OSX image editor. So: I want to have some part of my directory structure available both in OSX and inside the virtual machine. I’ve handled that in my Vagrantfile . The file is mostly the vagrant-provided default one. The relevant filesystem line is: # Share an additional folder to the guest VM. The first argument is # an identifier, the second is the path on the guest to mount the # folder, and the third is the path on the host to the actual folder. config . vm . share_folder "v-data" , "/vagrant" , "." , : nfs => true This mounts the ~/vm/django/ folder on OSX to /vagrant/ inside the virtual machine (with NFS), making them available on both. I checkout my source code in /vagrant/some_category/some_project on the virtual machine, which ends up as ~/vm/django/some_category/some_project on OSX. Note that I use checkoutmanager to create and update my svn/git/hg checkouts, so it was simple for me to split its configuration. I have a separate checkoutmanager config for my django virtual machine and one for OSX.

SSH, hostname, username¶ Vagrant provides a vagrant ssh command that gives you an ssh shell for the virtual machine in whose directory you are. But I don’t want to cd to a directory first. So I set up a hostname and ssh config for my virtual machines. I can do ssh django now to get inside my virtual machine. In /etc/hosts : 33.33 . 33.20 django 33.33 . 33.30 barebones 33.33 . 33.30 php In ~/.ssh/config : Host django HostName 33.33 . 33.20 User vagrant ForwardAgent yes Host barebones HostName 33.33 . 33.30 User vagrant ForwardAgent yes Host php HostName 33.33 . 33.40 User vagrant ForwardAgent yes The User line logs me in as user “vagrant” even though I’m “reinout” in OSX. And “ForwardAgent” lets me re-use my ssh key if I connect to servers from within the virtual machine.