12

I'm trying to install rubyrep on CentOS, and when executing sudo gem install rubyrep I get the error sudo: gem: command not found. Sudo and gem are both installed and working, just not when combined.

My setting:

[rubyrep]# which sudo
/usr/bin/sudo
[rubyrep]# which gem
/usr/local/rvm/rubies/ruby-2.1.2/bin/gem
[rubyrep]# which ruby
/usr/local/rvm/rubies/ruby-2.1.2/bin/ruby
[rubyrep]# $PATH
-bash: /usr/local/rvm/gems/ruby-2.1.2/bin:/usr/local/rvm/gems/ruby-2.1.2@global/bin:/usr/local/rvm/rubies/ruby-2.1.2/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin: No such file or directory
Kate
  • 712
  • 3
  • 6
  • 23

4 Answers4

21

Try without sudo. You should only use sudo if you did a root installation of ruby, which is not recommended.

ichigolas
  • 7,595
  • 27
  • 50
8

From http://web.archive.org/web/20130405074724/http://www.kleinfelter.com/node/188:

There are hundreds of reports on the net about "sudo: gem: command not found". Out of the dozen or so I looked at, the suggested resolution amounted to "ensure that 'gem' is in your PATH."

Yep. It is in my path. That is to say, the following works:

gem --help

but the following fails:

sudo gem --help

Further confusing matters, gem is in the path shown by:

sudo echo $PATH

The trick is that sudo doesn't use $PATH for its path on some editions of Linux. This behavior is considered "more secure." (See sudo changes PATH - why? ).

Bah! It is still a pain to work around. You have to get 'gem' into the path (not simply the $PATH!) You'll read about options you can pass to sudo, and that is fine and dandy when you type the command-line yourself. When someone's script invokes sudo for you, you're stuck with the command line that it uses.

I fixed it by doing:

alias sudo='sudo env PATH=$PATH'

as suggested in sudo changes PATH - why?.

Community
  • 1
  • 1
user4975679
  • 1,461
  • 16
  • 21
6

sudo env PATH=$PATH gem install rails

It works for me.

2

If anyone runs across this, like I did, and the without sudo isn't an option. You may want to check your sudoers for the 'secure_path'.

If this doesn't include the path of your installation/gem/ruby (which gem). Then you'll also get this error.

Fedora 23, built from source.

Dishcandanty
  • 411
  • 1
  • 6
  • 13