New installation of Ruby Enterprise Edition - "gem" recognised but "sudo gem" isn't

989 views Asked by At

I've just installed Ruby Enterprise Edition on a fresh Ubuntu 10.04 server. I went to install my first gem, and hit a weird problem I never saw before.

Without sudoing, if I just type 'gem' I get the blurb about gem. I can switch to root and do gem install gemname or give the full path to the gem exe, but if I do sudo gem install gemname I get an odd error sudo: gem: command not found.

If I do gem install gemname without sudo, it tells me I don't have permission to alter the gem folder (which is fair enough). It looks like something funny's happening when I sudo, basically.

I've added the Ruby bin path to my PATH variable in my .bashrc file, like so:

export PATH=/opt/ruby-enterprise-1.8.7-2011.01/bin:$PATH

then reloaded .bashrc with source. Any ideas, anyone?

3

There are 3 answers

1
Ken Li On

try sudo -s and then gem install gemname.

Long Answer .bashrc file only affects the bash shell you are in, and sudo does not know about it because sudo does not directly look at your .bashrc. You can use sudo -s to get around this. You can also use sudo -E to try to preserve the environment while running sudo commands.

Using /etc/profile to change your PATH instead of .bashrc can also work as well.

0
Calvin On

If you're using RVM, use rvmsudo gem install gemname.

2
Keating On

Try this:

gem install sqlite3-ruby