Given how developer friendly as Ruby on Rails is, getting a development environment up and running can be surprisingly tricky. I will describe the process that I consider to be “best practice” here, step by step. Although my guide will be focused on Ubuntu, the general idea should be the same on other distros as well. Also, please take note of the date of this post. If you’re reading this even a few months after I wrote this, don’t be surprised if your mileage will vary.
First off, we’re going to need Ruby, right? So let’s install that, and also throw in curl (we’re going to need it in a bit):
Update: actually, you don’t need Ruby installed on your system at all. Installing curl is enough here:
1
|
|
Next, we’re going to install RVM. RVM is, at the website puts it, a Ruby Version Manager. Basically, it allows you to install multiple versions of Ruby in your home folder and use them seamlessly (as if they were the default Ruby in the system). This is useful for us because the version of Ruby Ubuntu installed is 1.8.7 (as of this writing), while the latest stable version of Ruby is 1.9.3. RVM also allows us to keep separate collections of gems (called gemsets, for obvious reasons). This is very useful when working with multiple projects that may have conflicting dependencies.
Installing RVM is dead simple. Just run the command under Quick Install:
1
|
|
Warning: the above command might not be the latest one. Make sure to double check with the RVM website.
Now, close the current terminal and open a new one. This is so that RVM can load into the new bash session. Now, let’s see what RVM suggests we should install:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
Install the dependencies suggested by RVM for Ruby (see the marked lines above and make sure to use sudo).
Good. Now it’s time to install Ruby! Again. The next command will install the latest stable Ruby into your home directory (so the system is not affected). Beware, it will take a long time to complete, as it downloads the Ruby source code and compiles it!
1
|
|
Now, if the command above finishes with a message similar to:
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
Please visit https://rvm.io/integration/gnome-terminal/ for a solution.
Then do visit that URL and follow the instructions there. Finished? Good. Open a new terminal, again. Now type:
1
|
|
If all went well, you should see a green message telling you which Ruby is being used. Next, you should create a gemset (we talked about them in the beginning) for your new project, and also switch to it:
1
|
|
You finally have your environment set up. Next step is to install the rails gem:
1
|
|
Note: the —no-rdoc —no-ri parameters are passed so that it doesn’t waste time installing docs that are available on the Internet anyway. If you wish to have them installed for some reason, just omit those parameters.
Good, now you’re ready to create your new Rails app! Simply type:
1
|
|
To run the new app:
1 2 |
|
You just got an error now, didn’t you? Did it sound like this?
1
|
|
What’s happening is that Rails needs a JavaScript runtime (well, duh, it says so right in the error message!). What it needs it for, from what I can tell, is the new CoffeeScript (and LESS?) functionality added in the asset pipeline in Rails 3.1. To fix this, just require a JavaScript runtime gem in your app. We’re going to use one called “therubyracer”. Open up the file called “Gemfile” and add this line to it:
1
|
|
Now, in order to actually install that gem, just run:
1
|
|
in your project’s directory. The bundle command basically looks in your Gemfile and installs / updates dependencies based on it. Running the app should now actually finally ultimately work:
1 2 3 4 5 6 7 8 |
|
Now, every time you open a new terminal and intend to run the project, you will have to:
1 2 3 4 |
|
If you want to skip the two rvm
commands, you can use an .rvmrc
file. That, however, is beyond the scope of this post, and is documented here.
As I was saying, surprisingly tricky. Sure, you could have used the Ruby and Rails provided by your package manager, but given the rapid development of both, you would be several major versions behind and lacking many features. Also, managing multiple projects is now a breeze. Just install the required Ruby (some projects might rely on older versions of it), create a new gemset for it, install dependencies, run.
Whew!