15

There's several pages on the web that discuss this, but most are out of date or inaccurate in some way.

What's the scoop?

iconoclast
  • 21,213
  • 15
  • 102
  • 138
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465

4 Answers4

38

Build ruby, gem, and rails

as per http://rubyonrails.org/download:

build ruby
build gem
use gem to install rails

Get Oracle Instantclient

Download from https://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html

You need these two packages for your architecture.

instantclient-basic
instantclient-sdk

Unzip these files, and make this link

cd instantclient_10_2
# .dylib for mac, .so for linux
ln -s libclntsh.dylib.10.1 libclntsh.dylib

Build ruby-oci8

Note, JRuby users don't need ruby-oci8, but do need the Oracle JDBC jar, either ojdbc6.jar or ojdbc5.jar depending on whether you have Java 6 or Java 5.

Download from http://ruby-oci8.rubyforge.org/en/index.html and run

# DYLD for mac
export DYLD_LIBRARY_PATH=/path/to/instantclient_10_2
# LD for linux
export LD_LIBRARY_PATH=/path/to/instantclient_10_2
ruby setup.rb config
ruby setup.rb setup
ruby setup.rb install

Test with this line and your database connection string.

ruby -r oci8 -e "OCI8.new('scott/tiger@orcl').exec('select * from user_tables') do |r| puts r.join(','); end"

Install activerecord-oracle_enhanced-adapter

Note, not activrecord-oracle-adapter as many older pages mention.

gem install activerecord-oracle_enhanced-adapter

Do that sweet rails thing

rails railstest
cd railstest
# edit config/database.yml as below
ruby script/generate scaffold comic title:string issue:integer publisher:string
rake db:migrate
ruby script/server

Test in browser

<http://localhost:3000/comics>

config/database.yml

Use database if you have a TNS entry, otherwise use host. Note that you have three entries (devel, test, production) to update.

development:
    adapter: oracle_enhanced
    database: orcl           # format is tns-name entry
    host:  myorclhost/orcl   # format is hostname/instance-name
    username: scott
    password: tiger

References

Siwei
  • 19,858
  • 7
  • 75
  • 95
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
  • When I run the "ruby -r oci8 -e..." test line, terminal shows the error: "oci8.c:654:in oci8lib_210.bundle: ORA-12154: TNS:could not resolve the connect identifier specified (OCIError)". Any clues what I need to do? – Sean Ahrens May 02 '17 at 21:25
  • @SeanAhrens, try the instantclient connect string format. Here's a note on what it looks like: http://stackoverflow.com/questions/764916/oracle-what-is-the-instantclient-connection-string-format – Mark Harrison May 03 '17 at 20:47
  • 2
    to install ruby-oci8, come here: https://github.com/kubo/ruby-oci8. the original link is broken. – Siwei May 25 '17 at 10:06
  • 1
    Also, the first link is broke, to download sdk come here: https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html – Siwei Dec 04 '18 at 13:08
3

In my case I already had Ruby, gem, Rails and Oracle client on Windows 7. I installed ruby-oci8 binary package for Windows 32-bit:

Source: http://ruby-oci8.rubyforge.org/en/file.install-binary-package.html

gem install ruby-oci8
gem install --platform x86-mingw32 ruby-oci8

And then:

gem install activerecord-oracle_enhanced-adapter

and you are ready to do Rails on Oracle.

Perception
  • 79,279
  • 19
  • 185
  • 195
Boris Lopez
  • 516
  • 8
  • 14
3

Some additional links to previous answer.

If you are on a Mac then you can follow tutorial How to setup Ruby and Oracle Instant Client on Mac OS X to get access to Oracle database from Ruby.

Then you can read ActiveRecord Oracle enhanced adapter wiki to get Oracle connectivity in Ruby on Rails. This adapter is used in many Ruby on Rails on Oracle projects and is under active maintenance.

I also regularly post about Ruby and Oracle at my blog.

Raimonds Simanovskis
  • 2,948
  • 1
  • 21
  • 17
1

Just an update to reflect current versions (as of writing: August 2015):

  • Install libaio (on Ubuntu/Debian Linux probably with apt-get install libaio-dev - 0.3.109-4)
  • Download Oracle Instant Client (12.1.0.2.0, Basic + SDK should be enough, have not yet tried Basic Lite instead of Basic though)
  • Set environment variables (fit to your needs):

    export LD_LIBRARY_PATH=/path/to/the/Instant/Client/directory
    export NLS_LANG="German_Germany.WE8ISO8859P1"
    
  • Ruby (2.2.1p85), gem (2.4.6), Rails (4.2.3), ruby-oci8 (2.1.8, https://github.com/kubo/ruby-oci8) and activerecord-oracle_enhanced-adapter (1.6.2, https://github.com/rsim/oracle-enhanced) can be installed the usual way (I used rvm (1.26.11) for this - https://rvm.io/), so basically this should do the job for the three gems:

    gem install rails ruby-oci8 activerecord-oracle_enhanced-adapter
    
  • Create your Rails app

  • Configure your Rails app:

    • in Gemfile add activerecord-oracle_enhanced-adapter and ruby-oci8:

      gem 'activerecord-oracle_enhanced-adapter'
      gem 'ruby-oci8'
      
    • in config/database.yml add your Oracle environment:

      development:
        adapter: oracle_enhanced
        host: my-db-host
        port: my-port
        database: my-db
        username: my-user
        password: my-password
      
    • Let Bundle grab the gems: bundle install:

      ...
      Using activerecord-oracle_enhanced-adapter 1.6.2
      ...
      Using ruby-oci8 2.1.8
      ...
      
    • Ready to rumble:

      rails generate scaffold book title:string author:string
      rake db:create db:migrate
      ...
      

Note:

Depending on your Oracle environment (i.e. my-user already exists or has to be created), you can omit the db:create statement or not. In the latter case, rake prompts you for the SYS/SYSTEM password in order to be able to create the user and grant it the corresponding privileges.

tschlein
  • 662
  • 1
  • 8
  • 26