问题
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
回答1:
Try without sudo
. You should only use sudo
if you did a root installation of ruby, which is not recommended.
回答2:
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?.
回答3:
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.
回答4:
sudo env PATH=$PATH gem install rails
It works for me.
来源:https://stackoverflow.com/questions/24706277/error-sudo-gem-command-not-found