Wednesday, 29 February 2012

Getting started with Ruby on Rails and Pow for .Net developers

I recently purchased a MacBook Air, and I wanted to know how easy it is to setup Ruby on Rails and Pow for running the site. Watching the screencast for Pow it looks easy to do. However, getting started on a new platform can be difficult; not knowing commands and where stuff is can be a pain, especially coming from a .Net and Windows world. Even though the Internet is available for finding information it can still be a challenge. As is always the case nothing is as easy as it first seems. The following is what I had to do to get the sample working. This was all done on a clean install of OS X Lion.

Disclaimer this worked for me and I am by no means an expert on OS X, Ruby on Rails or Pow and you may have a different experience.

The first thing you need is Xcode - this is freely available via the App Store. You will need to use the C++ compiler later on. It is possible to use a different C++ compiler but I used Xcode.

Homebrew

The next tool we need is a utility called Homebrew. Basically think of this as Nuget for applications. Installing is easy, simply fire up a Terminal windows and enter the following command:

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

That's it, Homebrew is now ready to go.

Git

Next up is Git; you can either download and install it from the website or use Homebrew in the Terminal window:

brew install git

Ruby

Next is updating Ruby to the latest version, from your Terminal window type:

ruby - v

and you should see that you're running version 1.8.7 (this comes as standard on OS X Lion). We need to update this to the latest version - 1.9.3 (at time of writing). The easiest way to do this is to use the Ruby Version Manager (RVM); this basically makes it easy to manage the different versions of Ruby on Rails you will be using. Again like most tools this is done through the Terminal window:

bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

Before we can start using the command we need to refresh our shell environment with the following command:

source ~/.bash_profile

After all that we are finally at a stage to update Ruby, this is done by typing the following:

rvm install 1.9.3 --with-gcc=clang

The --with-gcc=clang specifies the compiler to use, without this it will not use the Xcode C++ compiler and will error out unless you have another C++ compiler available.

Now if we type:

ruby - v

It shows that we are still using version 1.8.7 not 1.9.3 that we've just installed. We need to tell the system to use the specific new version. When we install any libraries it's always against the version set, this way you can have multiple installs of different versions with different libraries. To use 1.9.3 we can use the following command using RVM:

rvm use 1.9.3

When your system is restarted this is reset to whatever version is set as the default. To change the default so it is the latest version use this command:

rvm --default use 1.9.3

Gems

Gems in Ruby on Rails is like Nuget as it helps to manage all the different packages that you might be using, for example Sqlite3 and Bundler to name a couple. You will be using the Gem command regularly to install various packages.

Rails

After all the commands to update Ruby, installing the latest version of Rails is relatively simple. All it takes is the following command:

gem install rails

This will install the latest version 3.2.1, this can be checked by typing:

rails -v

Bundler

In order to get the Pow example up and running you need to install the Bundle gem. This is done by typing:

gem install bundler

This should install version 1.0.21 (at time of writing). This can be checked by using the -v argument, most packages have this argument to easily check the version you are using.

Pow

Installing Pow is probably one of the simplest tasks and just requires you to type:

curl get.pow.cx | sh

Reddis

What is not shown in the Pow screencast is that you also need to have Reddis installed and running. This is easily installed via Homebrew with the following command:

brew install redis

To run a Reddis server you need the following command:

redis-server /usr/local/etc/redis.conf

This should probably be done in a separate Terminal window as the process needs to continually run.

Now We Are Ready

We now have everything installed, we can clone the Github repository and run the site. To clone the repository run the following command:

git clone https://github.com/defunkt/resque.git

The will clone the repository to the directory you are currently in. Personally I put all my code in a sub-folder located in the root of my user folder called github.

Now we have the repository checkout we need to get all the required gems (libraries). To do this we use Bundler. Navigate to the root of the solution and type:

bundle install

This will connect to http://rubygems.org and get all the required gems.

Next, set up the cloned repository to use Pow. First, navigate to the Pow folder and then run the command to setup the server:

cd ~/.pow

ln -s /Users//github/resque

Note that your folder location might be different.

This has now set up the server - if you go to http://resque.dev you will see the server running. However, if like me you get an error saying it cannot find bundler/setup, this is because we are using rvm and we need to point Pow to rvm. This can be done using the following command:

echo "export POW_RVM_PATH=/usr/local/rvm/scripts/rvm" >> ~/.powconfig

Before the site will work we need to kill the Pow process, the easiest way is using Activity Monitor (go to spotlight and type "Activity Monitor"). Once the Pow process has been killed it will start again automatically.

There you go, the resque site should be up and running now. This is slightly more long-winded than the screencast leads you to believe, but everything is now set up so when you set up your next site all you need to do is run the Pow command.

No comments:

Post a Comment