Search code examples
rubyrubygemsbundler

How Does Bundler Determine to Re-Install Something?


I'm using a CI tool (CircleCI to be specific) to cache some Ruby gems so I don't have to re-install them on every build. Currently on the first build I'm doing this:

gem install bundler
bundle install

Which installs a number of gems:

Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using public_suffix 3.0.2
Using addressable 2.5.2
Using bundler 1.16.1
Using mini_mime 1.0.0
Using mini_portile2 2.3.0
Fetching nokogiri 1.8.2
Installing nokogiri 1.8.2 with native extensions

Once that initial build is done I'm caching the directory which bundler shows where the gems are: /opt/circleci/.rvm/gems/ruby-2.3.1/gems/ and its contents.

The next time it runs I can do cd /opt/circleci/.rvm/gems/ruby-2.3.1/gems/ and reveal all of the gems from the cache that were restored. However if I navigate to my Gemfile and run bundle info nokogiri or any of the other gems in that folder it reveals an error: Could not find public_suffix-3.0.2 in any of the sources

If I run bundle install or gem install gemnamehere the gems install again which is not ideal because the goal is to save time on the build. Do I need to run some sort of command with bundle or Ruby to let it know that these gems are installed so it doesn't do it again?

Apologies if this is a silly question, I'm learning this as I go along.

Edit: CircleCI config as requested:

  test:
    machine: true
    steps:  
      - run:
          # Installs PhantomJS
          name: Install phantomjs
          command: |
            if ! [ $(which phantomjs) ]; then                
              curl --output /usr/local/bin/phantomjs https://s3.amazonaws.com/circle-downloads/phantomjs-2.1.1
            fi
      - checkout
      - restore_cache:
          keys:
            - v1-gem-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
      - run:
          name: "Install Dependencies"
          command: |
            cd ~/project
            gem install bundler
            bundle install
      - save_cache:
          key: v1-gem-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
          paths:
            - /opt/circleci/.rvm/gems/ruby-2.3.1/gems/
      - run:
          name: "Run  Snapshots"
          command: |
            cd ~/project
            yarn build
            cd ~/project/mocks/visual
            bundle exec ruby snapshots.rb

Solution

  • Solution was to use bundle install --path vendor/cache and cache the vendor/cache path instead.