I had the task of setting up a production server on VPS running Ubuntu 12.0.4 LTS. There are quite a number of articles on how to go about doing it. Thankfully they all have detailed instructions. However the frustration came up when you follow exactly the step by step instructions and came up with an error message that makes no sense. I do understand that this is probably due to the difference in OS environment or even OS version. Nevertheless it is a tedious process trying to figure out going back and forth to check my steps to make sure that I have not missed anything along the way.

In summary, the process involves installing the Rbenv first followed by Passenger and then Nginx. This article gives a step by step instruction on this. Quite a straight forward instructions except that it threw up a persistent message that doesn’t make much sense at the time.

After installing Rbenv successfully and verified that it was working, the next time was to run the following  commands to install Passenger and Nginx:

      sudo gem install passenger
      sudo passenger-install-nginx-module

These are the same steps outlined on the Phusion Passenger site.

For some reasons, it threw the following error message:

sudo: gem: command not found

I am certain that I have Rbenv installed and could access the gem command as a user.  But the sudo command just recognize the path apparently. I did a lot of googling before I came upon this sudo issue, which apparently isn’t an issue but a security measure that is a part of some OSes. Sudo replaces the caller’s $PATH environment variable with a predefined one that is safe.

The reason this does not work out-of-the-box is that most operating systems distribute sudo compiled with the secure_path option enabled. This throws away the caller’s $PATH environment variable and replaces it with a predefined list of search paths that are to be considered safe.

You can read more about this in this article by Dan Carley.

So the sudo command does not recognize the Rbenv path and hence complains that RubyGem was not installed. I found it surprising that this issue was not mentioned in the number of articles that touch on this subject of setting up an Ubuntu VPS with Rbenv, Passenger and Nginx. Again I assume that may be due to the fact that they be using a different version of Ubuntu which does not have the sudo security ‘feature’ implemented.

Anyhow the good news is Dan Carley not only highlighted the issue but also created a plugin called Rbenv-sudo that allows you to run rbenv-provided Rubies and Gems from within a sudo session. Kudos to Dan for this. After installing this plugin I was able to complete the installation of Passenger and Nginx without further problem.