Posts Tagged ‘cygwin’

Getting Ruby to talk to Oracle with Cygwin

January 7th, 2009

I need to be able to use Ruby to talk to Oracle databases to help us easily build scripts for maintenance and support. So, I started doing some googling and found ruby-oci8. Excellent!

I’m also mainly PC bound so I needed to get this working on my cygwin installation. My entire Ruby environment is running through cygwin.

So I started through the installation instructions on the ruby-oci8 site but started to find it pretty confusing to follow for the cygwin-specific installation. Finally, after a bit of trial and error, here are the steps I followed to get this baby humming:

  1. Download and unzip the Oracle Instant Client Basic and SDK packages. I successfully did this with version 11.1.0.7.0. The path I chose was:
    C:\oracle\instantclient_11_1
  2. Add the instant client to the front of your system path (through Control Panel –> System)
  3. Launch cygwin
  4. Install the gem
    $ gem install ruby-oci8
    Building native extensions.  This could take a while...
    Successfully installed ruby-oci8-1.0.3
    1 gem installed
    Installing ri documentation for ruby-oci8-1.0.3...
    Installing RDoc documentation for ruby-oci8-1.0.3...

That’s it! You’re now all installed! You’ll notice that the gem compiles some native extensions, this is why you need the instant client to be available in your path.

Now to give it a test run, open up and irb session and give it a go:

irb(main):001:0> require 'oci8'
=> true
irb(main):002:0> conn = OCI8.new('username', 'password', '//server:port/sid')
=> #<OCI8:0x7fdb3d44 @privilege=nil, @prefetch_rows=nil, @ctx=[0, #<Mutex:0x7fdb3cf4>, nil, 65535], @svc=#<ocisvcctx:0x7fdb3ce0>>

We have a connection!