Cannot use RVM-installed Ruby with sudo

RubyRvmSudo

Ruby Problem Overview


I have succefully configured RVM to use Ruby 1.9.2 and everything is fine. However when I'm trying to run Ruby using sudo it says it cannot find RVM or Ruby:

$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]

$ sudo ruby -v
[sudo] password for administrator:
sudo: ruby: command not found

Is that correct behavior or is my RVM misconfigured? Perhaps I should be using the system wide install?

Ruby Solutions


Solution 1 - Ruby

Use rvmsudo command instead of sudo

Solution 2 - Ruby

Taking @calas' answer one step further, add this to your ~/.bashrc:

alias sudo=rvmsudo

This avoids the requirement of having to remember to change sudo to rvmsudo for every command (particularly annoying for copy/pasted commands).

Solution 3 - Ruby

Sudo is resetting your PATH. rvm works by modifying your PATH environment variable so that the ruby you're using is a particular one, probably in your ~/.rvm/rubies directory. When you ruby ruby with sudo, sudo is not using that modified PATH, and so it will not find your rvm ruby. See this question for workarounds: https://stackoverflow.com/questions/257616/sudo-changes-path-why

Solution 4 - Ruby

@calas's answer is the answer. but If you can't achieve your goal by using rvmsudo, e.g. install the nginx using the command passenger-install-nginx-module, please just change the owner of the related folder/path. e.g.

$ chown <your_username>:<your_group> /opt -R
$ passenger-install-nginx-module

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexey ZakharovView Question on Stackoverflow
Solution 1 - RubycalasView Answer on Stackoverflow
Solution 2 - RubyCraig WalkerView Answer on Stackoverflow
Solution 3 - RubyBrandon YarbroughView Answer on Stackoverflow
Solution 4 - RubySiweiView Answer on Stackoverflow