Ruby: sudo, root, rvmsudo

When playing around with Ruby (and Rails) you’ll see web pages that mention different ways of gaining elevated privileges.

E.g. some may mention ‘root’, others ‘sudo’ and yet others ‘rvmsudo’.

So, what to use where?

Here’s a simple guide. Run:

`which ruby`

If you see a path with /home or /Users then Ruby is installed in your home directory and you may not need to use elevated privileges.

Otherwise, switch to a root shell as follows:

– using RVM? Run `rvmsudo -s`

– not using RVM? Run `sudo -s`

– sudo not installed? Run `su -c bash`

What’s going on?

sudo starts a new subshell that not contains environment variables.

rvmsudo passes on any environment variables that RVM set up to get you to the correct Ruby. This includes the $PATH as well has $GEM_HOME, $GEM_PATH and $BUNDLE_PATH variables.


If possible you want to avoid using rvmsudo as it allows gems to install code with elevated privileges.

Leave a Reply

Your email address will not be published. Required fields are marked *