[Ruby] Use rvmsudo instead of sudo in RVM environment

Tadashi Shigeoka ·  Fri, February 24, 2012

I’d like to share a story about how I resolved an error that occurred when I tried to run a WEBrick server on port 443 using sudo in a Ruby on Rails environment built on RVM.

■ Error Details

$ sudo rails s -p 443
Password:
/Users/your_username/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find rails (>= 0) amongst [minitest-1.6.0, rake-0.8.7, rdoc-2.5.8] (Gem::LoadError)
  from /Users/your_username/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
  from /Users/your_username/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems.rb:1208:in `gem'
  from /Users/your_username/.rvm/gems/ruby-1.9.2-p290@mygemset/bin/rails:18:in `
'

The cause was that the Rails development environment was built using RVM.

It seems that when you need sudo for ruby or rails commands in an RVM environment, you must use rvmsudo instead.

$ rvmsudo rails s -p 443
Password:
=> Booting WEBrick
=> Rails 3.1.0 application starting in development on http://0.0.0.0:443
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-02-23 23:58:43] INFO  WEBrick 1.3.1
[2012-02-23 23:58:43] INFO  ruby 1.9.2 (2011-07-09) [x86_64-darwin11.3.0]
[2012-02-23 23:58:43] INFO  WEBrick::HTTPServer#start: pid=4649 port=443

It worked successfully with rvmsudo.

That’s all from the Gemba.

Reference Information