sudo: gem: command not found

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 http://stackoverflow.com/questions/257616/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.

The hack, reproduced from the stackoverflow article:

­

mv /usr/bin/sudo /usr/bin/sudo.orig
cat > /usr/bin/sudo <<EOF
#!/bin/bash
/usr/bin/sudo.orig env PATH=$PATH "$@"
EOF

­
then your regular sudo works just like the non secure-path sudo.

Add new comment