53

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?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197

4 Answers4

115

Use rvmsudo command instead of sudo

calas
  • 1,603
  • 1
  • 12
  • 15
  • 1
    +1 - Thanks calas! This worked awesomely for me. I have many gems that require me to use sudo when running them (such as RubyCAS) and this solved my problem eloquently. Thanks again! – Topher Fangio Oct 25 '10 at 16:54
  • 1
    I needed to update rvm to install the command. `rvm update --head` fixed the problem for me, this was just after a fresh install. – tgandrews Dec 05 '10 at 03:35
  • 1
    Glad I found this. I was pulling out hair. – d11wtq Jun 20 '11 at 11:13
  • 1
    @tgandrews It's `rvm get [stable|latest|etc]` nowadays :) – Tim Baas Apr 18 '13 at 12:44
6

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).

Craig Walker
  • 49,871
  • 54
  • 152
  • 212
1

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: sudo changes PATH - why?

Community
  • 1
  • 1
Brandon Yarbrough
  • 37,021
  • 23
  • 116
  • 145
0

@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
Siwei
  • 19,858
  • 7
  • 75
  • 95