35

I am running the command

bundle install

in a project folder. In some project folders it will produce an error and in other projects folders it will not produce an error. The error is:

Your user account isn't allowed to install to the system RubyGems

I know this can be fixed by following the recommended advice:

bundle install --path vendor/bundle

My question is why is the behavior inconsistent?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
William Entriken
  • 37,208
  • 23
  • 149
  • 195
  • 1
    Also I notice that Travis CI run exactly the command `bundle install --jobs=3 --retry=3` but it actually implicitly installs in `vendor/bundle` as per https://docs.travis-ci.com/user/languages/ruby/#Default-Test-Script – William Entriken Mar 16 '17 at 20:48
  • just removing Gemfile.lock solved my problem – user_pruser Apr 17 '20 at 14:23
  • [My answer](https://stackoverflow.com/a/65800841/6889226) to [this question](https://stackoverflow.com/q/51126403/6889226) is also applicable to OP's problem. – volmedo Jan 19 '21 at 23:25

9 Answers9

35

In my case, I solved doing just what the error message suggests:

Your user account isn't allowed to install to the system RubyGems.
  You can cancel this installation and run:

      bundle install --path vendor/bundle

  to install the gems into ./vendor/bundle/

So, instead of:

bundle install

I ran:

bundle install --path vendor/bundle

That was the solution for this guy.

The downside of that solution is that it creates a vendor folder inside the current folder, which can be added to .gitignore if it is to distribute the application through Git.

Antônio Medeiros
  • 3,068
  • 1
  • 27
  • 22
  • 4
    In the case of a [Jekyll](https://jekyllrb.com/) static website, also add `vendor` to the `exclude` setting in `_config.yml`. For more information, see: https://jekyllrb.com/docs/troubleshooting/#configuration-problems – Antônio Medeiros Apr 09 '18 at 19:18
  • Instead of adding `vendor` to `.gitignore`, you can add it to `.git/info/exclude`. The file `.git/info/exclude` is not tracked. – Diego Pino Nov 27 '22 at 02:01
17

I had a similar experience. I would have simply ran the code below to fix it temporarily

bundle install --path vendor/bundle

The downside to this is that it does not solve the issue permanently, as the issue will re-surface when you start out with other Ruby on Rails Applications.

I tried this solution, but it did not work for me:

Display a list of all your local gems for the bundler gem

gem list bundler

N/B: The command above is for rbenv version manager, the one for rvm might be different

This will display the versions of the bundler gem installed locally

bundler (2.0.2, default: 1.17.3, 1.10.6)

And then ran the code below below to uninstall version 1.10.6

gem uninstall bundler

Afterwhich I ran the code below to rehash rbenv

rbenv rehash

However, this did not solve the issue.

Here's how I solved it;

The issue was that I mistakenly ran a bundle install operation with administrative rights, that is:

sudo bundle install

which made the owner of the ~/.rbenv OR ~/.rvm directory to become root

To solve the issue I ran the code below to change the ownership of the files and directories.

For rbenv users:

sudo chown -R $USER  ~/.rbenv

And for RVM users

sudo chown -R $USER  ~/.rvm

That's it.

I hope this helps

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
  • Didn't worked but this helped me remember that I have `rvm`. So I tried using the most recent `rvm` version then boom. It worked! – Adrian Enriquez Feb 18 '23 at 15:48
15

Usually if you're using RVM, rbenv or chruby to install Ruby, all the gems will be installed in your home folder under ~/.rbenv/ruby-version/...

If you're using your system Ruby though (the one that is installed by default) the gems are installed alongside it in a location that you don't have access to without sudo.

My guess would be that your version manager defaults to the system Ruby but some of your projects have a .ruby-version file in them that tells it to use a different version of Ruby which you have access to.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
isaacsloan
  • 885
  • 6
  • 18
7

Good rbenv fix

  • Avoids sudo
  • Avoids install to vendor/bundle

The problem, at least for me, was that bundler itself wasn't installed in my rbenv ruby version. Even though bundler existed and seemed usable... except the permissions error.

One of the things that clued me in was that at the actual command line itself I could install gems ok and not get the error message. I did this for a while as a work-around until I decided to fix the problem permanently as shown below:

To fix it I did:

rbenv local 2.5.0 # Make sure I'm using a local version that exists
gem list | grep bundler # Note no output! Need to fix that!
gem install bundler
rbenv rehash
bundle (within my project that has a Gem file)
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
3

Using RVM:

You have to install rvm for your single user (standard user / non root user), in your $HOME directory (It's not installed if you don't have /home/youruser/.rvm directory.

As is specified on rvm site under "Single-User Install Location: ~/.rvm/" section, to install rvm for single user run:

cd
\curl -sSL https://get.rvm.io | bash -s -- --ignore-dotfiles

Now, rvm should be installed. As in further prompt, refresh rvm (with your username provided):

source /home/---YOURUSERNAME---/.rvm/scripts/rvm

To prevent prompts for installing sudo-need packages, run:

rvm autolibs disable

You can install Ruby for your user only, in version 2.5 (or any other, listed in rvm list known)

rvm install 2.5

Explanation:

You probably try to install rvm using sudo and maybe with this package for ubuntu but you don't have sudo permissions.

This prompt is telling you that you can't bundle gems globally for the whole system, which may be good for your private home machine but not for your corporate user(machine), which is often administered by someone else.

If rvm will be installed in your $HOME gems will be bundled there, like usual.

Behavior may be inconsistent because other users have rvm installed in their $HOME directory

ToTenMilan
  • 582
  • 1
  • 9
  • 19
  • 1
    On Ubuntu 18.04 from Windows 10 store, you may need to `sudo apt update` & `sudo apt upgrade` & `sudo apt install gnupg2` before all other commands – ToTenMilan Jul 28 '18 at 13:50
3
  • Open your bashrc or zshrc file.
  • Add eval "$(rbenv init -)"at the end of the file and save it.
  • Run source ~/.zshrc or source ~/.bashrc

You can also close and open the terminal. bundle install and you are good to go.

2

In my case, I had an existing $BUNDLE_PATH without enough permissions to the bundler user to write in.

Your user account isn't allowed to install to the system RubyGems
bundle install --path vendor/bundle
pocheptsov
  • 1,936
  • 20
  • 24
1

Recently faced the same issue. I use rvm

sudo chown -R $USER ~/.rvm helped for me

Oleh Sobchuk
  • 3,612
  • 2
  • 25
  • 41
0

If you are using rbenv, all you need to do is provide a .ruby-version file.