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.
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: 'firstname.lastname@example.org: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.
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.
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.
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.
The moral of the story is twofold:
bundle --path your/chosen/path --cleanis a good way to ensure that old versions of git referenced gems do not accumulate