How to find unused gems in my Gemfile, so that I can cleanup references which are no longer used.
6 Answers
Use linux' file access time to see what's actually being used.
This requires:
- project's gems isolated in an rvm gemset
gems installed on a partition mounted with the atime (strictatime on ubuntu 12.04) option:
sudo mount -o remount,strictatime /
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.

- 2,830
- 2
- 30
- 32
-
1This is a very clever solution -- probably the most accurate of the answers. – Tom Harrison Apr 15 '15 at 20:57
-
Really appreciate that. It was a pretty long dive down the rabbit hole for me. – fakeleft Apr 16 '15 at 10:24
-
1This is certainly the best solution to make a cursory assessment of unused gems – OneChillDude Jul 06 '15 at 17:42
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.

- 977
- 6
- 22
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.

- 44,526
- 36
- 160
- 222
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:
- Add
gem 'gem_bench', :group => :console
to your Gemfile. - Run
bundle install
- Run
bundle exec rails console
with the following command: a = GemBench.check({verbose: true})

- 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
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.

- 12,923
- 3
- 46
- 74
-
3Sad but true. I do wish there were an automated process though - it would work with teams and projects where there are frequent changes! – Gautam Rege Sep 02 '13 at 08:19
-
1
There is the bundle clean --force
command to remove gems outside the Gemfile.lock
definitions.
See bundle-clean.

- 2,973
- 28
- 27

- 15,718
- 8
- 61
- 72
-
2As of bundle 1.3.5, -f option is no longer available, use --force option instead. – Hoa Feb 05 '14 at 01:01
-
-
11
-
2@iconoclast is correct. This just removes gems in your bundle's cache that aren't being used in your `Gemfile.lock`. This has nothing to do with whether or not Gems are being used in your application. – Joshua Pinter Mar 23 '21 at 17:07