1

I am using Amazon EC2 instance and when I run bundle install as ec2-user, it works fine but when I try the same as root user, it gives error.

bash: bundle: command not found

There is bundler gem exist when I run gem list for both users. I have installed ruby-devel rubygems ruby-libs as root user. Can anyone please help me understand this issue? I have searched and read for hours.

echo $PATH of both users are as below.

root: /sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin

ec2-user: /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin

Sohail Ahmed
  • 1,047
  • 1
  • 11
  • 27
  • possible duplicate of [Using $ sudo bundle exec ... raises 'bundle: command not found' error](http://stackoverflow.com/questions/14647745/using-sudo-bundle-exec-raises-bundle-command-not-found-error) – Pak Jul 26 '14 at 09:33

2 Answers2

6

I added /usr/local/bin and /usr/local/sbin to secure_path in /etc/sudoers

sudo visudo

Then change this line:

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

Changed this to

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin

Explanation:

Gems were installed at /usr/local path and without adding this in secure_path, root user cannot run scripts from there. You can check your gems path by running gem env in shell.

I also had another issue. I installed gems as ec2-user using bundle and default rubygems path in Amazon Linux installed them only for ec2-user, something I did not like so I installed ruby and rubygems from source which has path to installed them at central location under /usr/local. I know it is not recommended by many but I like it this way. By the way, I tried to find a way to change rubygems path but I could not.

Sohail Ahmed
  • 1,047
  • 1
  • 11
  • 27
0

Try sudo -i -u $USER bundle install

where $USER is the correct username so : sudo -i -u root bundle install

If this does not work, consider giving a try to the rbenv-sudo plugin.

Pak
  • 2,123
  • 22
  • 28