Today we’re going to take a detour into web programming using Ruby on Rails. The hardest part for me has been getting the development environment up and running. Some guides will walk you through setting up Ruby and then Rails directly on your Windows machine, but I abandoned that pretty quickly. You’ll soon grow tired of having to translate 99% of the Rails articles you read from Linux to Windows. I’ve found that running a development server in a virtual machine (more on that later) is a better way to go. It was a somewhat hairy process so we’ll walk through the steps it took to make it work.
What in the world is a “virtual development environment”?
Our goal today is to create our own web server to build and test web applications on. We could go out and buy a web hosting package for $10/mo. somewhere and do our development directly against a public web site but that’s not necessary for introductory development. Until you are ready to publish your application for all to see you can do just fine running a virtual server that looks and feels just like the public server you might pay someone else to set up for you. Click through to see the basics of how this is going to work:
Fig. 1. When you see this welcome screen you'll know you've gotten Ruby on Rails running on your virtual server.
First you’ll set up a virtual machine (VM). This VM is a program that runs on your computer – that pretends to be another computer altogether. This allows you to leverage the benefits of having multiple computers in the same room without actually having to buy extra machines and wire them up. The two components of a virtual machine that you need to be concerned with are the VM software itself and the operating system you’ll be running inside the VM. Once you get the operating system up and running in your VM it will act just like a second computer on your network. You’ll be able to log into the VM, configure it, and run new programs on it that interact with your other computer(s). Today we’re going to install a slimmed down Linux distribution with Ruby on Rails built in and ready for you to work on. When it’s all finished you’ll be able to write Ruby code either on your real machine or on the virtual machine and then transfer it to the virtual Ubuntu system so that you can test it. Testing will work just like any other web application – you’ll open a web browser on your host machine (Windows) and view the application at a URL like http://localhost:3000.
Get VirtualBox
VirtualBox is a free (GPL) virtualization tool from Sun that will allow you to run a “guest” operating system without having to mess with your computer’s existing “host” operating system. For the purposes of this article we’ll be using Windows as the host and Ubuntu Linux as the guest. Download the appropriate VirtualBox installer for your operating system here. I installed VirtualBox using all of the defaults on both Windows Vista and XP laptops.
Create a virtual Linux machine with VirtualBox
Once VirtualBox is up and running you’ll be prompted to create your first virtual machine. Name it ‘Rails’ or whatever you’d like to remember your machine as and be sure to tell it you’ll be using Ubuntu as your guest operating system. See Figure 2 to make sure you’re going down the right path. Click through all the defaults on the rest of the pages until the machine is configured. Now we’ve got to download Linux and install it into our currently empty virtual machine named Rails.
Fig. 3. Tell VirtualBox to let your virtual machine access this Turnkey Rails image you just downloaded.
Head over to Turnkey Linux to pick up a preinstalled Linux server running Ruby on Rails. Click on the Download link and you’ll get a ~170MB .iso file containing the entire preconfigured Rails environment. You’re going to wind up with a file on your hard drive named something like turnkey-rails-2009.03-hardy-x86.iso. Once the file finishes downloading go back to VirtualBox, select your Rails virtual machine, and then click Settings. Click on Network on the side panel and then change the Attached To dropdown from its default of NAT over to Bridged Adapter. This will cause the virtual machine to connect to your local network’s router and demand its own unique IP address. If you’ve got a consumer network (e.g. a Linksys router or anything you’d find at an electronics store) then this should work first try. If that doesn’t work, you can go back to the default of NAT but there will be some extra configuration required to make the virtual server accessible to your host computer.
Click over to CD/DVD-ROM on the VirtualBox settings sidebar and make sure Mount CD/DVD-Rom is checked. Select the ISO image file radio button and then browse to the Turnkey Rails .iso file you downloaded earlier. Once you’re done it will look something like Figure 3.
Install Turnkey Rails
Now your Linux machine should be all “wired” up and ready to start up. Click on the Start button in VirtualBox and you’ll see the “first run wizard” asking you where to find the installation media for your guest OS. Select Image File at the bottom and be sure it’s pointing to the Turnkey Rails iso you’ve just downloaded. Click Next and Finish and then you’ll see the Turnkey setup screen. Select the second option “Install to Hard Disk” and then wait for your Ubuntu Linux installer to load. Select all of the defaults :
- Guided – use entire disk
- Yes, write the changes to disks.
- Create passwords for your Linux and MySQL root users.
- Yes, restart now.
When the machine reboots you’ll want to choose “Boot from First Hard Disk” at the boot menu (You can also go to the VirtualBox settings and disconnect your Turnkey Linux .iso file whenever you get tired of having to manually choose the local disk at boot time). Your Turnkey Linux box will now boot up and you’ll see a blue configuration screen showing you the server’s IP address and the ports you’ll need to use to connect to the server. You’re going to want to do all you can to work across the network so you can get practice using this as a remote development server rather than a second computer sitting right next to you.
If you can’t connect via a web browser using the address shown in the blue Turnkey console (see Figure 4), you’ve probably got something wrong with your VirtualBox network settings. Close the window containing your virtual machine and then get to work on fixing the settings. If you’re in a hurry, try the Bridged network as it should be the easiest to make work in a home / small office environment. If you really must use NAT, yofu’ll need to open up a windows shell (cmd.exe) and set up port forwarding so that your virtual Rails box will be accessible through forwarded ports on your host machine. Follow the steps in this quick guide I wrote up on port forwarding using VirtualBox.
The nitty gritty: final setup from the Linux command line
Hopefully at this point you’ve gotten Turnkey Rails installed using all of the defaults and you’re able to connect to it across the network. Now all that’s left is to update our Linux, Ruby, and Rails software packages to the latest versions and then add a few more pieces that the Turnkey .iso image doesn’t include. Connect to your new machine using an SSH terminal (port 22 if you’re using network bridging; port 2222 if you used the port forwarding guide linked in the previous section). Log in using the root password you specified in the Linux installation step above.
First stop: Update Linux and Ruby using package handlers apt and gem. Enter in the following commands to update all of your Linux and Ruby packages to the latest available versions:
apt-get update gem update --system gem update
Once these complete there are two optional steps that will probably help you out. First is to install sqlite. sqlite is the database that Rails uses by default. While you can use the MySQL server that Turnkey Rails supplies you will find yourself having to mentally translate a lot of the Rails guides you read from the default sqlite commands to equivalent MySQL commands. It’s easy to install sqlite alongside MySQL, so that’s what we’re going to do:
apt-get install libsqlite3-dev sqlite3 gem install sqlite3-ruby
The final optional component is a WYSIWYG text editor. Turnkey Rails comes with Linuxy text editors like vim that require some reading to jump into. If you’re anything like me and you’d rather jump right into coding without having to learn a new text editor UI then you will want to try diakonos (or joe for that matter). Install diakonos using apt:
apt-get install diakonos
Now if you find yourself needing to tweak a config file on your Turnkey Rails server you won’t have to open it up in a file transfer client – you can just fix it on the server.
Next step: Write a Rails application!
Now you’ve got your system all set up and you’ll want to test it by building a simple Rails application. I recommend the First Rails App Walkthrough on the official Rails wiki. Remember, if the walkthrough mentions URLs like http://localhost:3000/users you may need to translate those to the name and port number of your new Turnkey Rails server.
Good luck! I’m sure there are a few gaps and inaccuracies in this writeup and I’d love to hear your feedback and corrections in the comments below.
Related articles by Zemanta
- How To: Virtualize Any OS For Free [How To] (gizmodo.com)
- How to: Create and manage snapshots in VirtualBox (en.onsoftware.com)
- What OS is on Your Netbook? (jkontherun.com)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=1d7a0223-235b-4ef9-b57b-9182c888190e)

