7

When I require certain gems, some of them work and some of them don't work. They seem to install find and I can do a gem list and they are there. However, when I try to run them in Irb or run my ruby programs from the terminal I get the following:

LoadError: no such file to load -- sanitize

/Library/Ruby/Site/1.8/rubygems/custom_require.rb:54:in `gem_original_require'

And so I followed the guide on RubyGems and everything seems fine:

noahclark$ gem list sanitize
*** LOCAL GEMS ***
sanitize (2.0.3)


noahclark$ ruby -rubygems -e 'require "sanitize"'
noahclark$ 

noahclark$ which ruby
/opt/local/bin/ruby
noahclark$  gem env | grep 'RUBY EXECUTABLE'
- RUBY EXECUTABLE: /opt/local/bin/ruby

So then I tried:

noahclark$ which gem
/opt/local/bin/gem

noahclark$ gem environment
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.10
- RUBY VERSION: 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin10]
- INSTALLATION DIRECTORY: /opt/local/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /opt/local/bin/ruby
- EXECUTABLE DIRECTORY: /opt/local/bin
- RUBYGEMS PLATFORMS:
    - ruby
   - x86-darwin-10
 - GEM PATHS:
    - /opt/local/lib/ruby/gems/1.8
    - /Users/noahclark/.gem/ruby/1.8
 - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
    - :benchmark => false
    - :backtrace => false
    - :bulk_threshold => 1000
 - REMOTE SOURCES:
    - http://rubygems.org/

You can see that which gem and gem environment are looking at two separate directories. I hunted around for a fix and I could only find something that suggested adding export PATH=$PATH:/opt/local/bin/gem to my .bashrc file. This did not fix it.

How should I go about fixing this?

Noah Clark
  • 8,101
  • 14
  • 74
  • 116

2 Answers2

4

Based on the path in your error message, your irb and apps are probably using the default system ruby.

What does which irb show? And what is the commandline you use to run your apps?

You may need to adjust the shebang line (the first line which starts with "#!") of your scripts to use /opt/local/bin/ruby.

Kelvin
  • 20,119
  • 3
  • 60
  • 68
  • which irb results in /opt/local/bin/irb – Noah Clark Sep 14 '11 at 16:29
  • 1
    @noahclark What error does this show (if any): `irb -rubygems -r sanitize` – Kelvin Sep 14 '11 at 16:41
  • Weird, I ran that it worked. I had restarted my computer to make sure that .bashrc was loaded, but that didn't help. Not sure what I did or why it works now but it does! Thanks! – Noah Clark Sep 14 '11 at 16:50
  • I just tried doing it and it was fixed before and it now flipped back so I'm having issues even though I haven't changed anything. Any ideas? – Noah Clark Sep 14 '11 at 17:55
  • 1
    @noah Do you mean `irb -rubygems -r sanitize` is failing? What's the error? Are you using a different terminal session than you did when it succeeded? Did you prepend /opt/local/bin (not /opt/local/bin/gem) to your PATH in .bashrc and restart the term session? – Kelvin Sep 14 '11 at 18:07
  • @noah i should also ask, what does `echo $PATH` return in the terminal prompt? – Kelvin Sep 14 '11 at 18:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3450/discussion-between-kelvin-and-noahclark) – Kelvin Sep 14 '11 at 18:11
3

Try adding this to your .bashrc?

export PATH=/opt/local/bin/gem:$PATH

That gives the your new gem path the first crack rather than the current, incorrect default.

peakxu
  • 6,667
  • 1
  • 28
  • 27
  • 1
    @noahclark How did you install ruby? If you used macports, it should've changed your ~/.profile so "/opt/local/bin" was first. You do have to exit any terminal session and open new ones - if you didn't, you might've installed some gems under the system ruby. – Kelvin Sep 14 '11 at 16:45