Sudo a gem executable does not work on Ubuntu

Posted Almost 13 years ago. Visible to the public.

Today I needed to execute a ruby gem executable with sudo. But, surprisingly, bash would tell me command not found for the gem that ran lovely without sudo.

Gem bins are installed to /var/lib/gems/1.8/bin, which is not in sudo’s PATH. Unfortunately, you can’t change the path, since sudo for Ubuntu is compiled with the --with-secure-path option.

#Solution A: symlink the gems (if you need only some few gems)

  • for each gem you need for sudo, run `ln -s /var/lib/gems/1.8/bin/gem_for_sudo /usr/local/bin/gem_for_sudo

#Solution B: update the path where gem install puts executables (if you need to execute many to all gems with sudo)

  • move all the bins in /var/lib/gems/1.8/bin/ to /usr/local/bin/ (so gem will still be aware of them)
  • edit or create ~/.gemrc and make sure there’s a line reading
    ^
    gem: --bindir /usr/local/bin

#Solution C: overwrite sudo’s secure path anyway

I discourage to do this:

  • run sudo visudo
  • add the line
    ^
    Defaults secure_path=:
Dominik Schöler
Last edit
Over 11 years ago
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2011-05-18 09:51)