A Ruby 4.0 upgrade for a Rails app is mostly a small, mechanical change. Ruby 4.0 has few breaking changes, and a few standard library gems moved out of the default set. The bigger reason to act now is security: on October 23, 2026, the Ruby team shipped fixes for regular expression engine bugs that GitLab reported as remote code execution risks, and the fixes only exist on Ruby 4.0.9, 3.4.12 and 3.3.13.
This article covers what changed in Ruby 4.0, which changes can affect a Rails app, a small audit script, and a rollout order that keeps the Ruby change separate from your Rails upgrade. It is written for Rails developers and for the CTOs and product owners who decide when this work gets scheduled. If you are also behind on Rails itself, read the Rails 8.1 upgrade guide first or alongside this one.
I could not install Ruby 4.0 in my environment, so I did not run these steps on Ruby 4.0. I ran the audit script in this article on Ruby 3.3.6 against a small sample project. Everything about Ruby 4.0 comes from the official release notes and the sources linked below, which I checked on November 8, 2026.
Where Ruby 4.0 stands in November 2026
Ruby 4.0.0 was released on December 25, 2025 (release announcement). As of November 8, 2026, the Ruby news page lists 4.0.9 (October 23, 2026) as the newest 4.0 patch, and Ruby 4.1.0-preview1 (October 12, 2026) as the next minor version in preview.
The Ruby branches data shows the support picture:
| Ruby branch | Status | Notes |
|---|---|---|
| 4.0 | Normal maintenance | No end-of-life date set yet |
| 3.4 | Normal maintenance | No end-of-life date set yet |
| 3.3 | Security maintenance | Expected end of life March 31, 2027 |
| 3.2 | End of life | Ended April 1, 2026 |
Rails 8.0 and 8.1 both require Ruby 3.2.0 or newer, according to the Upgrading Ruby on Rails guide. That minimum is already end-of-life, so an app that sits on Ruby 3.2 gets no security fixes for the language at all.
Why the October 2026 security release matters
The Ruby team’s security announcement describes bugs in Onigmo, Ruby’s regular expression engine. They can corrupt memory while a pattern is being compiled. GitLab reported that they could be used for remote code execution on its servers. The announcement lists four CVE IDs, assigned by GitLab: CVE-2026-89078, CVE-2026-93577, CVE-2026-97229 and CVE-2026-110264.
Who is affected, according to the announcement:
- Apps that compile a regular expression from a string an attacker controls, with
Regexp.new,Regexp.compile,Regexp.union, or interpolation such as/#{input}/. - Apps that use only regular expressions written by developers are not affected.
Regexp.timeoutdoes not protect against these bugs, and limiting pattern length is not reliable, because some bugs trigger with short patterns.
The fixed versions are Ruby 4.0.9, 3.4.12 and 3.3.13. The announcement adds that Ruby 3.3.13 is an exception to the security-only policy because GitLab assigned CVEs, and that Ruby has not requested its own CVE IDs yet. It does not say whether Ruby 3.2 will get a fix, so I would not plan on one.
For a Rails app, the risky pattern is easy to write by accident. A search box that turns user text into a regexp is the classic case:
# Risky: the pattern is built from a request parameter
Regexp.new(params[:q])
# Safer: user text is escaped, so it is matched literally
Regexp.new(Regexp.escape(params[:q]))Escaping the input is a sensible habit on any Ruby version. It does not replace upgrading, because regexps can also be built from data stored in your database or from settings that admins can edit.
What changed in Ruby 4.0 that can affect a Rails app
The Ruby 4.0.0 release notes list many changes. Most of them do not touch a typical Rails app. These are the ones I would check:
Gems that moved from default to bundled. ostruct, pstore, benchmark, logger, rdoc, irb, reline, readline, fiddle and win32ole are now bundled gems. A bundled gem is installed with Ruby, but Bundler does not load it unless it is in your Gemfile. Rails already depends on logger, irb, reline and rdoc in a typical app, so they appear in Gemfile.lock. If your own code requires ostruct or benchmark, add them to the Gemfile.
cgi was removed from the default gems, except cgi/escape, which keeps the escape and unescape methods. Code that calls CGI.escape still works. Code that requires cgi/session or other parts of cgi needs a gem or a rewrite. One community report on a Rails 8.1 upgrade mentions sitemap_generator hitting a cgi/session load error on Ruby 4.0, so check gems that touch CGI.
SortedSet is no longer autoloaded. If you use it, add the sorted_set gem and require it.
Net::HTTP no longer sets a Content-Type header automatically on requests with a body. Some servers may reject such requests. This affects code that posts to webhooks or third-party APIs through raw Net::HTTP. Higher-level clients usually set the header themselves, but a custom integration may not.
Process creation with a leading | in IO methods and Kernel#open is removed. Code that does this was already a security risk.
Backtraces and error messages changed. Internal frames no longer appear in backtraces, and “wrong number of arguments” errors now include the receiver’s class or module name. If you have tests that match exact error messages, or log parsers that match backtrace text, they may need updates.
Bundler 4 and RubyGems 4 ship with Ruby 4.0 (4.0.3 at release). A Gemfile.lock written by an older Bundler may be rewritten the first time you run bundle install, so expect lockfile changes in the diff.
Ruby Box and ZJIT are the headline features, but both are opt-in. The release notes call Ruby Box experimental, and they say ZJIT is faster than the interpreter but not yet as fast as YJIT, and advise against production use for now. I would leave both off. If you use YJIT today, the release notes do not suggest changing that.
Which Rails version works with Ruby 4.0
I could not find an official Rails statement that says “Rails x.y supports Ruby 4.0”. What I found is community evidence:
- A Heroku sample app pull request, merged on October 27, 2026, moved the app to Ruby 4.0.9 with Rails 8.1.4. The description says tests pass, assets precompile and the app boots, and that none of the compatibility changes in the Ruby 4.0 release notes required changes to the app. That is a small app, so it shows the upgrade is feasible, not that yours will be painless.
- A Rails 8.1 and Ruby 4.0 upgrade issue credits Rails 8.1.2 (January 22, 2026) with fixing a
Delegator#inspectwarning on Ruby 4.0, and mentions anecdotal reports of green test suites on later 8.1 patches. Treat that as a report, not a guarantee.
My recommendation: use the latest Rails 8.1 patch release, not 8.1.0 or 8.1.1, and let your own test suite be the proof. If you are on Rails 7.2 or 8.0, do the Rails upgrade on your current Ruby first, as described in the Rails 8.1 upgrade guide. Moving to 8.1 and to Ruby 4.0 in one deploy makes failures hard to trace.
An audit script for the usual suspects
Before you bump anything, scan your code for the patterns above. This script prints a file, a line number and a reason for each match, and exits with status 1 if it finds any. It is a quick filter, not a parser, so expect some false positives.
#!/usr/bin/env ruby
# bin/ruby4_audit.rb: flag code that Ruby 4.0 changed or that builds
# regexps from strings. Prints file:line and a reason, then exits 1.
CHECKS = {
/require\s+["'](ostruct|benchmark|pstore|logger|irb)["']/ =>
"now a bundled gem: add it to the Gemfile if you use it directly",
/require\s+["']cgi(\/session|\/cookie|\/util)?["']/ =>
"cgi was removed from default gems (only cgi/escape remains)",
/SortedSet/ => "no longer autoloaded: add the sorted_set gem",
/Regexp\.(new|compile|union)\((?!\s*Regexp\.escape)|\/[^\/\n]*#\{/ =>
"regexp built from a string: make sure input is never user-controlled"
}.freeze
hits = 0
Dir.glob("{app,lib,config}/**/*.rb").sort.each do |file|
File.foreach(file).with_index(1) do |line, number|
CHECKS.each do |pattern, reason|
next unless line.match?(pattern)
puts "#{file}:#{number}: #{reason}"
hits += 1
end
end
end
exit(hits.zero? ? 0 : 1)I ran it on Ruby 3.3.6 against a sample project with three files. It flagged require "ostruct", an unescaped Regexp.new(params[:q]) and require "cgi". It did not flag require "cgi/escape" or Regexp.new(Regexp.escape(params[:q])). It only reads files under app, lib and config, so it does not scan gems, views or rake tasks. For ERB and rake files, use a plain grep -rn "Regexp.new" app lib.
The script also cannot see gems. For those, the real test is bundle update on a branch followed by your full test suite.
A safe rollout order
I recommend shipping the Ruby change as its own release. These steps assume you are already on a supported Rails version.
- Move to the latest patch of your current Ruby first. If you are on 3.3 or 3.4, take 3.3.13 or 3.4.12. This closes the regexp bugs immediately and gives you a fast rollback point.
- Run the audit script and fix what it reports. Add any bundled gems you use directly to the
Gemfile. - Search for user-controlled regexps and escape the input or remove the feature.
- Update the Ruby version everywhere it appears. In a typical Rails app that means
.ruby-version, therubyline in theGemfile, the Dockerfile, and your CI configuration. Missing one gives you “works in CI, fails in production” surprises. If you deploy with Docker, see my Kamal deployment guide for Rails 8. - Run
bundle updatewith care. Update gems that fail to install or load on Ruby 4.0, not everything at once. Check that native extensions compile. - Run the full test suite and boot the app with production settings. Compile assets, run a migration against a copy of production data, and exercise background jobs, because job code often uses
BenchmarkorOpenStruct. - Deploy to staging, then to production with a quick rollback plan. Watch error rates and memory for a few days. A new Ruby version can change memory behavior, so compare before and after.
Do not enable Ruby Box or ZJIT as part of this release.
Ruby 4.0 or the latest 3.4: which should you pick
Both are in normal maintenance. Ruby 3.4.12 gets the regexp fixes too. The choice is about risk and runway.
| Situation | What I would pick |
|---|---|
| App on Ruby 3.2 or older | Go to 3.4.12 first, then plan 4.0 as a separate step |
| App on 3.3 with a strong test suite | Ruby 4.0.9 is reasonable, using the latest Rails 8.1 patch |
| App on 3.3 with thin tests | Move to 3.4.12 now, and write tests before going further |
| New project | Ruby 4.0.9, with the latest Rails 8.1 patch |
Ruby 3.3’s expected end of life is March 31, 2027, so apps on 3.3 have a deadline. Starting the move now spreads the work out.
If your app is on an old Ruby or Rails version and you don’t have the test coverage to upgrade with confidence, I can help. I handle Ruby and Rails upgrades as separate, reversible releases. See my legacy Rails modernization service.
Conclusion
Ruby 4.0 is an incremental upgrade for most Rails apps: add the bundled gems you use, check for cgi and SortedSet, review Net::HTTP calls and test for changed error messages. The urgent part is security. If your app builds regular expressions from untrusted strings, upgrade to Ruby 4.0.9, 3.4.12 or 3.3.13, and escape the input either way.
Treat the Ruby upgrade as its own release, use the latest Rails 8.1 patch, and rely on your test suite rather than on anyone’s compatibility claim, including mine. I did not run Ruby 4.0 for this article, so test it on a branch before you commit to a date.
If you want help planning this, I can audit your app for upgrade risks, fix gem and code incompatibilities, and ship the Ruby and Rails upgrades step by step with a rollback plan. Send me a short brief and book a discovery call, and I will tell you how large the job is.
Frequently asked questions
Does Rails 8.1 work with Ruby 4.0?
There is no official Rails statement that I could find. Community reports show Rails 8.1 apps running on Ruby 4.0, including a Heroku sample app on Rails 8.1.4 and Ruby 4.0.9. Rails 8.1.2 reportedly fixed a Ruby 4.0 warning. Use the latest 8.1 patch and run your own tests.
Is Ruby 4.0 stable enough for production?
Ruby 4.0 is in normal maintenance, and the project shipped 4.0.9 on October 23, 2026. The release notes mark Ruby Box as experimental and advise against using ZJIT in production, so leave both off. The rest of the language is a standard release.
Do I need to upgrade Ruby because of the October 2026 regexp bugs?
If your app compiles regular expressions from untrusted strings, yes, and you should also escape that input. If you only use developer-written regexps, the announcement says you are not affected. Staying on a supported branch is still wise, because fixes only land there.
Is Ruby 3.2 still supported?
No. Ruby 3.2 reached end of life on April 1, 2026, according to the Ruby branches data. Rails 8.0 and 8.1 still allow it as a minimum version, so a Rails app can run on a Ruby with no security fixes.
Which gems did Ruby 4.0 move to bundled gems?
ostruct, pstore, benchmark, logger, rdoc, irb, reline, readline, fiddle and win32ole. If your code requires one of them directly, add it to the Gemfile. Rails apps usually already have logger, irb, reline and rdoc through Rails.
Should I upgrade Ruby and Rails at the same time?
No. The official Rails upgrade guide recommends upgrading Ruby and Rails separately, and I agree. One change per release means that when something breaks, you know why.
Should I turn on ZJIT?
Not yet. The Ruby 4.0 release notes say ZJIT is faster than the interpreter but not yet as fast as YJIT, and they advise against production use for now. Keep YJIT if you use it.
References
- Ruby 4.0.0 released, Ruby news, December 25, 2025
- Regexp fixes for GitLab CVEs, Ruby news, October 23, 2026
- Ruby branches data, ruby-lang.org repository
- Upgrading Ruby on Rails, Rails Guides


