Setting up a virtualized Rails development environment

Posted by on August 30th, 2007.

As a Rails developer currently stuck using a Windows environment, I've run into a number of problems trying to use gems with native extensions such as RMagick, etc. Fed up with taking twice the time to accomplish the same thing, I decided to get a Linux-based server up and running locally. Since I don't have a dedicated box lying around, I decided to try virtualization. The end result is a virtualized Linux server into which I can shell, build apps, and test either locally or remotely.

Acquiring the Appliance

The first thing I needed to do was get The VMware Player. This freely available virtualization solution is widely used and allows for easy setup of virtualized operating systems of any kind. Download and install VMware Player using normal options.

Next, I needed to get my base appliance. While there is an existing Rails Appliance for VMware Player, I found that it didn't give me the flexibility I needed in installing custom gems and caused more problems than it eased. I decided to go with a clean install of Ubuntu Server on top of which I would add everything I needed. Get the Feisty Fawn distro I used here.

Once your distro has finished downloading, extract into desired location and run using the VMware player. The distribution linked in this article has no root, but an account called notroot with a password of thoughtpolice. You should obviously either change the password or create a new account for yourself immediately for security purposes.

Get Rails Up and Running

Now it's time to install ruby, rubygems, rails, and everything else you might need or want for your new web distribution. Note that I am not setting up an elaborate system, just the bare basics needed to run and test rails applications.

Next we're going to set up our distro with all of the things we'll need for a basic Rails development box. Do the following in your command shell (References: urbanpuddle.com, rails wiki):

sudo apt-get update
sudo apt-get install build-essential ruby rubygems irb1.8 ri rdoc ruby1.8-dev mysql-server libmysql-ruby libmysql-ruby1.8 libmysqlclient15off mysql-client-5.0 mysql-common mysql-server-5.0 rdoc1.8 ri1.8 ruby1.8 libmysqlclient5-dev
gem install rails --include-dependencies
gem install mongrel --include_dependencies
gem install mongrel_cluster
echo 'export PATH=$PATH:/var/lib/gems/1.8/bin' >> ~/.bash_profile
source ~/.bash_profile

That giant block of goodness builds and installs everything you need to get started with your development.We're installing Ruby, RubyGems, a MySQL server, and the associated development libraries that will let us build native extensions such as the mysql gem if desired.

Now that we have the basics, you should be able to pop open a directory (I used /var/www) and create your rails application skeletons. Gem installations should work as normal, and you now have a very basic Rails development environment.

SSH and NAT

You may want to be able to shell into your new virtual machine. The first thing you will need to do is install an SSH server:

sudo apt-get install openssh-server

The trickier part comes in configuring the virtual machine's networking to allow external access. First you need to find out the internal IP for your virtual machine. In your shell, type ifconfig and write down your IP. Now, if you are running VMware Workstation, open the "Manage Virtual Networks" item in the VMware Start Menu folder. If you are running the free VMware Player, open up vmnetcfg.exe in your install directory.

There's a lot to this program, but what you want now is the NAT tab. Make sure NAT service is started then click the "Edit..." button. Click through to "Port Forwarding...". Now you need to add port forwarding for any ports you may want accessible outside the virtual machine. To add a forwarded port, go to add and fill in the IP address you wrote down from ifconfig.

I forwarded port 22 for SSH and ports 3000-3005 for my rails development apps. Once you've added all of the ports you wish to forward, hit "OK" and "OK" again and "OK" again. You should now be able to shell into your VM using PuTTy or the like, transfer files with an SCP program such as WinSCP, and visit your Rails apps live once you run a server with script/server.

There's infinitely more to go over in the world of server configuration, but this is a quick and easy way to get a Linux environment on a Windows box.

As a Rails developer currently stuck using a Windows environment, I've run into a number of problems trying to use gems with native extensions such as RMagick, etc. Fed up with taking twice the time to accomplish the same thing, I decided to get a Linux-based server up and running locally. Since I don't have a dedicated box lying around, I decided to try virtualization. The end result is a virtualized Linux server into which I can shell, build apps, and test either locally or remotely.

Acquiring the Appliance

The first thing I needed to do was get The VMware Player. This freely available virtualization solution is widely used and allows for easy setup of virtualized operating systems of any kind. Download and install VMware Player using normal options.

Next, I needed to get my base appliance. While there is an existing Rails Appliance for VMware Player, I found that it didn't give me the flexibility I needed in installing custom gems and caused more problems than it eased. I decided to go with a clean install of Ubuntu Server on top of which I would add everything I needed. Get the Feisty Fawn distro I used here.

Once your distro has finished downloading, extract into desired location and run using the VMware player. The distribution linked in this article has no root, but an account called notroot with a password of thoughtpolice. You should obviously either change the password or create a new account for yourself immediately for security purposes.

Get Rails Up and Running

Now it's time to install ruby, rubygems, rails, and everything else you might need or want for your new web distribution. Note that I am not setting up an elaborate system, just the bare basics needed to run and test rails applications.

Next we're going to set up our distro with all of the things we'll need for a basic Rails development box. Do the following in your command shell (References: urbanpuddle.com, rails wiki):

sudo apt-get update
sudo apt-get install build-essential ruby rubygems irb1.8 ri rdoc ruby1.8-dev mysql-server libmysql-ruby libmysql-ruby1.8 libmysqlclient15off mysql-client-5.0 mysql-common mysql-server-5.0 rdoc1.8 ri1.8 ruby1.8 libmysqlclient5-dev
gem install rails --include-dependencies
gem install mongrel --include_dependencies
gem install mongrel_cluster
echo 'export PATH=$PATH:/var/lib/gems/1.8/bin' >> ~/.bash_profile
source ~/.bash_profile

That giant block of goodness builds and installs everything you need to get started with your development.We're installing Ruby, RubyGems, a MySQL server, and the associated development libraries that will let us build native extensions such as the mysql gem if desired.

Now that we have the basics, you should be able to pop open a directory (I used /var/www) and create your rails application skeletons. Gem installations should work as normal, and you now have a very basic Rails development environment.

SSH and NAT

You may want to be able to shell into your new virtual machine. The first thing you will need to do is install an SSH server:

sudo apt-get install openssh-server

The trickier part comes in configuring the virtual machine's networking to allow external access. First you need to find out the internal IP for your virtual machine. In your shell, type ifconfig and write down your IP. Now, if you are running VMware Workstation, open the "Manage Virtual Networks" item in the VMware Start Menu folder. If you are running the free VMware Player, open up vmnetcfg.exe in your install directory.

There's a lot to this program, but what you want now is the NAT tab. Make sure NAT service is started then click the "Edit..." button. Click through to "Port Forwarding...". Now you need to add port forwarding for any ports you may want accessible outside the virtual machine. To add a forwarded port, go to add and fill in the IP address you wrote down from ifconfig.

I forwarded port 22 for SSH and ports 3000-3005 for my rails development apps. Once you've added all of the ports you wish to forward, hit "OK" and "OK" again and "OK" again. You should now be able to shell into your VM using PuTTy or the like, transfer files with an SCP program such as WinSCP, and visit your Rails apps live once you run a server with script/server.

There's infinitely more to go over in the world of server configuration, but this is a quick and easy way to get a Linux environment on a Windows box.

Share:

3 Responses to “Setting up a virtualized Rails development environment”

  1. Robert Aganauskas

    Do you edit files from Windows or Linux environment? What editor do you use?
  2. Michael Bleigh

    I edit my files in a Windows environment using e Text Editor (it's a TextMate-inspired editor for Windows). I get access to the files through SftpDrive, although you can also just directly edit through SSH with something like WinSCP.
  3. Frank Schwach

    Maybe you know a solution to a problem I'm having: I have a linux server at work and I would now like to work on my RoR apps from home (Windows XP). I set up port forwarding for ports 3000 and 80 through a gateway server and port 80 works fine - when I point my browser on the windows machine to localhost:80 I get my internal website and the Perl CGI apps I've got there. But when I try to connect to port 3000 the browser just hangs (no error message, just waiting for server). Something seems to be happening though because as soon as I disconnect my SSH tunnel or stop the mongrel server for my Rails app the browser immediately comes back with a connection error message. Did you have similar problems connecting to RoR on the virtual machine? I can't find anything in Google that would help me with this problem, so any hint would be much appreciated! Thanks.


Leave a Reply