Default gems and bundled gems

suggest change

The Ruby standard library is no longer one big block of code that ships with the interpreter. Over the Ruby 3.x releases most of it was split into gems, and those gems come in two kinds:

Ruby keeps moving libraries from the first group to the second. This is why an app that ran fine on Ruby 3.3 can fail on Ruby 3.4 or 4.0 with cannot load such file -- csv or -- logger, even though nobody touched its code.

All output below comes from Ruby 4.0.7 (RubyInstaller, x64-mingw-ucrt) with RubyGems and Bundler 4.0.20.

Telling the two kinds apart

gem list labels default gems with default::

$ gem list json
json (default: 2.18.0)

$ gem list csv
csv (3.3.5)

gem list -d (--details) shows the same thing as Installed at (default):

$ gem list "^(json|csv)$" -d
csv (3.3.5)
    Authors: James Edward Gray II, Kouhei Sutou
    Homepage: https://github.com/ruby/csv
    Licenses: Ruby, BSD-2-Clause
    Installed at: C:/Ruby40-x64/lib/ruby/gems/4.0.0

    CSV Reading and Writing

json (2.18.0)
    Author: Florian Frank
    Homepage: https://github.com/ruby/json
    License: Ruby
    Installed at (default): C:/Ruby40-x64/lib/ruby/gems/4.0.0

    JSON Implementation for Ruby

From Ruby code, use Gem::Specification#default_gem?:

%w[json csv tsort logger rake].each do |name|
  spec = Gem::Specification.find_by_name(name)
  kind = spec.default_gem? ? "default" : "bundled"
  puts format("%-7s %-8s %s", name, spec.version, kind)
end
$ ruby kinds.rb
json    2.18.0   default
csv     3.3.5    bundled
tsort   0.2.0    default
logger  1.7.0    bundled
rake    13.3.1   bundled

The two kinds are also stored differently. A default gem's code sits in Ruby's own library directory (rubylibdir), and its gemspec is in specifications/default. A bundled gem is unpacked into gems/, just like a gem you installed yourself:

require "json"
require "csv"

%w[json csv].each do |name|
  spec = Gem.loaded_specs[name]
  puts "#{name} #{spec.version} default_gem?=#{spec.default_gem?}"
  puts "  spec: #{spec.loaded_from}"
  puts "  code: #{$LOADED_FEATURES.find { |f| f.end_with?("/#{name}.rb") }}"
end
$ ruby where.rb
json 2.18.0 default_gem?=true
  spec: C:/Ruby40-x64/lib/ruby/gems/4.0.0/specifications/default/json-2.18.0.gemspec
  code: C:/Ruby40-x64/lib/ruby/4.0.0/json.rb
csv 3.3.5 default_gem?=false
  spec: C:/Ruby40-x64/lib/ruby/gems/4.0.0/specifications/csv-3.3.5.gemspec
  code: C:/Ruby40-x64/lib/ruby/gems/4.0.0/gems/csv-3.3.5/lib/csv.rb

To count both kinds in a fresh install, filter the gems in Ruby's own gem directory:

specs = Gem::Specification.select { |s| s.base_dir == Gem.default_dir }
default, bundled = specs.partition(&:default_gem?)
puts "default gems: #{default.size}"
puts "bundled gems: #{bundled.size}"
puts bundled.map(&:name).sort.join(" ")
$ ruby count.rb
default gems: 47
bundled gems: 38
abbrev base64 benchmark bigdecimal csv debug drb fiddle getoptlong irb logger matrix minitest mutex_m net-ftp net-imap net-pop net-smtp nkf observer ostruct power_assert prime pstore racc rake rbs rdoc readline reline repl_type_completor resolv-replace rexml rinda rss test-unit typeprof win32ole

If you have run gem install without a custom GEM_HOME, your own gems land in the same directory and show up as "bundled" here too.

Default gems can be updated but not removed

A default gem is still a gem. You can install a newer version, and require will pick the newest one:

$ gem install json
Successfully installed json-3.1.0
1 gem installed

$ gem list "^json$"
json (3.1.0, default: 2.18.0)

$ ruby -e 'require "json"; puts JSON::VERSION'
3.1.0

$ ruby -e 'gem "json", "2.18.0"; require "json"; puts JSON::VERSION'
2.18.0

The version that ships with Ruby stays, though. gem uninstall refuses to remove a default gem, so require "json" always works somehow:

$ gem uninstall json -v 2.18.0
Gem json-2.18.0 cannot be uninstalled because it is a default gem

A bundled gem has no such protection. It's an ordinary installed gem, so gem uninstall csv really removes it. Ruby only finds bundled gems through the normal gem path. If you point GEM_HOME and GEM_PATH at an empty directory, the bundled gems disappear, while the default gems still load:

$ GEM_HOME=$PWD/empty GEM_PATH=$PWD/empty ruby miss.rb
C:/Ruby40-x64/lib/ruby/4.0.0/did_you_mean/core_ext/name_error.rb:11: warning: csv is not part of the default gems since Ruby 3.4.0. Install csv from RubyGems.
miss.rb:1:in 'Kernel#require': cannot load such file -- csv (LoadError)
	from miss.rb:1:in '<main>'

$ GEM_HOME=$PWD/empty GEM_PATH=$PWD/empty ruby -e 'require "json"; require "tsort"; puts "default gems still load"'
default gems still load

(miss.rb is just require "csv".) The same thing happens in Docker images and CI setups that use their own gem directory.

The promotions from 3.0 to 4.0

Moving a library from default gem to bundled gem means it can be updated or dropped on its own schedule, without a new Ruby release. Ruby has done this in waves:

Ruby Became bundled gems
3.0 rexml, rss (and rbs, typeprof were added as bundled gems)
3.1 net-ftp, net-imap, net-pop, net-smtp, matrix, prime (and debug was added)
3.3 racc
3.4 abbrev, base64, bigdecimal, csv, drb, getoptlong, mutex_m, nkf, observer, resolv-replace, rinda, syslog
4.0 benchmark, fiddle, irb, logger, ostruct, pstore, rdoc, readline, reline, win32ole
4.1 (planned) tsort

Ruby 3.3 printed warnings for the 3.4 gems, and 3.4 did the same for the 4.0 gems. Ruby 4.0 warns about tsort. syslog isn't installed at all on Windows.

The 4.0 wave is the one most people notice, because it includes logger (used by Rails and many other gems), ostruct, benchmark, and even irb.

Gem::BUNDLED_GEMS

The list behind those warnings is in Gem::BUNDLED_GEMS. RubyGems loads it from bundled_gems.rb at startup. It's an internal module (marked :nodoc:), so it can change between releases, but it's handy for checking. SINCE maps each gem to the Ruby version where it stopped being a default gem:

require "bundled_gems"

Gem::BUNDLED_GEMS::SINCE.group_by { |_, version| version }.each do |version, gems|
  puts "#{version}: #{gems.map(&:first).join(", ")}"
end
$ ruby since.rb
3.3.0: racc
3.4.0: abbrev, base64, bigdecimal, csv, drb, getoptlong, mutex_m, nkf, observer, resolv-replace, rinda, syslog
4.0.0: ostruct, pstore, rdoc, win32ole, fiddle, logger, benchmark, irb, reline
4.1.0: tsort

The list only goes back to 3.3, so rexml and net-smtp aren't in it. There is also EXACT, which maps a feature to a gem with a different name. Right now it only has "kconv" => "nkf".

Here's a small script that scans a project for require lines and prints the Gemfile entries it needs:

require "bundled_gems"

dir = ARGV.fetch(0, ".")
found = Hash.new { |h, k| h[k] = [] }

Dir.glob("**/*.rb", base: dir).sort.each do |file|
  File.foreach(File.join(dir, file)).with_index(1) do |line, lineno|
    next unless line =~ /^\s*require\s+["']([^"']+)["']/
    feature = $1
    gem_name = feature.split("/").first
    gem_name = Gem::BUNDLED_GEMS::EXACT.fetch(gem_name, gem_name)
    next unless Gem::BUNDLED_GEMS::SINCE.key?(gem_name)
    found[gem_name] << "#{file}:#{lineno}"
  end
end

found.sort.each do |name, places|
  since = Gem::BUNDLED_GEMS::SINCE[name]
  status = Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(since) ? "bundled" : "default until #{since}"
  puts format("gem %-14s # %-20s %s", name.inspect, status, places.join(", "))
end

Given a project where lib/app.rb requires json, csv, logger, ostruct, set, tsort and base64, and lib/tool.rb requires bigdecimal/util, optparse and irb:

$ ruby check_bundled.rb proj
gem "base64"       # bundled              lib/app.rb:7
gem "bigdecimal"   # bundled              lib/tool.rb:1
gem "csv"          # bundled              lib/app.rb:2
gem "irb"          # bundled              lib/tool.rb:3
gem "logger"       # bundled              lib/app.rb:3
gem "ostruct"      # bundled              lib/app.rb:4
gem "tsort"        # default until 4.1.0  lib/app.rb:6

json and optparse are default gems, and set is a core class in Ruby 4.0, so they don't show up. It's a simple regexp scan, so it misses require calls built at runtime.

What happens under Bundler

Bundler limits require to the gems listed in your Gemfile.lock, plus the default gems. Take this Gemfile:

source "https://rubygems.org"

gem "rake"

And report.rb:

require "json"
puts JSON.generate(ok: true)

require "csv"
puts CSV.generate_line(["a", "b"])

json loads because it's a default gem. csv is installed on the machine, but it's not in the bundle:

$ bundle exec ruby report.rb
{"ok":true}
report.rb:4: warning: csv used to be loaded from the standard library, but is not part of the default gems since Ruby 3.4.0.
You can add csv to your Gemfile or gemspec to fix this error.
C:/Ruby40-x64/lib/ruby/4.0.0/bundled_gems.rb:60:in 'Kernel.require': cannot load such file -- csv (LoadError)
	from C:/Ruby40-x64/lib/ruby/4.0.0/bundled_gems.rb:60:in 'block (2 levels) in Kernel#replace_require'
	from report.rb:4:in '<main>'

The same script works with plain ruby report.rb. That's why this problem often shows up only in production or CI, where everything runs through bundle exec.

The logger gem from the 4.0 wave fails the same way:

$ bundle exec ruby lg.rb
lg.rb:1: warning: logger used to be loaded from the standard library, but is not part of the default gems since Ruby 4.0.0.
You can add logger to your Gemfile or gemspec to fix this error.
C:/Ruby40-x64/lib/ruby/4.0.0/bundled_gems.rb:60:in 'Kernel.require': cannot load such file -- logger (LoadError)
	from C:/Ruby40-x64/lib/ruby/4.0.0/bundled_gems.rb:60:in 'block (2 levels) in Kernel#replace_require'
	from lg.rb:1:in '<main>'

Executables of bundled gems are affected too. In Ruby 4.0 irb is a bundled gem, so bundle exec irb stops working until you add it:

$ bundle exec irb
bundler: failed to load command: irb (C:/Ruby40-x64/bin/irb)
C:/Ruby40-x64/lib/ruby/4.0.0/bundler/rubygems_integration.rb:236:in 'block in Gem.replace_bin_path': can't find executable irb for gem irb. irb is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
	from C:/Ruby40-x64/lib/ruby/4.0.0/rubygems.rb:239:in 'Gem.find_and_activate_spec_for_exe'
	...

The fix: add the gem to the Gemfile

source "https://rubygems.org"

gem "rake"
gem "csv"
$ bundle install
Fetching csv 3.3.6
Installing csv 3.3.6
Bundle complete! 2 Gemfile dependencies, 2 gems now installed.

$ bundle exec ruby report.rb
{"ok":true}
a,b

Bundler resolved csv like any other gem and picked 3.3.6 from rubygems.org, newer than the 3.3.5 that came with Ruby. That's the point of the change: you choose the version and update it with bundle update csv. You can pin it as usual, for example gem "csv", "~> 3.3".

For development tools, put them in a group:

group :development do
  gem "irb"
  gem "debug"
end

Adding a gem that is a default today, like tsort, is harmless. Bundler 4 treats it like any other gem: bundle install prints Installing tsort 0.2.0, the same version Ruby ships, and the app loads that copy instead of the one in the standard library. See Using a Gemfile and Bundler for Gemfile basics and Bundler inline for single-file scripts, where you list gem "csv" inside the gemfile do ... end block.

The "will no longer be part of the default gems" warning

For a gem that is still a default gem but is scheduled to move, Ruby warns instead of failing. In Ruby 4.0 that's tsort:

require "tsort"
puts TSort
$ ruby ts.rb
TSort

$ bundle exec ruby ts.rb
ts.rb:1: warning: tsort was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 4.1.0
You can add tsort to your Gemfile or gemspec to silence this warning.
TSort

The warning only appears under Bundler, and only once per gem. Ruby 3.3 showed it for csv, base64 and the rest of the 3.4 wave, and Ruby 3.4 showed it for logger and ostruct. If you see it, add the gem to your Gemfile now. On the next Ruby upgrade the warning becomes the LoadError shown above.

When the require comes from another gem

Often the require isn't in your code. It's in a gem you depend on. Here's a tiny gem, depsort, that uses tsort but doesn't list it as a dependency:

# lib/depsort.rb
require "tsort"

module Depsort
  class Graph < Hash
    include TSort
    alias tsort_each_node each_key
    def tsort_each_child(node, &) = fetch(node, []).each(&)
  end

  def self.order(deps) = Graph[deps].tsort
end
# depsort.gemspec
Gem::Specification.new do |spec|
  spec.name    = "depsort"
  spec.version = "0.1.0"
  spec.summary = "Sorts tasks by their dependencies"
  spec.authors = ["Example"]
  spec.files   = ["lib/depsort.rb"]
end

An app with gem "depsort" in its Gemfile runs this build.rb:

require "depsort"

p Depsort.order("app" => ["lib"], "lib" => ["config"], "config" => [])

It gets the warning, and the path (shortened here) points into the gem:

$ bundle exec ruby build.rb
C:/.../gems/depsort-0.1.0/lib/depsort.rb:1: warning: tsort was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 4.1.0
You can add tsort to your Gemfile or gemspec to silence this warning.
["config", "lib", "app"]

When it can work out which installed gem made the call, the message also says "Also please contact the author of ... to request adding tsort into its gemspec." (It didn't here.) Either way, the fix belongs in the gem: declare the dependency in the gemspec.

Gem::Specification.new do |spec|
  spec.name    = "depsort"
  spec.version = "0.2.0"
  spec.summary = "Sorts tasks by their dependencies"
  spec.authors = ["Example"]
  spec.files   = ["lib/depsort.rb"]
  spec.add_dependency "tsort"
end

After bundle update depsort, tsort shows up in Gemfile.lock as a dependency of depsort, and the warning is gone:

$ bundle exec ruby build.rb
["config", "lib", "app"]
GEM
  specs:
    depsort (0.2.0)
      tsort
    tsort (0.2.0)

Until the gem's author releases a fix, add the missing gem to your own Gemfile. That silences the warning (or fixes the error) for every gem in the bundle. If you write gems, add add_dependency for every bundled gem you require. Your gem's users might be on a newer Ruby than you are. See Dependencies for more on declaring them.

Dropping the dependency instead

Some of the moved libraries are small enough to replace with core Ruby, so you don't need the gem at all:

# Base64 without the base64 gem
encoded = ["hello, world"].pack("m0")
puts encoded
puts encoded.unpack1("m0")

# URL-safe variant: swap the two characters by hand
puts ["\xFF\xFE"].pack("m0").tr("+/", "-_")

# A Data class instead of OpenStruct
Point = Data.define(:x, :y)
pt = Point.new(x: 1, y: 2)
p pt
p pt.with(y: 5)
$ ruby nodeps.rb
aGVsbG8sIHdvcmxk
hello, world
__4=
#<data Point x=1, y=2>
#<data Point x=1, y=5>

These give the same results as Base64.strict_encode64 and Base64.urlsafe_encode64. For bigger libraries like csv, bigdecimal and logger, just add the gem. See CSV, Rational and BigDecimal and Logging.

Checklist for a Ruby upgrade

  1. Run your test suite with bundle exec on the current Ruby and look for "will no longer be part of the default gems" warnings. Each one names a gem to add.
  2. Run ruby check_bundled.rb (above) or grep -rn "require" lib app to find requires of bundled gems.
  3. Add those gems to the Gemfile (apps) or as add_dependency (gems), then bundle install.
  4. Upgrade the gems that warn about their own dependencies, or add those dependencies to your Gemfile until they are fixed.
  5. Add irb, debug, rdoc and rake to a development group if you run them with bundle exec.

To check whether a gem is installed from inside a program, see Checking if a required gem is installed from within code. For installing gems in general, see Installing Ruby gems.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents