Bundler and Disk Space

19 Mar 2015

Earlier this week I noticed that my MacBook Pro’s 256GB hard disk was 95% full, which prompted me to start investigating what I could remove in order to free up space.

ncdu

Having remembered that on a previous occasion a work colleague had suggested using ncdu, I started this very helpful tool and task-switched whilst waiting for it to finish.

When I delved into what ncdu had to show me about large files, I eventually made an interesting discovery.

After regaining some space by deleting some Apple Mail log files, I was surprised to find many git versions of some internal RubyGems that we use at work. Some of these are large. All of them are referred to in Gemfiles by their GitHub repositories, or in other words they are git referenced gems defined in the Gemfile like this:

gem 'blake-data-source', git: 'git@github.com:blake-education/blake-data-source.git', branch: 'develop'  

The result was this:

That’s right. The gems for Ruby 2.1.2 were taking up 10.5GB of disk space! Looking closely, I could see that there were many old versions of large gems.

At this point it is worth pointing out that I was using rbenv and Bundler to manage my gems in an unsophisticated manner. This had unintended consequences as I will explain further.

Cleaning up old versions of gems

gem cleanup

One way of cleaning up old versions of gems is to use the gem cleanup command. However, whilst this will remove old versions of gems that have been tagged with version numbers, it won’t include git referenced gems in it’s sweep.

bundle clean

Another command that can help clean up old gem versions is bundle clean —force. This is definitely a helpful command but one to be used with caution.

In my case I had to be aware that, since this command would clean up gem versions unused in my directory, it would also clean up gems used by other projects using the same Ruby version.

bundle —path

Advice from my colleague Josh Kunzmann quickly allowed me to see the solution. That was to run bundle clean —force and then change to using the bundle install —path command, or bundle —path for short. As the documentation says, the —path option allows you to:

Specify a different path than the system default ($BUNDLE_PATH or $GEM_HOME). Bundler will remember this value for future installs on this machine

And, since I am in the habit of using bash aliases and tmuxinator, I am now using aliases like this to start project coding sessions:

alias mkp=‘bundle —path ~/.appgems/kpdotcom —clean && mux kp’  

In this way I should be able to keep the disk space used by RubyGems under control. It also ensures that dependent gem versions are installed before I fire up tmuxinator.

Conclusion

The moral of the story is twofold:

  1. ncdu is a very useful tool for discovering large files on your hard disk
  2. bundle —path your/chosen/path —clean is a good way to ensure that old versions of git referenced gems do not accumulate

Other posts

Previous post: The Merits of Explanation

More recently: Rails and RSpec with Two Databases

© 2024 Keith Pitty, all rights reserved.