Read more

Sudo a gem executable does not work on Ubuntu

Dominik Schöler
May 18, 2011Software engineer at makandra GmbH

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.

Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more Show archive.org snapshot

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=:
Posted by Dominik Schöler to makandra dev (2011-05-18 11:51)