14

I'm looking for simple, but good way to cleanup gemfile and make rails startup faster. How can I get a list of all required gems vs all loaded gems.

4 Answers 4

27

bundle clean --force will remove old gems (or older versions of currently-used gems) previously installed, but not currently being used in your current Gemfile.lock manifest.

Edit: As correctly mentioned in comments, this won't 'clean' or alter the Gemfile text contents itself.

2
  • 7
    This doesn't help clean up the Gemfile.
    – iconoclast
    Commented Apr 4, 2018 at 19:48
  • Yep. It doesn't clean up the Gemfile, but it surely remove gems that are not used in Gemfile, so it works for me.
    – jiggysoo
    Commented Dec 23, 2023 at 7:50
6

First, if you want to check what are the gems used by your project, I invite you to run gem server in your project folder root, then go to http://0.0.0.0:8808/

You will be able to understand the dependencies of all the gems your project is using. It will also show you all versions of the same gem.

To remove old versions of gems you can run as @changingrainbows mention bundle clean --force

After this step run your gem server again and watch the result, a clean and understandable gem list with all dependencies.

0
3

It depends what you're after here.

If you're looking to remove old, unused gem versions, then bundle clean.

If you've been adding gems as you develop and have lost track of the ones you actually use, and have good test coverage, then try this answer.

If you want to reduce the number of gems rails pulls in at startup to the bare minimum, try gem_bench.

-2

I think it is impossible. When your APP starts it loads gems from Gemfile.lock but it does not know if they (gems) are needed in your code or not. The APP inform you by raising an exception When something calls a class or method that is undefined if some needed gem is missed (if you remove it from Gemfile), but this can happen at any moment (not during starting your APP).

So if you are looking the way to clean up your gem list I think the best way to do it manually (I know it is not easy way). Analyse each gem to find out what functionality it provides and decide (or find in your code) if it is needed or not. Additionally tests (if you have them) should help you a lot.

1
  • 18
    "I think it is impossible" means "I don't know", in which case you should not answer.
    – iconoclast
    Commented Apr 4, 2018 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.