!!! Update !!!
New version of this instruction for Intel Macs with Leopard is available here.
Introduction
I have been using Oracle technologies for many years but just some time ago discovered Ruby and Rails. As I had ideas how to make Ruby on Rails frontends for existing Oracle based systems I started to explore how to use Ruby on Rails together with Oracle databases.
Just recently I switched from PC notebook to MacBook Pro and unfortunately found out that Oracle Instant Client is not yet released for Intel Macs. As there is no promise when it will be released and as I could not wait for that I decided to make old PowerPC version of Oracle Instant Client to run on Intel Mac. As I didn’t find any good description how to do that I decided to write description of it by myself - hopefully it will help others.
Universal or “fat” binary Ruby
The first thing is that you need to get “universal” or “fat” binary installation of Ruby - it means that it contains both Intel and PowerPC (PPC) binary code. You will need PPC version of Ruby when you will work with Oracle and you will need Intel version of Ruby when you will do other things. PPC code is running in emulation mode on Intel processors and therfore is slower as well as there are some compatibility issues with some other libraries (I will mention later what issues I found out).
It is possible to get and install precompiled Ruby either from MacPorts or using Ruby One-Click Installer. But I prefer to compile Ruby from source code as it gives me more control what is installed and where. I used Hivelogic guide to compile Ruby from source code.
Here are my additional notes what I changed to Hivelogic guide to make “fat” binaries.
Installing “readline”
At first you need to install “readline” shared libraries. At first I tried to make them from “readline” library source code but I always got just Intel binaries. Therefore I found and downloaded “readline” library installer package with universal binaries and installed it. If you previously installed Intel binary of “readline” library then it is better to restart Mac to ensure that new dynamic library will be loaded.
Compiling Ruby
Next you need to download and extract Ruby 1.8.6 source code.
Before running “configure” command I made the following change in “configure” file:
# Choose a default set of architectures based upon platform.
case "$target_os" in
darwin*)
TARGET_ARCHS="ppc i386"
;;
You can specify this also on command line but this change ensures that you will not forget it :) Then you need to run “configure” script with additional parameter at the end:
./configure --prefix=/usr/local --enable-pthread --with-readline-dir=/usr/local \ --enable-shared --enable-fat-binary
After running “make” hopefully you will get everything compiled and you should get “fat” ruby binary. You can verify it with the following command and should see the following result:
$ file ruby ruby: Mach-O universal binary with 2 architectures ruby (for architecture ppc): Mach-O executable ppc ruby (for architecture i386): Mach-O executable i386
If you do not see that ruby binary contains both ppc and i386 executables then something went wrong. If it is OK then you can do “sudo make install” to install binaries in target directories.
Make PPC and “fat” versions of Ruby
As you will need to be able to force to run PPC version of Ruby later then we need to extract PPC executable in a separate file and store original “fat” binary in another file:
ditto -arch ppc /usr/local/bin/ruby /usr/local/bin/ruby_ppc mv /usr/local/bin/ruby /usr/local/bin/ruby_fat
Then I recommend to create simple scripts that will help you to switch between “fat” and PPC versions of Ruby:
ppc_ruby.sh:
#!/bin/bash sudo ln -fs /usr/local/bin/ruby_ppc /usr/local/bin/ruby
fat_ruby.sh:
#!/bin/bash sudo ln -fs /usr/local/bin/ruby_fat /usr/local/bin/ruby
So when you need to have PPC version of Ruby then run “ppc_ruby.sh” script and when you need “fat” version (which will actually run Intel binary) then run “fat_ruby.sh” script.
Install Oracle Instant Client for PPC
Install Oracle Instant Client according to the following description.
Compile and install ruby-oci8
I used the following description as a basis but some additional changes were needed for ruby-oci8-1.0.0-rc3 compilation.
Open ruby-oci8 README file and find section “=== Intel Mac” where are described what to do to compile ruby-oci8 on Intel Mac.
Before running any ruby scripts you need to run ppc_ruby.sh script to switch to PPC binary.
If you compiled “fat” Ruby from source code then you need to modify file /usr/local/lib/ruby/1.8/fat-darwin8.10.1/rbconfig.rb according to README file (make backup of file before modifications). If you installed Ruby from binary distribution then find where is located your rbconfig.rb file.
- find lines with CONFIG["CFLAGS"] and CONFIG["LDFLAGS"] and CONFIG["ARCH_FLAG"]
- remove “-arch i386″ if present (was not present in my case)
- and add “-arch ppc” to all of these lines.
Make and install ruby-oci8 and if it finishes successfully then restore rbconfig.rb file from backup.
Try
ruby -r oci8 -e "OCI8.new('scott', 'tiger','orcl').exec('select * from emp') do |r| puts r.join(','); end"
or similar to verify that you can access Oracle database from ruby.
Congratulations! You have managed to connect to Oracle DB from Ruby on Intel Mac! If you got some issues at some point then please write it in comments and I will try to help with that.
Post installation notes
If you previously installed (or if you will install in future) any Ruby gems with native extensions (e.g. Mongrel or MySQL C API) then you need to reinstall these gems with “fat” Ruby (i.e. running “fat_ruby.sh” script and then “sudo gem install”). It will ensure that all gems will also have “fat” native extensions so that you can run them both in PPC Ruby and Intel Ruby.
Known issues with PPC Ruby
So far I have got the following issues with running PPC version of Ruby:
- Capistrano is failing to make SSH connection when running in PPC Ruby (I was testing with new Capistrano 2.0). Therefore you need always to switch to “fat” Ruby before running “cap deploy”.
- As I also wanted that MySQL is running under PPC I got some issues with MySQL native extensions compilation - I solved them using the following description.
- ruby-ldap connections were failing when running in PPC Ruby within Ruby on Rails. I managed to fix it by putting “require ldap” as a first line in config/environment.rb file.
Great resource Raimonds!! Your tutorial came at a very timely moment for me. I have spent the day trying to get ruby and oci to work together on my intel mac but am still unable to get it to work.
First I took your advice and downloaded the readline installer (so that I get both ppc and i386 versions). My first question is: how can I verify that readline is installed properly? I since have tried compiling my own version, but am running into the problem that you mentioned … I’m only getting the intel files (so I went with the installer).
Next I went ahead and downloaded the one-click ruby installer and verified that I had the universal binary. I was able to create all the symlinks and ruby seemed configured properly.
I then installed and configured the oracle client. I used /opt/oracle/instantclient10_1/ as my sandbox for oci. I then changed rbconfig.rb (which resides in /usr/lib/ruby/1.8/universal-darwin8.0/rbconfig.rb on my machine) to reflect the CONFIG flag changes. My rbconfig.rb file did not have the -arch flags defined, so I simply added “-arch ppc” to both the CFLAGS and LDFLAGS options. Unfortunately, I am still unable to succeed in running:
$ ruby setup.rb config — –with-instant-client=/opt/oracle/instantclient10_1/
I am confident that my ruby executable is ppc as I can verify by:
$ which ruby
/usr/local/bin/ruby
$ file /usr/local/bin/ruby
/usr/local/bin/ruby: symbolic link to `/usr/local/bin/ruby_ppc’
$ file /usr/local/bin/ruby_ppc
/usr/local/bin/ruby_ppc: Mach-O executable ppc
I am seeing some strange error messages, however. For one, the console output of setup.rb prints:
Oracle doesn’t support intel mac.
This doesn’t concern me too much, however. An error that does concern me is:
checking for OCIInitialize() in oci.h… no
Perhaps relatedly, I am seeing multiple warnings in ext/oci8/mkmf.log:
/usr/bin/ld: warning /usr/local/lib/libruby-static.a archive’s cputype (7, architecture i386) does not match cputype (18) for specified -arch flag: ppc (can’t load from it)
This thread mentions a recompile of ruby as a solution:
http://rubyforge.org/forum/message.php?msg_id=14841
This doesn’t strike me as a solution, however, because the ruby executable I’m running seems to be ppc based. It seems like more of an environment configuration problem.
Any information you can possibly provide would be a great help to me! Please feel free to ask for more information … I can provide whole files via email should you request. Also feel free to correspond with me via email.
Thank you in advance!!,
Fellow Mac Purist =D
Comment by degzhaus — August 30, 2007 @ 2:45 am
I updated my tutorial with additional modification that you need to do to rbconfig.rb that helped you to solve the issue.
Comment by Raimonds Simanovskis — August 30, 2007 @ 10:19 pm
Great stuff, man. Thanks a lot!
Comment by Carlos Villela — September 19, 2007 @ 10:20 am
Note that you can use Locomotive (http://locomotive.raaum.org/) with a PPC bundle, on your Intel Mac, and just add the Oracle PPC Instant Client, and compile in the OCI8 driver, and you are good to go. It’s a little simpler than this. I’ve been working with this setup for about 18 months now, and it works for me.
People have been asking for Intel Mac Oracle drivers for a lot longer than the thread you posted (Mar 2007) indicates. This was posted by one of my coworkers in Apr 2006 - http://forums.oracle.com/forums/thread.jspa?threadID=378474&tstart=0
Comment by Lori M Olson — September 24, 2007 @ 1:37 am
To degzhaus
I got your situation,too.
well,read oncemore Riamond text
>find lines with CONFIG[”CFLAGS”] and CONFIG[”LDFLAGS”] and CONFIG[”ARCH_FLAG”]
>remove “-arch i386″ if present (was not present in my case)
>and add “-arch ppc” to all of these lines.
perhaps you didnot remove all “-archi386″.
CFLAGS and LDFLAGS and ARCH_FLAG
I forgot ARCH_FLAG,and “ARCH_FLAG = -arch ppc”
you can success,too
regard
Comment by Yasuhiro Kitayama — October 10, 2007 @ 4:28 pm
thanks for the post!
i have been having an error when i do make:
ruby.c
ruby.c: In function ‘ruby_init_loadpath’:
ruby.c:320: error: syntax error before ‘__ARCHITECTURE__’
ruby.c:327: error: syntax error before ‘__ARCHITECTURE__’
make: *** [ruby.o] Error 1
I am actually trying to rebuild on leopard… i wiped out the ruby that came with it…
thanks in advance!
Comment by keith — April 1, 2008 @ 7:15 pm
If you want to install ruby-oci8 on Leopard then I recommend to use original ruby installation on Leopard and make these additional steps - http://www.foliosus.com/2007/11/19/connecting-ruby-on-rails-to-oracle-on-an-intel-mac-in-leopard-mac-osx-105/
If you still want to compile ruby on Leopard from scratch then you can use guidelines from http://hivelogic.com/articles/ruby-rails-leopard/ or http://michael.biven.org/2008/01/25/installing-ruby-on-rails-on-mac-os-105-or-patching-dan-benjamins-guide/
I compared both approaches on Leopard and found out that original ruby installation was working faster and therefore used it.
And in addition I can tell that this guide will not be necessary soon as I have already got Oracle Instant Client beta for Intel Mac and it is promised that it might be available for all during a month.
Comment by Raimonds Simanovskis — April 1, 2008 @ 11:18 pm
thanks so much raimonds!
greatly appreciated
Comment by keith — April 2, 2008 @ 4:20 am
Good post.
Comment by Rhona — October 27, 2008 @ 3:17 pm