Wednesday, August 29, 2012

MSYS/MinGW or virtualized Linux?

As I've been working with jQuery and learning about the development environment and various resources available, I've found that many of them use a lot of command line utilities to perform various tasks. Most recently, I've been utilizing Git for source control. The Git for Windows install includes a copy of MSYS so you can perform the command line tasks you might do in Linux. As I've been browsing GitHub for other jQuery projects of interest, I've noticed many of them use Grunt to handle file concatenation, compression, and testing. Grunt require Node.js to work and is installed as a global package. The Windows install of Node.js also includes a copy of MSYS. Next, I wanted to work with some markdown tools which are written in Ruby. The Windows Ruby Devkit has another copy of MSYS. So now I have 3 copies of MSYS and no consistent path setup so everything works nicely from the command line (I've modified my path environment variable quite a bit to make everything run properly). MSYS is great, having a bash like shell in Windows is exactly what I need - I can run my Windows apps, use my favorite editors, and still have a powerful scripting environment. However, there's just no centralization or consistency. If MSYS had a package manager like aptitude or yum to pull all these tools down and install them in one consistent spot, then things would be much better.

So I decided another solution was needed to give me the shell access I wanted but still have access to my local files and allow me to work with my favorite editors in Window - all on the machine I currently own.

I decide to try a virtualized Linux install to accomplish my objective. If it worked, I'd get those qualities and have a sandbox to try out some other technologies without cluttering up my main computer. A friend suggested I look at VirtualBox. Its free, so I went to, downloaded a copy and installed it.

The following instructions will get you a VirtualBox running on a Windows host with a Debian Squeeze guest mounted to your local filesystem, SSH access via PuTTY, and some starting tools like Git, Node.js, and Grunt.

Creating a new VirtualBox instance is pretty straight forward. My instructions are specifically for Debian Squeeze. I'm not going to step through all the wizard screens. There are several key points that need to be setup though so the remainder of this tutorial works. Here's some screenshots of those places and a quick description:

I selected VDI as the type and used a Fixed size instead of Variable.  Fixed will allocate all the space immediately instead of adding it as needed.

I chose a Bridged Adapter.  You can dig through the help to learn about the other ones and decide how much work you want to put into getting each setup.  This one was good enough for me so I went ahead with using it.

I had an ISO of the Debian install so I added it as a storage device so I could mount it inside a session and boot to it.

This is where you'll add the folder(s) you want to be able to access inside your virtual environment.  These will be mounted to the Linux file system later in this tutorial.

Once you have your setting ready, launch the instance, mount the installation ISO/CD and boot into the Linux install.  After completing that process, you should be able to login as root.  If you have problems getting this far, look at the VirtualBox documentation, it has some pretty good information to help work through some of the issues.

At this point, you should be logged into the console as root. I'm also assuming that during the Debian install, you added the user "vmuser".

We're going to start by ensuring aptitude is pointing to the correct source and is fully up-to-date:

## As root (from console)
## Remove any references to cdrom from here:
vi /etc/apt/sources.list

## Make sure everything is up-to-date
apt-get update
apt-get upgrade

Now, check if you have an SSH server running. The Debian install gives you the option to add this, but in case you missed it:

## Make sure you have SSH server installed
aptitude show openssh-server
Package: openssh-server
State: installed

## If not, install it
apt-get install openssh-server

Next, we want to be able to do things as root from vmuser, so install sudo and add vmuser to that group:

## Install sudo and add vmuser to the sudo group
apt-get install sudo
usermod -aG sudo vmuser

Now you want to mount folders from your host computer to the guest's filesystem:

## Mount the share
mkdir /mnt/share
mount -f vboxsf myshare /mnt/share

## Add it to fstab so its there each time
## you boot your VM:
vi /etc/fstab

## Add this line to the end of the fstab file:
myshare /mnt/share vboxsf

Now, determine your IP. With the Bridge Adapter, you will be assigned a real IP usable on your network via DHCP:

## Find your IP

eth0 Link encap:Ethernet HWaddr 08:00:27:b2:65:42
inet addr: Bcast: Mask:
inet6 addr: fe80::a00:27ff:feb2:6542/64 Scope:Link
RX packets:38883 errors:0 dropped:0 overruns:0 frame:0
TX packets:13938 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:31179355 (29.7 MiB) TX bytes:2285447 (2.1 MiB)

lo Link encap:Local Loopback
inet addr: Mask:
inet6 addr: ::1/128 Scope:Host
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:560 (560.0 B) TX bytes:560 (560.0 B)

My IP is so, in Windows, open PuTTY and start an SSH session to Login as vmuser and start setting up your environment. I'll begin by creating a symbolic link to the share in my home directory:

## Open PuTTY and start an SSH connection to the VM's IP
## Login as vmuser

## Add a symbolic link to the share for convenience
ln -s /mnt/share share

You can now navigate through the directories in your share. Changes you make from Windows will be visable here - changes you make inside your Linux install will be visible in Windows.

Now, install Git:

## Install some tools!
## Install Git
sudo apt-get install git

Additionally, install Grunt. It requires Node.js, however, its not a package in the Squeeze distribution. We could pull it from Sid, but might as well just compile it from source:

## Debian Sid has Node.js as a package
## Going to follow the instruction from
## to compile it locally and install (as root):

sudo bash

apt-get install make python g++
mkdir ~/nodejs && cd $_
wget -N
tar xzvf node-latest.tar.gz && cd `ls -rd node-v*`
make install

## Clean up
cd ~
rm -Rf nodejs

## Now that Node.js is installed.
## Grunt can be added via Npm:

npm install -g grunt

I've been using this setup now for a while now and its been working out great. I've even installed some other tools, run other web services, and seamlessly move back and forth between my Windows environment and my Linux environment - all on the same computer.