Published 2024-07-26.
Time to read: 1 minutes.
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:
$ 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:
$ 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.
$ 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:
$ 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:
$ 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:
$ bundle graph /my/current/directory/gem_graph.png
The plugin creates a PNG image called gem_graph.png
.
To view it in WSL, type:
$ explorer.exe gem_graph.png