42

How to find unused gems in my Gemfile, so that I can cleanup references which are no longer used.

Joe
  • 14,513
  • 28
  • 82
  • 144

6 Answers6

49

Use linux' file access time to see what's actually being used.

This requires:

  1. project's gems isolated in an rvm gemset
  2. gems installed on a partition mounted with the atime (strictatime on ubuntu 12.04) option:

    sudo mount -o remount,strictatime /

  3. complete test coverage (i.e. we'll be relying on test runs to update file access times)

Note the time and run your tests. Then from your gemdir, do:

ls --time-style long-iso -ltud1 $PWD/*/lib/** | grep "21:44" | sed s/.*gems.// | sed s/.lib.*// | sort -u

Change the 21:44 to whatever time you ran the tests at.

fakeleft
  • 2,830
  • 2
  • 30
  • 32
18

Run your tests and then:

gem stale

Which does the following:

The stale command lists the latest access time for all the files in your installed gems.

You can use this command to discover gems and gem versions you are no longer using.

L.R.
  • 977
  • 6
  • 22
15

Any gem should be considered for removal if all tests pass in its absence.

Assuming you have good test coverage - particularly high-level functional tests - you could write a script to selectively remove one gem at a time. ie run all your tests N times, where N is the number of gems in your Gemfile and each test has one missing gem. That will help weed out gems not pulling their weight.

mahemoff
  • 44,526
  • 36
  • 160
  • 222
13

You can use the gem_bench gem to analyze your Gemfile and identify which gems do not need to be required at boot time. From there it just requires a bit of analysis to determine which gems can be removed completely.

To generate a list of gems that can be removed from boot time:

  1. Add gem 'gem_bench', :group => :console to your Gemfile.
  2. Run bundle install
  3. Run bundle exec rails console with the following command:
  4. a = GemBench.check({verbose: true})
Mike
  • 1,852
  • 1
  • 15
  • 19
  • Followed these steps exactly, then got `uninitialized constant GemBench`. Seems there should be another step between 3 and 4 which reads `require 'gem_bench'` – Mitya Dec 08 '22 at 13:59
9

I doubt if there is an automated way to find unused gems in the Gemfile.

For someone who has built the application over time, it should be easy to manually identify gems that were discarded along the way for some reason or the other.

For a legacy application inherited from someone else, it is a much difficult task to manually identify unused gems. If there is comprehensive test coverage for the system, it would help in removing unused gems by trial and error, while ensuring that the tests pass at each change in the Gemfile.

Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74
7

There is the bundle clean --force command to remove gems outside the Gemfile.lock definitions.

See bundle-clean.

facundofarias
  • 2,973
  • 28
  • 27
iltempo
  • 15,718
  • 8
  • 61
  • 72