Learning Rails: Installing Rails, Part 1 – Debian

Table of Contents

Before I can write code, I need to install Rails.  The best place to get started is the Ruby on Rails Download page. I work on both a Windows box and a Debian box, so I’ll need to install Rails on both machines. This time, I’m going to focus on installing Rails on the Debian box, which is actually the server where this project will eventually run. When I’m doing a lot of command line work, I prefer to work on the Debian box.

Installing Ruby and RubyGems

Note: I’m running a clean install of Debian Squeeze for this project. If you aren’t, your steps might differ. Follow the instructions at the Rails download page.

First, I install ruby.

$ sudo apt-get install ruby
Reading package lists... Done
...snip...
Setting up ruby1.8 (1.8.7.302-2squeeze1) ...
Setting up ruby (4.5) ...

$ ruby --version
ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]

That looks good. Now I need RubyGems.

RubyGems is the standard package manager for ruby.  If you need a Ruby library, like Rails, there’s a good chance it’s available as a gem. So, I download and install the latest version.

$ wget http://rubyforge.org/frs/download.php/75309/rubygems-1.8.10.tgz
...snip...
2011-10-19 21:49:25 (351 KB/s) - `rubygems-1.8.10.tgz' saved [249683/249683]

$ tar -xzvf rubygems-1.8.10.tgz
...snip...

$ cd rubygems-1.8.10

$ sudo ruby setup.rb
RubyGems 1.8.10 installed
...snip...
RubyGems installed the following executables:
        /usr/bin/gem1.8

That was pretty easy.

$ gem --version
-bash: gem: command not found

Where is it? It turns out the installer places the binary at /usr/bin/gem1.8.  If you want to refer to it as gem, create a symlink.

$ sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

And that should be it.

$ gem --version
1.8.10

Installing Rails

Now that I’ve installed Ruby and RubyGems, I can install the rails gem.

$ sudo gem install rails
...snip...
Fetching: json-1.6.1.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing rails:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.8 extconf.rb
extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
        from extconf.rb:1

What? I have no idea what that means, but Google tells me I need to install the ruby1.8-dev package, which has the Ruby development headers.

$ sudo apt-get install ruby1.8-dev
Reading package lists... Done
...snip...
Setting up ruby1.8-dev (1.8.7.302-2squeeze1) ...

After some research, it turns out that some of the gems are actually written in C and only distribute the source code. When you install a gem, the system must compile the source into a binary, which is then installed. The Debian ruby package doesn’t include all the headers required to build some of the gems.

So I try again.

$ sudo gem install rails
Building native extensions.  This could take a while...
...snip...
Successfully installed rails-3.1.1
5 gems installed
...snip...

$ rails --version
Rails 3.1.1

Testing

I’ve installed Rails, but I need to test it. First, I create a test application with rails new testapp.

$ rails new testapp
      create
...snip...
         run  bundle install
Fetching source index for http://rubygems.org/
...snip...
Installing sqlite3 (1.3.4) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.8 extconf.rb
checking for sqlite3.h... no
sqlite3.h is missing. Try 'port install sqlite3 +universal'
or 'yum install sqlite3-devel' and check your shared library search path (the
location where your sqlite3 shared library is located).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

It looks like I don’t have sqlite3 installed, which Rails uses as its default database engine.

$ sudo apt-get install sqlite3 libsqlite3-devel

So I try to generate the Rails app again.

$ rm -rf testapp

$ rails new testapp
      create
...snip...
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled \
        gem is installed.

Good. Now that the application is generating successfully, I try to run the development server.

$ rails server
/usr/lib/ruby/gems/1.8/gems/execjs-1.2.9/lib/execjs/runtimes.rb:47:in `autodetect': \
        Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs \
        for a list of available runtimes. (ExecJS::RuntimeUnavailable)
from /usr/lib/ruby/gems/1.8/gems/execjs-1.2.9/lib/execjs.rb:5
...snip...
from script/rails:6:in `require'
from script/rails:6

Rails is attempting to use a javascript runtime via execjs, which looks for the most appropriate runtime installed on the machine. Unfortunately, Debian doesn’t have one installed by default. There is a node.js package available for Debian sid, so I use that. I follow the instructions here.

$ sudo sh -c "echo deb http://ftp.us.debian.org/debian/ \
> sid main > /etc/apt/sources.list.d/sid.list"

$ sudo apt-get update

$ sudo apt-get install nodejs

Once that’s done, I fire up the Rails development server again.

$ rails server
=> Booting WEBrick
=> Rails 3.1.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-10-20 04:45:51] INFO  WEBrick 1.3.1
[2011-10-20 04:45:51] INFO  ruby 1.8.7 (2010-08-16) [x86_64-linux]
[2011-10-20 04:45:51] INFO  WEBrick::HTTPServer#start: pid=11702 port=3000

WEBrick starts correctly, and when I visit the page in my browser I get the rails start page. It looks like Debian is finally ready to go.

Awesome. But, I’m not done installing Rails.  I still need to install it on my Windows laptop.  If you have any comments or suggestions, let me know. Tune in next time!

  1. Anonymous says:

    Thanks for this, helped me save a lot of time.
    Expecting also for Win and other OS.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>