No such file to load –rubygems

September 28, 2011

So you are getting this error when you try to start or server or if you’re in irb and do a “require ‘rubygems'”, but you can clearly see that gem -v works and you can even list your gems with “gem list”… So why can irb find rubygems?!

Well, in my case, it seems ruby was looking for rubygems in the same directory as itself and could not find it there, but did not continue to search in other directories. Namely:

which ruby # => /opt/local/bin

which gem # => /usr/local/bin

The solution, though hard to find, was simple. Use the same directory for gem and ruby. Since I had a version of ruby in /usr/local/bin I simply had to reorder the preference in which to search for binaries putting /opt/local/bin (MacPorts) at the end instead of the beginning. To do this I opened up ~/.bash_profile and changed

export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/var:$PATH
to
export PATH=$PATH:/opt/local/bin:/opt/local/sbin:/opt/local/var
If you are unable to see the difference, in the second line I have $PATH at the beginning instead of at the end.