Search code examples
rubyrubygemsbundlerrootsudo

How to fix "sudo bundle install"


I read a lot of questions like "How can I fix an accidental 'sudo bundle install dir_name'?", "cannot recover from `sudo bundle install`", "`bundle install` failed due to permission denied" but none worked for me.

Under Debian 10.2, I ran bundle install as root by mistake. I used Ctrl+c than 1 second after I issue it. Now, as normal user, every time I try to use bundle I get:

bash: /srv/dev-disk-by-label-data/home/mark/gems/bin/bundle: Permission denied

I did:

  • gem uninstall bundler and gem install bundler
  • rm -r ~/.bundle/
  • rm -r ~/.gem/
  • bundle install --path ~/.gem and its variants
  • checked all the files under ~/gems are owned by myself

The permission denied error is still there.


Solution

  • From Bundler's documentation:

    By default, Bundler installs gems to the same location as gem install.

    In some cases, that location may not be writable by your Unix user. In that case, >Bundler will stage everything in a temporary directory, then ask you for your >sudo password in order to copy the gems into their system location.

    From your perspective, this is identical to installing the gems directly into the >system.

    You should never use sudo bundle install. This is because several other steps in >bundle install must be performed as the current user:

    Updating your Gemfile.lock

    Updating your vendor/cache, if necessary

    Checking out private git repositories using your user's SSH keys

    Of these three, the first two could theoretically be performed by chowning the >resulting files to $SUDO_USER. The third, however, can only be performed by >invoking the git command as the current user. Therefore, git gems are downloaded >and installed into ~/.bundle rather than $GEM_HOME or $BUNDLE_PATH.

    As a result, you should run bundle install as the current user, and Bundler will >ask for your password if it is needed to put the gems into their final location.

    As a possible solution you can fully uninstall Ruby and its dependencies, and then install Ruby using asdf, RVM or rbenv and run Bundler again.

    I use Bundler just to install gems from a gemfile. cd into the project with the gemfile and run bundle install. It must find the gemfile and install the gems automatically. I think it's bad to use Bundler inside the home directory.

    I have all gems installed here:

    ~/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems