41 North/blog

Creating a Ruby/Rails development environment in OS X using a Parallels virtual machine

Published on May 31st, 2012 by Jim

There are a lot of good reasons to use virtual machines to host your development environments. Different projects you are working on can require different configurations or component versions, and it’s way too easy to mess up a machine you are trying to split between production and development work. Keeping all the dev work in its own little sandbox avoids a lot of headaches down the line.

I was setting up a new Rails dev environment last week and I came across a lot of inconsistent and outdated info on the web. In the interests of helping out someone else in the same boat, I put together this step-by-step guide to building a Rails 3.2 development environment from scratch using Debian Linux running on a Parallels Desktop 7 virtual machine. My hardware is a MacBook Pro running OS X Lion with 8GB of RAM.

Step 1: Installing Debian

The first step is to download a Debian image file to install your virtual machine from. I used one of the network install images – specifically, the AMD64 small CD image.

Once you have downloaded your image file, launch Parallels and go to File–>New. Select the “Install Windows or another OS from DVD or image file” option, and browse to the location where you saved the Debian ISO and click Continue.

Debian install screenshot 1

If a window pops up saying “Automatic detection failed”, go ahead and select “More Linux” and then “Debian GNU/Linux” from the drop down box, then click OK.

On the next screen, give your new VM a name and specify a location to save it in. You can also check the “Customize settings before installation” if you want to change the amount of resources allocated to the VM, but leaving everything at the defaults is fine. Select “Continue” and your new VM will be created. The next screen you see should be the Debian installer boot menu.

Debian install screenshot 2

Now, feel free to use the fancy graphical installer if you like, but this is a development machine! You should be in the command line 100% of the time on this box, so geek up and choose the old school installer (the first option marked simply, “Install”). On the first screen, select your installation language. On the next screen, select the country you are in. Next, select your keyboard layout. Debian will now install some components and initialize your network adapter. I had no problem with the installer finding and using both the Ethernet and Wi-Fi adapters in my MacBook Pro.

Now – you have a very important choice to make:

xkcd #910: Permanence

Once you are done selecting your hostname, you can enter a domain name on the next screen (or leave the default of “localdomain”). You will then have to select a root password. Do not lose this password! Write it down, you will need it later. After setting a root password, you will create your first user account. This is the user account you will be using most of the time. Set a real name, username, and password. The next screen will be a time zone selection – easy peasy. Then, we come to the partitioning screen.

Debian install screenshot 3

If you know what you are doing, feel free to partition the disk however you like. You might want a certain partition scheme to mirror your production environment as closely as possible. If you don’t know what you are doing, just stick with the defaults. Remember that the disk you are talking about is the virtual machine’s disk – you won’t harm anything on your host machine during this process.

After partitioning is complete, Debian will install the base system. You will then select a mirror site where you will download the remaining packages from. Select the nearest country to your location and then select a mirror site. If you access the Internet through a HTTP proxy you will be able to enter in the proxy details. Debian will ask if you want to participate in a package usage survey – this is personal preference, but I select “No”. Next, you will be asked to install optional software packages.

Debian install screenshot 4

I always deselect the graphical environment – I prefer the command line. There is nothing you can’t do with the command line, and you will learn a lot more about Linux and how it works behind the scenes. It also makes it possible to launch and use your virtual machine while remoting into your host machine using VNC. Again, this is personal preference, but I would ditch the windows unless you have a really good reason to use them.

Finally, you will be asked to install the Grub boot loader – just select the defaults here. Debian is now installed, and you will reboot into your new Linux system!

Step 2: Setting up for sudo and ssh

Now we will get ssh installed so you will have the ability to ssh into the VM from your host machine. We will also install sudo so we do not have to stomp around our system in our root boots. We will use Debian’s “Advanced Packaging Tool”, apt, to install these tools. Go ahead and logon to the system as root and issue the following commands:

apt-get install ssh
apt-get install sudo

Once apt finishes its thing and installs ssh and sudo, we need to add your username to the sudo users list. To do this, type:

visudo

which will launch nano and allow you to edit your /etc/sudoers file.

Move your cursor down to the line after “root ALL=(ALL) ALL” and type in a new line. You are going to copy that root line, but using the username you created during the install. My user was created with the username jim, so I would enter “jim ALL=(ALL) ALL” as shown below:

visudo screenshot

Save the file (press CTRL-X, hit “y”, hit Enter) and you’re good to go! You can logout as root now – remember, absolute power corrupts absolutely!

Step 3: Installing Ruby and RVM

Now we are going to install a few prerequisites – cURL, git, and a base version of Ruby. We can do this all in one fell swoop with a single command. Note the sudo before the command – this tells Debian to run the command in super user mode and the system will prompt you for your password before executing. Enter your user’s password, not root’s password (you did use different passwords, right?)

sudo apt-get install curl git-core ruby-full

As you can see, you can specify multiple packages for apt to install by listing them separated by spaces. This will take a while – it installs quite a few libraries and such that ruby uses. Once it is complete, we can install RVM. RVM stands for Ruby Version Manager. RMV makes it so painless to manage multiple versions of Ruby, Rails, and various gems that I can’t understand why someone wouldn’t use it. If you don’t want to use RVM, just leave – you aren’t allowed to read this blog anymore. Also, delete the VM you just created, because I am retroactively denying you access to these instructions you’ve been following. Also, hit yourself in the face, because I can’t be there to do it for you.

Still here? OK, good. Let’s install the latest stable release of RVM using curl:

curl -L get.rvm.io | bash -s stable

and when that is complete:

source ~/.rvm/scripts/rvm

So now we have a base Ruby version and RVM installed – you can verify this by running:

ruby -v

and

rvm -v

which should give you output like the screen below:

Ruby and RVM screenshot

We are almost ready to install Rails, but first let’s check the prerequisites that Ruby on Rails needs – RVM has a handy command for this:

rvm requirements

Down in the “Additional Dependencies” section, there is a list of packages we should install to make sure we have all of our bases covered. Ignore the sections for jruby (Ruby on JVM) and IronRuby (Ruby on .NET) – we just want the packages in the section that says “For Ruby / Ruby HEAD”. Copy the apt-get command – we are going to paste it into our command line and execute it, but remember to run it using sudo. Here’s what it looked like on my system:

sudo /usr/bin/apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

Don’t worry if you see some packages in there that we already installed – apt will just skip over those. It will take a little bit of time to install all of these packages. When you are done, it’s time to install Rails!

Step 4: Installing Rails

First, let’s get the most recent Ruby version:

rvm install 1.9.3

Note that the apt package installs Ruby 1.8.7 as the base version – this is still the “official” base version used by Debian (as well as OS X), but this is probably going to change pretty soon. There is a push to move people on to the much improved 1.9 version. However, for us it just doesn’t matter – we can let Debian have its 1.8.7 and be happy, and we can use RVM to install as many different versions of Ruby as we want. That is the true beauty of RVM – one day you can work on a legacy app using 1.8.7, the next day mess around with the latest edge version, and never mess up your environment!

So now we have Ruby 1.9.3 installed – the next step is to install Rails. I like to make gemsets for each Ruby/Rails version combo that I use – this allows you to easily switch between versions. Read more about gemsets – they are extremely useful. Let’s set one up:

rvm gemset create rails3.2.3

And set it to the default:

rvm use 1.9.3@rails3.2.3 –default

and install Rails:

gem install rails

OK – we are almost done. We just need to install a JavaScript runtime. I recommend Node.js

Step 5: Installing Node.js

I followed the excellent directions at sekati.com/etc/install-nodejs-on-debian-squeeze to install Node.js. Basically, we are going to use git to clone the Node.js project from github.com and then we are going to get all old-school Unix-y and compile the source from scratch.

Make sure you are in your home directory and clone the project:

cd
git clone https://github.com/joyent/node.git

Now we switch into the directory git just created and checkout the latest stable version (0.6.18 at the time of this post – check https://github.com/joyent/node/tags to find the latest stable build):

cd node
git tag
git checkout v0.6.18

Now – we specify where our openssl library is, since for some reason the installer has trouble locating it:

./configure –openssl-libpath=/usr/lib/ssl

And now, we build – these steps will take some time to complete:

make
make test
sudo make install

We’re done!

Step 6: Testing

First, let’s get our dev box’s IP address so we can browse to it from our host machine:

sudo ifconfig

Write down the IP address. Now – to test that everything is working, we will create a new Rails project, start the server, and make sure we can browse to the welcome page:

cd
rails new sampleproj
cd sampleproj
rails s

This should startup the default WEBrick server on port 3000 (see my post on fixing an annoying WEBrick warning for some alternatives). Go to your host machine, fire up a browser, and point it to http://:3000. You should see the below screen:

Rails welcome screen

Congratulations! You are now the proud admin of a shiny new Debian development box! Have fun!!

Step 7: Bonus Round: Installing Parallels Tools

Very quickly, one last step. Let’s install Parallels Tools, which will allow you to share directories from your host machine to your VM. I keep my Rails apps on my host machine where they are backed up to a NAS. I mount this directory from the VM – this allows me to edit the code on my host machine with my favorite tools (I use TextMate and run the Rails environment in the VM. To install Parallels Tools, go to the Virtual Machine menu in Parallels and select “Install Parallels Tools”. This will make a CD image available to your VM. Inside the VM, mount the CD with program execution enabled:

sudo mount -o exec /media/cdrom

Then run the installer:

cd /media/cdrom
sudo sh ./install

Just hit “Next” through all of the installer screens, reboot when prompted, and you are all set!