Time to Learn Something New!

I’ve taken it upon myself to start learning a new programming language and leaving my happy little LAMP (or preferably LNMP) comfort zone. I love PHP and how easy it is to get up and running. Its pretty simple and gets the job done quick. PHP unfortunately lacks a lot of the “grace” and modern nuances that a more modern platform such as Ruby on Rails provides.

Now, before the pitchforks start coming out I already know that I’m kind of comparing Apples to Apple Pie. PHP is a language with a ton of community support and Ruby on Rails is a framework. I get it, but there’s a lot to be desired in PHP land. PHP has some good frameworks as well – CakePHP, Zend, CodeIgniter, Yii, etc. – but the sheer variety creates a lot of discontinuity in the community and a lot of the frameworks out there are just Ruby on Rails wannabe’s anyways. More importantly besides any of that, PHP is getting a little boring and I want to learn something new 🙂 So, I’m starting to learn Ruby, Sinatra and eventually Ruby on Rails and share my learning here.

Environment Setup

So, at work I use a Mac, at home I use a PC and I dual boot between Windows 7 and Ubuntu. I personally prefer Ubuntu, but sometimes I have to have access to Photoshop and Photoshop through Wine sucks. From what I’ve found, the best way to run a development Ruby environment is through a VM installation of Ubuntu Server. It can be a little tricky to setup, but I’ve chronicled how to get yourself going with a Ruby development environment that will work on an Ubuntu server and Mac OS (variations in installation noted where appropriate).

Getting Started on a Mac

First things first if you’re running a Mac – get your system setup for development. Install a few key tools to get your system ready for developing and utilizing the command line:

Install Homebrew to give you access to a missing app repository that gives you the ability to install tools for development much like Ubuntu’s apt-get system.

Install RVM

RVM (Ruby Version Manager) will allow you to maintain individual version controls of Ruby, Rails and Gems on a per-project/folder basis. Very useful when working on multiple projects of varying maturity.

Install RVM itself.

1

2

3

curl-Lhttps://get.rvm.io | bash -s stable --auto

This will install RVM in your user’s folder and will allow you to run any future ruby, or gem commands without the need for sudo. For a few steps later this is important to keep in mind as the version of Ruby installed by RVM is the one that will need to be used, not the version of Ruby that comes with your operating system.

Set RVM’s Default Ruby Version.Check the version of Ruby that was installed by running ruby -v. This will output your installed major version of Ruby including the patch number that will look something like:

1

2

3

ruby1.9.3p327(2012-11-10revision37606)[x86_64-linux]

Set RVM to use the version seen there by running:

1

2

3

rvm--defaultuse1.9.3-p327

Take note of the added “-” between the major version number and the patch number.

Installing Rails

With RVM installed you’re ready to run Ruby on your machine, the next step is installing the Rails framework. This can easily be accomplished with our recently installed gem library that was installed with Ruby.

Install Rails.Simply run:

1

2

3

gem install rails-v3.2.9

Install Node.The new Rails 3 asset pipeline offers support for server-side JavaScript compilation of JavaScript and CoffeeScript files. I recommend installing Node.js for this processing. This also has the added benefit of installing some support libraries on Ubuntu that will allow you to use the SetEnv Apache command in your LAMP stack to specify a development environment. To install Node.js in MacOS:

1

2

3

brew install node

In Ubuntu:

1

2

3

sudo apt-get install nodejs

Ubuntu Preparation.On Ubuntu machines there are some ancillary packages to install if you plan on running Ruby on Rails with MySQL or PostgreSQL databases in your local development environment. Install these with the following command:

1

2

3

sudo apt-get install libmysqlclient-dev libpq-dev

Install Phusion Manager

If you have a LAMP or LNMP stack already installed on your machine you can also install Phusion Passenger for easy Virtual Host management in your development environment. This will allow you to simply create virtual hosts and run multiple Ruby on Rails, Sinatra or other Rake/Ruby based applications at the same time accessible via their virtual host domain maps. There are a few tricks to note about running Phusion Passenger with RVM, so be sure to follow the modified instructions here for installation:

Install Phusion Passenger. Go to https://www.phusionpassenger.com/download and follow the installation instructions at the bottom of the page for your LAMP or LNMP stack with one major exception – do not run ANY of the commands with the sudo command. Since you installed RVM earlier, you want Phusion Passenger to work with your RVM installation not your operating system’s default Ruby installation.

Install the Passenger Module. After running the appropriate passenger module installation instructions from step 2 on the Phusion Passenger installation page, be sure to follow any instructions given by the installer for adding lines to your Apache configuration files. These lines will automatically be set to use your RVM installed copy of Ruby so you don’t need to modify them. Base your virtual host configurations based off the example given by the Passenger installer. You can also consult the Phusion Passenger documentation for more details – Apache or Nginx – for more detail on setting up for various frameworks.

Configure Your Local Environment. By default Rails and Rack environments are set to production when using Passenger. If you are running Apache you can simply set the environments to development in your domain’s <VirtualHost>tag:

1

2

3

4

5

6

# Ruby on Rails environment

SetEnv RAILS_ENV development

# Rack environment (Sinatra and other Ruby frameworks)

SetEnv RACK_ENV development

Alternatively, this can go in an .htaccess file when running Apache or even right next to your Passenger module load lines if you want all your environments to be development.

Speed up Passenger.Out of the box, Passenger is slow as mollasses. This of course negates much of the benefit of running something like Passenger, so lets speed things up. Add these lines next to your Passenger module load lines:

Special Modifications For Virtual Box Installations. If you have installed Ubuntu Server in a Virtual Box VM and have your development workspace available to the server as a permanent Shared Folder, you will need to make some user permission modifications. As the Shared Folder mount will be owned by the vboxsf group, you will need to make sure that both Apache and Phusion Passenger are run as part of that group. Modify the APACHE_RUN_GROUP value to be vboxsf in your /etc/apache2/envvars file to tell Apache to run itself as a user belonging to the vboxsf group. To tell Phusion Passenger to run under a different group, just add the line PassengerGroup vboxsf right next to the other lines you added to your Apache configuration in step 2.

That’s it! You should now have a fully functional development environment ready for Ruby on Rails

Hehe, pretty recently. Since we decided to focus on RoR as our primary platform now, its becoming more and more relevant to be operating normally in a Nix based environment. Besides, a 16GB equipped Mac mini is a pretty inexpensive little power house to work on 🙂