Skip to content

Instantly share code, notes, and snippets.

@rstacruz
Last active May 14, 2020 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rstacruz/8cdb10116fa085177fbbd556c1fc8c2e to your computer and use it in GitHub Desktop.
Save rstacruz/8cdb10116fa085177fbbd556c1fc8c2e to your computer and use it in GitHub Desktop.

Updating gems with Bundler

Bundler recommends using bundle update to update Rails. It also accepts --conservative to try not to update shared dependencies.

bundle update [--conservative] rails

This method can often produce unexpected results. Even with the conservative flag, it may try to update gems that you may not need updated. In some cases, bundler might even give some errors.

Fetching gem metadata from https://rubygems.org/..
Resolving dependencies.......
Bundler could not find compatible versions for gem "activerecord":
  In snapshot (Gemfile.lock):
    activerecord (= 4.2.11.1)

  In Gemfile:
    ar-multidb (= 0.1.13) was resolved to 0.1.13, which depends on
      activerecord (>= 3.0)

    kaminari (>= 1.1.1) was resolved to 1.1.1, which depends on
      kaminari-activerecord (= 1.1.1) was resolved to 1.1.1, which depends on
        activerecord

    pg_search was resolved to 1.0.6, which depends on
      activerecord (>= 3.1)

    rails (= 5.0.7.2) was resolved to 5.0.7.2, which depends on
      activerecord (= 5.0.7.2)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

Improved method

Rather than getting Bundler to upgrade a gem, I found that telling Bundler to install gems as new gems instead can be more resiable.

To do this, Bundler has to think that the gem isn't installed. Try removing the gem you want to update from the Gemfile.lock itself. Remove them from the level right below GEMspecs: section. It's best to remove it from the DEPENDENCIES section as well.

@@ Gemfile.lock @@
 GEM
   remote: https://rubygems.org/
   specs:
     actionmailer (4.2.11.1)
       actionpack (= 4.2.11.1)
       actionview (= 4.2.11.1)
-    activerecord (4.2.11.1)
-      activemodel (= 4.2.11.1)
-      activesupport (= 4.2.11.1)
-      arel (~> 6.0)
     activerecord-deprecated_finders (1.0.4)
     activesupport (4.2.11.1)
 ...

 DEPENDENCIES
   ...
-  activerecord (~> 4.2)

Run Bundler again after manually editing Gemfile.lock. THe manual edits to the lock file should tell Bundler that those gems aren't installed yet, so Bundler will try to treat it like a newly-added gem.

bundle

Updating Rails

When upgrading Rails, it might be helpful to remove all of Rails's sub-gems as part of the steps above. For example, when upgrading from Rails 4.2.11.1, it'd be helpful to remove all gems with that version:

 GEM
   remote: https://rubygems.org/
   specs:
-    actionmailer (4.2.11.1)
-      actionpack (= 4.2.11.1)
-      actionview (= 4.2.11.1)
-      ...
-    actionpack (4.2.11.1)
-      actionview (= 4.2.11.1)
-      activesupport (= 4.2.11.1)
-      ...
-    actionview (4.2.11.1)
-      activesupport (= 4.2.11.1)
-      builder (~> 3.1)
     active_link_to (1.0.5)
       actionpack
       addressable
-    activejob (4.2.11.1)
-      activesupport (= 4.2.11.1)
-      ...
-    activemodel (4.2.11.1)
-      activesupport (= 4.2.11.1)
-      ...
-    activerecord (4.2.11.1)
-      activemodel (= 4.2.11.1)
-      activesupport (= 4.2.11.1)
-      ...
-    activesupport (4.2.11.1)
-      i18n (~> 0.7)
-      minitest (~> 5.1)
-      ...
     activesupport-cascadestore (0.0.2)
       activesupport
-    rails (4.2.11.1)
-      actionmailer (= 4.2.11.1)
-      actionpack (= 4.2.11.1)
-      ...
-    railties (4.2.11.1)
-      actionpack (= 4.2.11.1)
-      activesupport (= 4.2.11.1)
-      ...
     rake (12.3.3)
     redis (3.3.3)
     ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment