Having a Developer VM is nice, you can try different server configurations without having to change your host machine. A virtual machine can be easily distributed to all members of a development team, so that everyone has the same environment without much ado. Clean state for everyone, as long as you have the one and only offical master VM for you team at least. Make sure they don’t change VM configurations (and forget to document) on their own. Memo to self: write a post about how useful a WIKI can be for development processes sometime.
Especially handy is having a virtual machine if you have the code on your host and let the VM access it via smb e.g. Whoever was stuck with a screwed up VM with uncommitted code knows what I’m talking about. It just happens from time to time and yes, it sucks enormously. Also you cannot access your source code without booting the VM each time if it is stored on the VM.
Ok, back to the task: how to set up a Developer VM on a Mac?
My blog’s my WIKI, so let’s see how it is done.
1. First we need to download and install VirtualBox, http://www.virtualbox.org.
2. Then there’s the decision about the operating system. Guess what, see name of the blog, we want to do some php stuff. So we’d like to set up a VM for programming web applications with php and mysql. I’m used to Ubuntu so Ubunto it will be. Dowload the iso image from http://www.ubuntu.com and burn to dvd.
Hint: if you have your Mac running in 32 bit kernel mode (check with uname -a ) forget about installing a 64 bit ubuntu, it will complain about invalid settings
Additional hint: the 32 bit version of Ubuntu does not come with openssh server installed, so best
do sudo apt-get install openssh-server
otherwise you won’t be able to connect with ssh from host to vm, see below.
3. Start VirtualBox, click the shiny button for new virtual machine and follow the wizard.
(It is called wizard not WHIZARD, will I ever learn?) Use NAT (Network Address Translation) for network and what you think is best for all else.
4. Let the fun begin. We want the code on the host system but the application running on the VM. First I want to connect from host to VM via ssh because using a terminal inside the VM starts getting on my nerves.
Task: host needs to see VM in the Network. NAT means the VirtualBox networking engine acts like a router for the VM.
Drawback: like in a home network, the computer (here VM) cannot be seen from outside. Unfortunately the host is also outside and cannot access VM. Btw, VM comes up with an IP like 10.0.2.15. We need port forwarding like described in the VirtualBox manual chapter 6.
Very easy on a Mac, just use tool VBoxManage from host’s terminal to forward all TCP traffic arriving on the localhost interface (127.0.0.1) via port 2222 to port 22 in the guest VM. VM needs to be powered off to apply the changes.
So for my VM called Devel2011 that’s
VBoxManage modifyvm “Devel2011″ –natpf1 “guestssh,tcp,,2222,,22″
Cool, it works! Now I can connect via ssh to my VM:
ssh -p2222 <user name on vm>@<host name>
or on my machine
ssh -p2222 hrdev@Durin
does the trick.
5. Somehow my hostname is not what i expect (should be Devel2011) so I adapt /etc/hostname file. Reboot VM and see if it survives.
Yeah, did survive. Enough for today.