I’ve been coding in Ruby on Rails for the past couple of months. It was a fun and sometimes tough journey (and still is sometimes) learning a new language and platform. My main background is Java, but last year I was fed up with that. It was too hard to do simple stuff. I reckoned that there should be an easier way. Plus, learning a new language broadens your vision and it helped me to improve my programming skills. This blog post is the first of a series (probably, if there’s enough interest) towards implementing a Rails application. I’d like to give you a heads up of what I discovered head first.
In this first post I’ll introduce you how to setup your machine to be able to rock some Rails code.
1. Rubies please
Instructions how to get Ruby for your environment you can find here. On a Mac it is installed by default. You can easily find it out by executing the ‘ruby’ command in your shell and see what happens.
Image may be NSFW.
Clik here to view.
No worries if this isn’t the latest version available. We will only use it to get you bootstrapped.
2. RVM
RVM (Ruby Version Manager) is a tool to help you to be able to use multiple Ruby environments on one machine. If you have multiple Ruby projects on your machine it is quite handy to be able to let them run in their own (sort of) sandbox. Simply put: if you don’t use RVM all dependencies (gems) will be installed system-wide. You just don’t want that as things will get messy. Given you have successfully installed Ruby, you will now install RVM. This short installation step summary requires you having git installed. If not you can view the full installation guide here.
$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
Edit your ~/.bash_profile to contain the following:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
Now start a new shell and you should be able to perform something like
$ type rvm | head -1
This should give you
rvm is a function
Next, and last step, execute
rvm notes
This gives you an overview of what to do next for your environment. On a Mac, be sure to have the right XCode version installed as said in the output.
Now what did this do?
You got the version manager tool installed, but what about the bash_profile stuff? What this small line does is making the rvm command available on your PATH. Handy, you can now run it anywhere you go. Also, it helps you to switch environments easily when navigating through different Ruby projects. We’ll get into that later. Moving along to the next step…
3. Install Ruby within RVM – creating your first RVM environment
Let’s install the latest Ruby version within RVM:
$ rvm install 1.9.2-p180
This is the latest version at the time of writing. You can go here to verify if there exists a newer version.
If you run
$ rvm use 1.9.2
$ which ruby
you should get something similar to the following output:
Image may be NSFW.
Clik here to view.
So…
From now on, all the dependencies or Ruby versions you will install will be placed in the RVM folder that’s located in your home folder. If you install stand alone gems (equivalent of JARs) or upgrade dependencies this will only affect the currently used Ruby installation stored in your home folder. If you mess up, there’s nothing to worry about :).
Almost there. Going well so far. You’ve got the latest and greatest Ruby version within RVM. You’re almost ready to rock…
4. Install Rails and create your first application
Rails is a web framework for Ruby. Install it by running
$ gem install rails
Create a new application
$ rails new example
This will create a new folder containing your first Rails application. Plus it has created a lot of stuff for you. It’s out of scope of this post to cover fully what happened. Just remember this generated a skeleton and a kick start for your first application.
5. Bundler (Maven for Ruby)
Bundler helps you to maintain dependencies similar as what Maven does for Java. You cannot start the example application without retrieving the required dependencies. Retrieve the dependencies by:
$ cd example $ bundle install
6. Rock and roll
Your first application is there! It might have been a boring road with nothing much to see. But… you’ve got an application that is ready to start. Give it a kick by
$ rails server
Now open a browser pointing to http://localhost:3000. You should see this screen:
Image may be NSFW.
Clik here to view.
Wrapup
Setting up development environments isn’t the most fun you can have. But doing the above gives you enormous flexibility. By using RVM instead of installing everything system wide makes it easy to switch Ruby versions, be flexible with the dependencies – and not be surprised by versioning issues over different projects. There’s more to RVM than described above, but this is enough to give you a heads up.
If you like this to be the first of a series, please let me know!