I have installed rbenv on an Ubuntu sever. I can cd into my application directory and run $ bundle
without issue, however I need to run $ sudo bundle exec
Not entirely sure, but maybe this would work for you:
sudo -i -u $USER bundle exec...
or
sudo -i -u username_with_correct_env bundle exec...
Why you get the error has been addressed already. But I was able to get around it by saying:
sudo /full/path/to/bundle exec ...
In my case, I'm using rbenv, so I had to:
sudo /home/renier/.rbenv/shims/bundle exec ...
That worked. To get sudo to not ask for a password, you would need to configure your /etc/sudoers
file for this. See https://serverfault.com/a/160587.
Dan Carley's rbenv-sudo looks like it will do what you want:
rbenv-sudo is a plugin for rbenv that allows you to run rbenv-provided Rubies and Gems from within a sudo session.
A more detailed explanation of how it works is provided in this article: Sudo Rbenv Me a Sandwich
To do this without using rbenv or rvm, do this:
sudo -E bundle exec ...
-E The -E (preserve environment) option will override the env_reset option in sudoers(5). It is only available when either the matching command has the SETENV tag or the setenv option is set in sudoers(5). sudo will return an error if the -E option is specified and the user does not have permission to preserve the environment.