Search code examples
rubymacosrubygemshomebrewrbenv

Cannot switch gem versions in an rbenv environment


I have rbenv 1.1.2 installed via Homebrew on my macOS 10.14.4.

I am having trouble running gems at a version different from the latest available.

For example, I have three versions of the xcodeproj gem installed (1.8.2, 1.8.1, 1.8.0, 1.5.7).

If I run xcodeproj --version, I see 1.8.2, as expected.

However, if I run xcodeproj _1.5.7_ --version, I also see 1.8.2, while expecting 1.5.7.

Note that passing the version does something - if I pass an invalid version (e.g. 1.5.0), I do get the "can't find gem xcodeproj (= 1.5.0)" error as expected.

What can I do to be able to run previous versions?


Solution

  • you should use bundler to mange the rubygems dependencies specified in a Gemfile. for instance:

    $ cat Gemfile
    # frozen_string_literal: true
    
    source "https://rubygems.org"
    
    gem "xcodeproj", "1.5.7"
    

    install them using bundler

    $ bundle install --path gems
    Fetching gem metadata from https://rubygems.org/...............
    Resolving dependencies...
    Fetching CFPropertyList 3.0.0
    Installing CFPropertyList 3.0.0
    Fetching atomos 0.1.3
    Installing atomos 0.1.3
    Using bundler 1.17.2
    Fetching claide 1.0.2
    Installing claide 1.0.2
    Fetching colored2 3.1.2
    Installing colored2 3.1.2
    Fetching nanaimo 0.2.6
    Installing nanaimo 0.2.6
    Fetching xcodeproj 1.5.7
    Installing xcodeproj 1.5.7
    Bundle complete! 1 Gemfile dependency, 7 gems now installed.
    Bundled gems are installed into `./gems`
    

    then execute your rubygem using bundler

    $ bundle exec xcodeproj --version
    1.5.7
    

    note that when you are using rbenv and would like to execute a rubygem without bundler management, then you will have to make sure that for each rbenv version, you install your favorite gems. for instance, if you are using 2 different ruby versions and you like pry rubygem, then you will have to install pry for each ruby version.