Search code examples
rubybundlerrbenv

Gem list doesn't show bundler after "Successfully installed bundler"


I'm trying to install a second version of bundler. The installation outputs success message, but the new bundler installation appears not to exist at all. How can I correct this installation?

# Use sudo because of *system* rbenv installation
sudo gem install bundler:2.1.4
# => Successfully installed bundler-2.1.4

# Check for existence of new installation, but only the old version is available
gem list bundler
# => bundler (default: 1.17.2)
bundle _2.1.4_ -v
# => can't find gem bundler (= 2.1.4) with executable bundle (Gem::GemNotFoundException)
ls /usr/local/rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/
# => [shows bundler-1.17.2 but not any other bundler directories]

I am using a system installation of rbenv (not rvm). I'm not working within a bundle (nor within a gemset since I'm not using rvm).


Solution

  • The PATH of the root user is likely not the same as the PATH of your current user. Therefore the gem command that root loads will not be the same as the gem command you load as a normal user. This makes sudo gem install save the gems into a different location (the location of the Ruby installation found in the PATH of the root user).

    To fix this issue, the most straightforward solution is to force root to use the same gem command by giving it the full path:

    sudo `which gem` install ...
    

    Note the use of backticks. Using backticks this way, the command will essentially expand to something like:

    sudo /some/path/to/the/user/ruby/installation/bin/gem install ...
    

    To figure out if the gem command of root is different from your default gem command, you can do this:

    # As normal user check output of this command
    which gem
    
    # ..and compare it to the output of this command
    sudo which gem