# [HOWTO] Installation of Ruby; integration in Apache; Rails

## cprior

Installation of Ruby; integration in Apache; Rails

The mid-term break called for sth. productive and I decided to poke around in Ruby.   *ruby-lang.org wrote:*   

> Ruby is the interpreted scripting language for quick and easy object-oriented programming. It has many features to process text files and to do system management tasks (as in Perl). It is simple, straight-forward, extensible, and portable.

 

Most of the current packages required are in portage.

At first, the language itself:

```
emerge dev-lang/ruby
```

This depends on dev-ruby/ri (interactive help), dev-ruby/ruby-config, dev-ruby/text-format. Interestingly, ruby-config also depends on an older version of dev-lang/ruby, which gets installed in another slot. I do not know (yet) if that's desired  or a bug; however, it did not hurt with the first steps either. Update: This behaviour changed and ruby-config no longer installs a slotted dependency. (Currently ruby-1.8.2 and ruby-config -0.3.1 on 11th of february 2005)

Now you can already use the interactive Ruby shell: Start it with irb --simple-prompt and you will be greeted by an unspectacular prompt >> . I made an alias in .bashrc for the shortcut rb right away: 

 *In ~/.bashrc I  wrote:*   

> alias rb='clear && irb --simple-prompt'

 

To just explore the langauage this installation is already enough. I recommend the online book Programming Ruby, The Pragmatic Programmer's Guide, First Edition, aka PickAxe. If you feel that the Euro is especially strong and the Dollar cheap, you might as a citizen of the Euroarea consider buying the new one for 25 USD. Update: Make sure to get the 2nd edition. 

You can safely stop at this point and start enjoying the language.* The following is by no means necessary for a first impression as the interactive shell and the possibility to have files interpreted is more than enough for a short mid-term break! ;-) Anyhow, as Gentoo users have been said to "compile it, if it moves!", I could not resist at all:

To prepare Ruby for higher tasks, we make it capable of talking to some databases, adding the PostgreSQL extension to package.keywords -- the example is for x86 machines:

```
echo "=dev-ruby/plruby-0.4.2 ~x86" >> /etc/portage/package.keywords && \

emerge mysql-ruby plruby ruby-dbi
```

And finally we want Ruby to work with Apache:

```
emerge eruby mod_ruby
```

What eruby does is enabling embedded Ruby in otherwise html files, similar to embedded PHP. It is usually configured in Apache for the file suffix .rhtml and might look like this:

 *In ruby.rhtml I wrote:*   

> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
> 
> <html><head><title>eruby example</title></head>
> 
> <body>
> ...

 

In contrast to this we can also use Ruby just like cgi scripts, the following setup will prepare for these kind of files to be executed:

 *In ruby.rbx I  wrote:*   

> #!/usr/bin/ruby
> 
> puts "Hello World"

 

In the meanwhile the emerge should have finished. Just like a PHP install, emerge does tell us to add an option to /etc/conf.d/apache2.conf If you have already PHP installed, make it look like this.

 *In /etc/conf.d/apache2 I  wrote:*   

> APACHE_OPTS="-D PHP4 -D RUBY"

 

But just as a PHP install, emerge does _not_ tell us to load the appropriate module in /etc/apache2/conf/apache2.conf: I just added this line right underneath my similar looking php4_module directive. Update: The ebuild now spits out the following remark:

 *Quote:*   

> * To enable mod_ruby, edit /etc/conf.d/apache2 and add "-D RUBY"
> 
> * You may also wish to edit /etc/apache2/conf/modules.d/20_mod_ruby.conf
> 
> * You must restart apache for changes to take effect

 

It is no longer necessary to add a directive for loading the module.

The change to /etc/conf.d/apache2 enables mod_ruby, but not eruby, the embedded interpreter. We have to comment out the lines in /etc/apache2/conf/modules.d/20_mod_ruby.conf from the line RubyRequire apache/eruby-run on, the block about Location and Files included.

We also need to comment out the block after # exec *.rbx as ruby scripts.

I assume that every Gentoo installation has mod_rewrite enabled in

/etc/apache2/conf/apache2.conf:

 *In /etc/apache2/conf/apache2.conf I  wrote:*   

> LoadModule rewrite_module modules/mod_rewrite.so

 

Most computer problems originate 60cm away from the screen and thou shalt not forget to do the requested

```
/etc/init.d/apache2 restart
```

Now you can put the above files ruby.fhtml and ruby.rbx somewhere under your document root. Caveat: If you did not use cgi scripts in your ~/public_html or whatever a possible userdir on your machine might be, you better go for somewhere in /var/www/localhost/htdocs. (This will be from now on referred to as $DOC_ROOT) **

So a browser request to http://localhost/ruby.rhtml will count for you from 1 to 10, if you used my example from above.

To install the highly praised framework Ruby on Rails http://rubyonrails.org on our machine, we need at first RubyGems, which is not on portage. So we download the latest version from http://rubygems.rubyforge.org/ and unpack it to some place: Update: now available via Bugzilla. I therefore deleted all old instructions and replaced them with the updated (11th of february 2005) instructions below:

At first we need the ebuilds -- bug no. 80828 lists them all in its dependency tree:

 *Quote:*   

> Bugs that bug 80828 depends on
> 
> ( view as bug list)
> 
> 80823 [ruby@gentoo.org] - gems.eclass (Ruby gems installer).
> ...

 

Download them all and put them in your overlay, e.g. /usr/local/portage:

```
cd /usr/local/portage  && mkdir actionmailer actionpack activerecord rake

# snip: copy the downloaded ebuilds in their directory

# and create the checksums

# [b]gems.eclass belongs to /usr/portage/eclass/[/b]

ebuild /usr/local/portage/dev-ruby/rails/rails-0.9.5.ebuild digest

ebuild /usr/local/portage/dev-ruby/rake/rake-0.4.15-r1.ebuild digest

ebuild /usr/local/portage/dev-ruby/actionpack/actionpack-1.4.0.ebuild digest

ebuild /usr/local/portage/dev-ruby/activerecord/activerecord-1.6.0.ebuild digest

ebuild /usr/local/portage/dev-ruby/actionmailer/actionmailer-0.6.1.ebuild digest
```

So all that is left now is to

```
emerge -v rails
```

which typically shows dependencies like:

```
# emerge rails -vp

These are the packages that I would merge, in order:

Calculating dependencies ...done!

[ebuild  N    ] dev-ruby/sqlite-ruby-2.2.2  55 kB

[ebuild  N    ] dev-ruby/ruby-postgres-0.7.1  28 kB

[ebuild  N    ] dev-ruby/rubygems-0.8.4  132 kB

[ebuild  N    ] dev-ruby/activerecord-1.6.0  0 kB [1]

[ebuild  N    ] dev-ruby/actionpack-1.4.0  0 kB [1]

[ebuild  N    ] dev-ruby/actionmailer-0.6.1  0 kB [1]

[ebuild  N    ] dev-ruby/rake-0.4.15-r1  0 kB [1]

[ebuild  N    ] dev-ruby/rails-0.9.5  +mysql +postgres +sqlite 0 kB [1]

Total size of downloads: 216 kB

Portage overlays:

 [1] /usr/local/portage
```

You should now be able to install a first test application. I created a directory at first:

```
export DOC_ROOT=/var/www/localhost/htdocs/

mkdir $DOC_ROOT/rails

rails $DOC_ROOT/rails
```

The default paths in some of the installed files have to be changed. Replace all shebang lines with #!/usr/bin/ruby or nothing will work. Update: No longer necessary.

Test if the installation was successful:

```
htdocs $ ls rails/

CHANGELOG  Rakefile  config  doc  log     script  vendor

README     app       db      lib  public  test
```

Now you should be greeted by a page beginning with "Congratulations, you're on Rails!" by browsing to http://localhost/rails/public/ (depending on your Apache setup the trailing slash is important)

For the sake of completion, the following lines will show if Rails is working:

Create a sample database in MySQL:

```
mysql -u root -p -h localhost

mysql> CREATE DATABASE rails_production;

mysql> CREATE DATABASE rails_test;

quit
```

Edit config/database.yml to reflect your setup.

And create a basic application (taken from the included help) 

```
script/new_controller Blog list display new edit
```

A simple test can be performed typing

```
rake
```

A first Rails-generated page can be accessed with

http://localhost/rails/public/dispatch.cgi?controller=Blog&action=display

It should look somewat like

Blog#display

Find me in app/views/blog/display.rhtml

If not, and no careful reading of my above script-excerpt help upon second examination, feel free to annotate your suggestions for enhancements.

If your browser shows permissions problems or even the plain file contents, try ruby filename on the file. It should interprete the file and you know at least it's Apache-related.

---------------

* I love the type "ranges", how you can "extend" a class or module by kinda appending, found the yield iterator interesting and like the overall logic. But I am just "interested public", no programmer.

** Hint: To enable Apache to access your ~/public_dir, add apache to your users group and chmod g+x /home/yourUsername. Apache needs to traverse into it. Notice how g+x does not imply read access.Last edited by cprior on Fri Feb 11, 2005 8:14 pm; edited 5 times in total

----------

## drinian

I might add that RubyGems is now in Portage.

Also, you can install the latest version of Rails simply by typing:

```

gem install -r rails

```

Thanks for the HOWTO!

----------

## Dolio

The Pickaxe is an excellent book. However, if you're going to actually buy it, make sure you get the second edition, with all sorts of new and updated stuff.

ruby-doc.org is also a good online reference if you need brief descriptions of methods and classes.

----------

## nazgum

I really like ruby and saw rails and knew I must install this =)

It "seems" like I have this working, except I am having some problems with it.

Anyone able to post their apache configuration they have for this?

I'm certain that is where my problem is.

Cheers!

Edit: I fixed this by using vhosts from their tutorial rather then trying it without[/b]

----------

## nazgum

hehe well I thought I got this all working, except it is not =p

Following the excellent ruby on rails tutorial at:

http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=3

You make a recipe example.

I've setup the database and the controller,

Browsing to rails/Recipe

Does list items already in the database - the links beside the item for show edit and destroy all do not work.  The new at the bottom also does not work.

Going to rails/Recipe/new

Does bring me to a page to add info to my mysql database with the proper fields, but filling them out and clicking create does NOT bring me to the list view and does NOT add the information to my mysql database.

Anyone available for help on this one? =)

I'd really love to get this working, as it looks like a very nice tool =)

Edit: ALL of my problems stemmed from http://rails/Recipe to http://rails/recipe   [MUST be lowercase]

ehe tutorial had it uppercase and i presumed that is how it must be, so if anyone else gets stuck on that this is your answer.[/b]

----------

## Pythonhead

I've written a gems eclass so we can install Rails and the four packages it depends on via ruby gems:

https://bugs.gentoo.org/show_bug.cgi?id=80828

Be sure to look at the bug's dependency tree and install all the ebuilds if you want to try it out.

EDIT: dev-ruby/rails is now in portage

----------

## Pythonhead

Here is a quick HOWTO for creating a very simple cookbook using Ruby on Rails with sqlite and Ruby's lightweight web server. (This is the 2 minute Linux version of the above mentioned onlamp.com Windows 5 page tutorial ;)

```
USE='sqlite' emerge dev-ruby/rails
```

As your regular (non-root) user:

```
rails Cookbook

cd Cookbook

{$EDITOR} config/database.yml

(replace with my database.yml below)

${EDITOR} db/schema

(paste my schema below)

sqlite db/recipes_test.db < db/schema 

./script/generate model Recipe

./script/generate controller recipe

${EDITOR} app/controllers/recipe_controller.rb 

Edit file to look like:

class RecipeController < ApplicationController

    model :recipe

    scaffold :recipe

end

./script/server

```

Point your browser at: http://localhost:3000/recipe/

Test your new cookbook app. This is very basic, but you can see how easy it is to 

get a web app started. You can modify the SQL table, add some new fields and then

simply refresh the browser and the form will be updated without changing any code.

When you're ready to go into production, check out Cookbook/README on how to get it

working with apache. 

Sample code:

config/database.yml

```

development:

  adapter: sqlite

  dbfile: db/recipes_test.db

test:

  adapter: sqlite

  dbfile: db/recipes_test.db

production:

  adapter: sqlite

  dbfile: db/recipes_test.db

```

db/schema

```

CREATE TABLE 'recipes' (

'id' INTEGER PRIMARY KEY NOT NULL,

'title' VARCHAR(255) DEFAULT NULL,

'description' VARCHAR(255) DEFAULT NULL,

'instructions' TEXT DEFAULT NULL

);

```

----------

## emlyn

I've been following the howto at the top of this thread (which is very nicelly written btw), and I seem to have gotten myself stuck when it comes to getting mod_ruby to load. I'm running apache-2.0.54-r5, which is not a 'stable' version, but at the moment using ~x86 for apache is necessary if one wants to run PHP5. I have experience with mod_php, but I"ve never used ruby before.

What Works

I've emerged ruby, and it works.

I've emerged eruby, and mod_ruby, and I can used eruby directly. For example, for example, I've put the eruby script provided in the howto into /var/www/localhost/htdocs/rubytest.rhtml, and if I call;

eruby /var/www/localhost/htdocs/rubytest.rhtml

I get the expected output. 

What Doesn't Work

If I try and browse to that same location (http://localhost/rubytest.rhtml), I see the source of the script, not the result. And no, I didn't forget to restart apache, in fact I've done it every step of the way.  :Smile: 

I believe that mod_ruby isn't being correctly loaded. I have added the -D RUBY to /etc/conf.d/apache 2, and I don't see any errors in the apache2 error_log when I restart it. However, I look at a page generated by the phpinfo() function (which lists all loaded apache modules), I don't see mod_ruby.

I suspect that this may have something to do with the current changes in apache configuration. I currently have module config files in two directories; /etc/apache2/conf/modules.d and /etc/apache2/modules.d. I seem to have the file 20_mod_ruby.conf in both of them. Suggestions would be welcome. My eventual goal is to try and install rails. I've tried messing round with fastcgi and I didn't even get this far (mainly because I didn't understand the process). I'd rather use mod_ruby anyway, because it seems more similar to what I do with php. I've tried re-emerging various packages, but that doesn't help

thanks

Craig

----------

## Pythonhead

In this file :

/etc/apache2/conf/apache.2.conf

Do you have?

```
LoadModule ruby_module                /usr/lib/apache2-extramodules/mod_ruby.so
```

And in /etc/apache2/conf/modules.d/20_mod_ruby.conf

```

<IfDefine RUBY>

  <IfModule !mod_ruby.c>

    LoadModule ruby_module    extramodules/mod_ruby.so

  </IfModule>

</IfDefine>

# taken from the example file

# If the ruby module is installed, this will be enabled.

<IfModule mod_ruby.c>

  # for Apache::RubyRun

  RubyRequire apache/ruby-run

  #Required by rails:

  RubySafeLevel 0

  # exec files under /ruby as ruby scripts.

  <Location /ruby>

    SetHandler ruby-object

    RubyHandler Apache::RubyRun.instance

    Options ExecCGI 

  </Location>

  <Files *.rb>

    SetHandler ruby-object

    RubyHandler Apache::RubyRun.instance

  </Files>

</IfModule>
```

You may need to uncomment this:

```

  # # handle *.rhtml as eruby files.

  # <Files *.rhtml>

  #   SetHandler ruby-object

  #   RubyHandler Apache::ERubyRun.instance

  # </Files>
```

The important line to get mod_ruby working with Rails is this:

```
  RubySafeLevel 0
```

fastcgi can be a pain to configure, but if you find mod_ruby too slow it will be worth the effort to setup fcgi. Check my configs in this post:

https://forums.gentoo.org/viewtopic-t-303278-highlight-modruby.html

----------

## emlyn

Thanks Pythonhead,

I don't appear to have a /etc/apache2/conf/apache.2.conf file. Or in fact a file of that name in any directory.

However, in /etc/apache2/httpd.conf there are many LoadModule statements, for things like mod_auth and heaps of others. 'External modules', such as mod_php, don't seem to be there, instead there is the line:

```
Include conf/modules.d/*.conf
```

I presume that is loading all files matching that pattern. Certainly that should include the mod_ruby configuration file which is present at:

```
/etc/apache2/conf/modules.d/20_mod_ruby.conf
```

The load module bit in this file looks like this:

```
<IfDefine RUBY>

  <IfModule !mod_ruby.c>

    LoadModule ruby_module    extramodules/mod_ruby.so

  </IfModule>

</IfDefine>
```

That .so file is present in my system at "usr/lib/apache2-extramodules/mod_ruby.so", so I've tried changing the above block to:

```
<IfDefine RUBY>

  <IfModule !mod_ruby.c>

    #LoadModule ruby_module    extramodules/mod_ruby.so

    LoadModule ruby_module      /usr/lib/apache2-extramodules/mod_ruby.so

  </IfModule>

</IfDefine>
```

...just in case. But this doesn't seem to help. Perhaps mod_ruby is being loaded correctly, and it is the rest of the configuration which is wrong (although that doesn't explain mod_ruby not being listed on the phpinfo() page). 

The rest of my 20_mod_ruby.conf file looks like this:

```
<IfModule mod_ruby.c>

  # for Apache::RubyRun

  RubyRequire apache/ruby-run

  # Requred by rails:

  RubySafeLevel 0

  # exec files under /ruby as ruby scripts.

  <Location /ruby>

    SetHandler ruby-object

    RubyHandler Apache::RubyRun.instance

    Options ExecCGI

  </Location>

  # exec *.rbx as ruby scripts.

  <Files *.rbx>

    SetHandler ruby-object

    RubyHandler Apache::RubyRun.instance

  </Files>

</IfModule>
```

I've tried changing the *.rbx section to *.rb so that it matches your example, but that doesn't help. I've also tried adding an identical section for *.rhtml but that doesn't help either. The file I'm testing against is a .rhtml file, so I'm not sure if apache is being told to execute that as eruby.

So I'm still stuck. Just to confirm again, my -D RUBY is definatelly in place, and I'm definatelly remembering to restart apache2.

thanks,

Craig

----------

## NorthGoingZax

I followed this howto, and I am getting the following in my error.log: 

[code]

Fri Jul 15 22:34:07 2005] [notice] Apache/2.0.54 (Gentoo/Linux) PHP/4.3.11 mod_ruby/1.2.4 Ruby/1.8.2(2004-12-25) configured -- resuming normal operations

[Fri Jul 15 22:34:15 2005] [error] mod_ruby: error in ruby

[Fri Jul 15 22:34:15 2005] [error] mod_ruby: (eval): (eval): uninitialized constant Apache::ERubyRun (NameError)

[Fri Jul 15 22:34:15 2005] [error] mod_ruby:   from (eval):0:in `value'

[/code]

What am I missing?

----------

## Dizzutch

I'm getting the same error, not sure what's going on with it.

----------

## Dizzutch

I just uncomented everything in /etc/apache2/modules.d/21_mod_ruby.conf

```

<IfDefine RUBY>

  <IfModule !mod_ruby.c>

    LoadModule ruby_module    modules/mod_ruby.so

  </IfModule>

</IfDefine>

# taken from the example file

# If the ruby module is installed, this will be enabled.

<IfModule mod_ruby.c>

  # for Apache::RubyRun

  RubyRequire apache/ruby-run

  # exec files under /ruby as ruby scripts.

  <Location /ruby>

    SetHandler ruby-object

    RubyHandler Apache::RubyRun.instance

    Options ExecCGI

  </Location>

  # exec *.rbx as ruby scripts.

  <Files *.rbx>

    SetHandler ruby-object

    RubyHandler Apache::RubyRun.instance

  </Files>

  # # for Apache::ERubyRun

   RubyRequire apache/eruby-run

  #

  # # handle files under /eruby as eRuby files by eruby.

   <Location /eruby>

     SetHandler ruby-object

     RubyHandler Apache::ERubyRun.instance

     Options ExecCGI

   </Location>

  #

  # # handle *.rhtml as eruby files.

    <Files *.rhtml>

      SetHandler ruby-object

     RubyHandler Apache::ERubyRun.instance

   </Files>

  # # for Apache::ERbRun

   RubyRequire apache/erb-run

  #

  # # handle files under /erb as eRuby files by ERb.

   <Location /erb>

     SetHandler ruby-object

     RubyHandler Apache::ERbRun.instance

     Options ExecCGI

   </Location>

  # # for debug   

   RubyRequire auto-reload

</IfModule>

```

this works for now, I'll figure out later what exactly it was.

----------

## metalhedd

I've managed to get ruby to work properly, the test script works, apache doesn't seem to have any problem with its config.  the issue I'm having is in getting rails working.  I've created a directory in my /var/www/localhost/htdocs/intranet/ called links (to manage a collection of links) create the rails framework, generated the database tables etc. basically followed the recipe tutorial to a tee. however apache just gives me a directory listing when I browse http://localhost/intranet/links/  I dont know how to actually access my rails program through apache.

----------

## Pistos

```
uninitialized constant Apache::ERubyRun (NameError)
```

 I recently had this problem on two servers.  The problem is, you need to uncomment this:

```
RubyRequire apache/eruby-run
```

in order for Apache to load eRuby.

It seems this Apache upgrade has caused the old 20_mod_ruby.conf file to get replaced with a default 21_mod_ruby.conf file, which has lots of things commented out.

----------

## realAzrael

same problem here, emlyn

i dont have a file /etc/apache2/conf/apache.2.conf 

/etc/apache2/httpd.conf

looks at /etc/apache2/modules.d/ for modules, but the file was in /etc/apache2/conf/modules.d/20_mod_ruby.conf

so i cp to the right location and got this error:

```
# cp /etc/apache2/conf/modules.d/20_mod_ruby.conf /etc/apache2/modules.d/

# /etc/init.d/apache2 restart

 * Apache2 has detected a syntax error in your configuration files:

Syntax error on line 3 of /etc/apache2/modules.d/20_mod_ruby.conf:

Cannot load /usr/lib/apache2/extramodules/mod_ruby.so into server: /usr/lib/apache2/extramodules/mod_ruby.so: cannot open shared object file: No such file or directory
```

i found that file at /usr/lib/apache2-extramodules/mod_ruby.so

i could change that line, but i copied the file to the right place

```
# mkdir /usr/lib/apache2/extramodules

# cp /usr/lib/apache2-extramodules/mod_ruby.so /usr/lib/apache2/extramodules/
```

Restarted Apache and here we go! eruby works

i can browse on my ruby.rhtml and see the result

i tried with ruby.rbx, but my browser keeps saying:

 *Quote:*   

> Forbidden
> 
> You don't have permission to access /ruby.rbx on this server.

 

any ideas?

----------

## realAzrael

uhm, i forgot the Options ExecCGI in my /etc/apache2/modules.d/20_mod_ruby.conf

```
 <Files *.rbx>

    SetHandler ruby-object

    RubyHandler Apache::RubyRun.instance

    Options ExecCGI

 </Files>
```

works fine now

----------

## - LeChuck -

I followed this instruction a few times, already re-installed apache, and still get as *.rbx output a plain text instead of html. I absolutely have no more clue. My 21_mod_ruby.conf looks correct, also vhosts.conf. I tried ExecCGI at .htaccess or vhosts.conf, without success. Eruby runs perfect btw. I'm no apache guru, and may have overseen something (mime, scriptalias, cgi-bin, ... don't know). With FastCGI I got same. Apache logs show no errors.

----------

## paulfleetwood

I have gotten mod_ruby working (instead of eruby from the cgi-bin directory per the Pragmatic Programmers book (http://www.rubycentral.com/book/web.html)).

Here are the steps I performed.

I added index.rhtml to the end of the DirectoryIndex line in /etc/apache2/httpd.conf file (just like the book says), like so:

```
DirectoryIndex index.html index.html.var index.rhtml
```

This enables apache to find index.rhtml as the "base" document of a folder, in my case "http://127.0.0.1/rubytest".

But that only showed the ruby code, and didn't execute it.   For that to happen, I also had to (again, as the book instructs), add "-D RUBY" to the "APACHE2_OPTS=" line in /etc/conf.d/apache2.

But, that was not enough.  I also had to edit the /etc/apache2/modules.d/21_mod_ruby.conf file so that these lines were not commented out:

```
RubyRequire apache/eruby-run

<Files *.rhtml>

     SetHandler ruby-object

     RubyHandler Apache::ERubyRun.instance

</Files>
```

Apparently, the RubyRequire line above is what makes the mod_ruby actually take the place of eruby.  And the <Files...> section says if a file ending with *.rhtml is encountered, it should be interpretted with eruby.

Because of these changes, the "AddType" and "Action" lines that the book suggested were not necessary.  The instructions with the book work fine as-is, but don't deliver the performance improvement they mention, which comes from mod_ruby.

----------

## flaab_0n

I got it!! Yeah!!

Thanks a lot!

----------

