Sunday 20 January 2013

Setting up Ubuntu (12.04 LTS) in VirtualBox under OSX Mountain Lion


I've recently needed  to run an Ubuntu VM on my 2012 Mac Book Pro (Retina) and being a cheapskate I reached for VirtualBox.

Initial installation is fairly easy but I immediately ran into a whole host of problems:

  • The newest version of Ubuntu (12.10) desktop requires 3D acceleration so it runs like crap.
  • I can't see my Mac's Documents folder that I had marked as a shared folder in VirtualBox's settings.
  • I couldn't ssh into the VM.

So to save anyone else the trouble I thought I would document the steps I went through here.

#1 System Configuration

Download Ubuntu 12.04 LTS (instead of 12.10). OK so it's not the latest version but not having to wait 2 seconds for the Ubuntu desktop to respond is vital to not loosing patience with the whole setup.
I chose 12.04 LTS 64 bit which is in the downloads section just bellow the download link for the latest version, here (Note: 32-bit is selected by default): http://www.ubuntu.com/download/desktop.

Using VirtualBox (https://www.virtualbox.org/) I set up a VM with the following configuration:

* General
Name: Ubuntu

* System
RAM: 2048 GB
HDD: 8GB , Virtual Disk Image (VDI), dynamically allocated
CPU: 2 x CPU (though this causes a non-optimal settings warning, not sure why).

* Network
Type: NAT
Port forwarding (under 'Advanced' tab in networking):
 name: 'ssh'
 host port: 2222
 guest port: 22

* Shared Folders
Name: Documents, Path : /Users/jamesdoig/Documents
Do not check "Auto mount" (as it's bugged)

#2 Installation
* Under Settings -> Storage place the Ubuntu .iso into the CD/DVD drive and start the virtual machine.

* Go through the Ubuntu installation procedure.
I chose the: English: Macintosh, international keyboard layout to match my Mac Books kb layout.

* Once you have booted into the operating system and logged in: From the virtual box menu select: Device-> Install Guest Additions. Follow the on screen prompts.

* Reboot your VM.

#3 Folder sharing setup

* Sign into Ubuntu and open up a terminal,
Make a directory in /media to mount your shared folder to:
$ sudo mkdir /media/shared
Mount the shared folder (In my case Documents) :
$ sudo mount -t vboxsf Documents /media/shared

This will mount your Documents folder at /media/shared but you will have permission problems.

* Still in your Ubuntu VM terminal do:
$ cat /etc/passwd
and make note of the numbers that appear in the entry for your login name:
e.g: james:x:1000:1000:james,,,:/home/james:/bin/bash
So 1000 in my case.

* Now using your text editor of choice (I installed emacs by this point) :
$ sudo emacs /etc/fstab
and add the line:
Documents   /media/shared  vboxsf  uid=1000,gid=1000,nodev,noexec,nosuid,auto,rw 0 0
Where uid & gid = the number you noted earlier,
'Documents' is the name of the folder you shared in VirtualBox
And '/media/shared' is the mount point your created earlier.

* Reboot your VM.

#4 SSH setup

Open up the Ubuntu terminal and do:
$ sudo apt-get install openssh-server

I'd rebooted the VM a lot during this process so can't be sure if this step also required it.

Now you should be able ssh into your Ubuntu VM from your Mac terminal with:
$ ssh -p 2222 <your login name>@127.0.0.1

#5 Running the VM in headless mode and SSHing in

This step isn't required but I did it for convenience:
In your Mac terminal:
Edit your ~/.bash_profile
e.g: $ emacs ~/.bash_profile
And add the following:

alias ubu='ssh -p 2222 james@127.0.0.1'
alias uvm='VBoxHeadless --startvm "Ubuntu"'
alias uvmx='VBoxManage controlvm "Ubuntu" poweroff'

Where "Ubuntu" is the name you gave the VM in Virtual box.
Also pick aliases that work for you.

Using these I can launch my VM headlessly (Without launching the GUI and VM desktop) from the terminal with: $ uvm
Shut down the vm with: $ uvmx
And ssh into the vm with: $ ubu

No comments:

Post a Comment