No release in over 3 years
A Bundler plugin that keeps your Gemfile organized by alphabetizing gem declarations within each group automatically.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 3.0

Runtime

>= 2.2
 Project Readme

bundle_alphabetically

bundle_alphabetically is a Bundler plugin that keeps your Gemfile organized by alphabetizing gem declarations within each group.

  • Automatic mode: after bundle add / bundle install, it rewrites your Gemfile so gems are sorted alphabetically inside each group.
  • Manual mode: run bundle sort_gemfile whenever you want to clean things up (with an optional --check for CI).

Installation

Install the plugin from RubyGems:

bundle plugin install bundle_alphabetically

Or add it to your Gemfile and install:

plugin "bundle_alphabetically"
bundle install

For development, you can install from a local checkout:

cd /path/to/bundle_alphabetically
bundle plugin install bundle_alphabetically --path .

Once installed, Bundler will load plugins.rb and register the command and hooks.

What it does

  • Sorts gem entries alphabetically by name within each context:
    • top-level (no group)
    • each group :name do ... end block, separately
  • Preserves:
    • non-gem lines like source, ruby, plugin, path, etc.
    • group block structure and indentation
    • comments and most surrounding blank lines
    • multi-line gem declarations (e.g. options hashes split across lines)

It operates on the current Bundler.default_gemfile and rewrites it in place.

Automatic sorting (hook)

The plugin registers an after-install-all hook:

  • After a successful install (including bundle add), Bundler calls into bundle_alphabetically.
  • The plugin sorts the Gemfile and prints a short message like:
    • Gemfile gems alphabetized by bundle_alphabetically

If the Gemfile is already sorted, it does nothing.

If it encounters an error parsing the Gemfile, it raises a Bundler::BundlerError and reports a message via Bundler.ui.error, but leaves the file unchanged.

Manual command

The plugin adds a bundle sort_gemfile command via Bundler::Plugin::API:

bundle sort_gemfile
  • Sorts the current Gemfile in place using the same rules as the hook.
  • Prints Gemfile already sorted if no changes were needed.

For CI, you can use --check (or -c) to verify sorting without modifying the file:

bundle sort_gemfile --check

In --check mode:

  • Exit successfully if the Gemfile is already sorted.
  • Raise Bundler::BundlerError (non-zero exit) if changes would be required.

Limitations

  • Designed for conventional Gemfiles:
    • top-level gem calls and group ... do blocks
    • straightforward multi-line gem entries
  • It does not attempt to fully evaluate arbitrary Ruby or heavy metaprogramming inside the Gemfile.

If you have an unusually dynamic Gemfile and hit issues, you can temporarily uninstall the plugin with:

bundler plugin uninstall bundle_alphabetically