Mike Slinn

Visualizing Dependencies with bundle graph

Published 2024-07-26.
Time to read: 1 minutes.

This page is part of the ruby collection.

You can visualize the dependencies between Ruby gems by using the bundle graph subcommand. This subcommand generates a PNG file of the dependencies of current Ruby project as a dependency graph.

The bundle graph subcommand is not installed by default. Instead, it is implemented as a Bundler plugin. The git project is here.

For the curious, see the list of known Bundler plugins .

If you attempt to run bundle graph without installing the plugin, you will get the following error:

Shell
$ bundle graph
Could not find command "graph".

Installation

Bundler plugins can either be installed globally or for specific projects. I think the plugin is best installed globally, not per-project. Follow these instructions for a global installation.

The documented installation instructions are incomplete. This article provides more information. Here is the help message:

Shell
$ bundler plugin help install
Usage:
  bundler plugin install PLUGINS
Options: [--source=URL of the RubyGems source to fetch the plugin from] [--version=The version of the plugin to fetch] [--git=URL of the git repo to fetch from] [--local-git=Path of the local git repo to fetch from (deprecated)] [--branch=The git branch to checkout] [--ref=The git revision to check out] [--path=Path of a local gem to directly use]
Description: Install plugins either from the rubygems source provided (with --source option), from a git source provided with --git, or a local path provided with --path. If no sources are provided, it uses Gem.sources

Installing the graph plugin for Bundler requires three steps.

Globally install the ruby-graphviz gem.

Shell
$ gem install ruby-graphviz
Fetching ruby-graphviz-1.2.5.gem
You need to install GraphViz (https://graphviz.org) to use this Gem.
For more information about Ruby-Graphviz : * Doc: https://rdoc.info/github/glejeune/Ruby-Graphviz * Sources: https://github.com/glejeune/Ruby-Graphviz * Issues: https://github.com/glejeune/Ruby-Graphviz/issues
Successfully installed ruby-graphviz-1.2.5 Parsing documentation for ruby-graphviz-1.2.5 Installing ri documentation for ruby-graphviz-1.2.5 Done installing documentation for ruby-graphviz after 5 seconds 1 gem installed

Install the graphviz dependency. On WSL/Ubuntu I installed it like this:

Shell
$ sudo apt install graphviz

To install the Bundler plugin globally, first move to your home directory, then type the following. If you instead type this incantation from a Ruby project, the plugin will only be installed for that project:

Shell
$ cd
$ bundler plugin install bundler-graph Fetching gem metadata from https://rubygems.org/. Resolving dependencies... Fetching bundler-graph 0.2.1 Installing bundler-graph 0.2.1 Installed plugin bundler-graph

Running Bundle Graph

With the bundler-graph plugin installed, it can be invoked from the command line as follows:

Shell
$ bundle graph
/my/current/directory/gem_graph.png 

The plugin creates a PNG image called gem_graph.png. To view it in WSL, type:

Shell
$ explorer.exe gem_graph.png
* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.