diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb
index 137fbead8498e23e0087c8687f6baf12f9586da2..8d293f647e5b2b5ead41553217888114b7daf1f6 100644
--- a/Library/Homebrew/brew.rb
+++ b/Library/Homebrew/brew.rb
@@ -12,9 +12,15 @@ raise "HOMEBREW_BREW_FILE was not exported! Please call bin/brew directly!" unle
 std_trap = trap("INT") { exit! 130 } # no backtrace thanks
 
 # check ruby version before requiring any modules.
+unless ENV["HOMEBREW_REQUIRED_RUBY_VERSION"]
+  raise "HOMEBREW_REQUIRED_RUBY_VERSION was not exported! Please call bin/brew directly!"
+end
+
+REQUIRED_RUBY_X, REQUIRED_RUBY_Y, = ENV["HOMEBREW_REQUIRED_RUBY_VERSION"].split(".").map(&:to_i)
 RUBY_X, RUBY_Y, = RUBY_VERSION.split(".").map(&:to_i)
-if RUBY_X < 2 || (RUBY_X == 2 && RUBY_Y < 6)
-  raise "Homebrew must be run under Ruby 2.6! You're running #{RUBY_VERSION}."
+if RUBY_X < REQUIRED_RUBY_X || (RUBY_X == REQUIRED_RUBY_X && RUBY_Y < REQUIRED_RUBY_Y)
+  raise "Homebrew must be run under Ruby #{REQUIRED_RUBY_X}.#{REQUIRED_RUBY_Y}! " \
+        "You're running #{RUBY_VERSION}."
 end
 
 # Also define here so we can rescue regardless of location.
diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb
index 107998cde502aeb9f863e2b664e0f6d2aa24731a..c84ea6c939f49e649db5bf654dc7bf9280f801eb 100644
--- a/Library/Homebrew/cleanup.rb
+++ b/Library/Homebrew/cleanup.rb
@@ -348,24 +348,26 @@ module Homebrew
     end
 
     def cleanup_portable_ruby
-      system_ruby_version =
-        Utils.popen_read("/usr/bin/ruby", "-e", "puts RUBY_VERSION")
-             .chomp
-      use_system_ruby = (
-        Gem::Version.new(system_ruby_version) >= Gem::Version.new(RUBY_VERSION)
-      ) && !Homebrew::EnvConfig.force_vendor_ruby?
-      vendor_path = HOMEBREW_LIBRARY/"Homebrew/vendor"
-      portable_ruby_version_file = vendor_path/"portable-ruby-version"
-      portable_ruby_version = if portable_ruby_version_file.exist?
-        portable_ruby_version_file.read
-                                  .chomp
+      rubies = [which("ruby"), which("ruby", ENV["HOMEBREW_PATH"])].compact
+      system_ruby = Pathname.new("/usr/bin/ruby")
+      rubies << system_ruby if system_ruby.exist?
+
+      use_system_ruby = if Homebrew::EnvConfig.force_vendor_ruby?
+        false
+      else
+        check_ruby_version = HOMEBREW_LIBRARY_PATH/"utils/ruby_check_version_script.rb"
+        rubies.uniq.any? do |ruby|
+          system ruby, "--enable-frozen-string-literal", "--disable=gems,did_you_mean,rubyopt",
+                 check_ruby_version, HOMEBREW_REQUIRED_RUBY_VERSION
+        end
       end
 
-      portable_ruby_path = vendor_path/"portable-ruby"
-      portable_ruby_glob = "#{portable_ruby_path}/*.*"
+      vendor_dir = HOMEBREW_LIBRARY/"Homebrew/vendor"
+      portable_ruby_latest_version = (vendor_dir/"portable-ruby-version").read.chomp
+
       portable_rubies_to_remove = []
-      Pathname.glob(portable_ruby_glob).each do |path|
-        next if !use_system_ruby && portable_ruby_version == path.basename.to_s
+      Pathname.glob(vendor_dir/"portable-ruby/*.*").select(&:directory?).each do |path|
+        next if !use_system_ruby && portable_ruby_latest_version == path.basename.to_s
 
         portable_rubies_to_remove << path
         puts "Would remove: #{path} (#{path.abv})" if dry_run?
@@ -373,7 +375,7 @@ module Homebrew
 
       return if portable_rubies_to_remove.empty?
 
-      bundler_path = vendor_path/"bundle/ruby"
+      bundler_path = vendor_dir/"bundle/ruby"
       if dry_run?
         puts Utils.popen_read("git", "-C", HOMEBREW_REPOSITORY, "clean", "-nx", bundler_path).chomp
       else
diff --git a/Library/Homebrew/extend/os/mac/diagnostic.rb b/Library/Homebrew/extend/os/mac/diagnostic.rb
index 21cd0faee4ea0b44b38330d7f2defaf1c454d00b..ff7d549fbe83802d25ab06cdf07405dc0e854236 100644
--- a/Library/Homebrew/extend/os/mac/diagnostic.rb
+++ b/Library/Homebrew/extend/os/mac/diagnostic.rb
@@ -178,13 +178,12 @@ module Homebrew
       end
 
       def check_ruby_version
-        ruby_version = "2.6.3"
-        return if RUBY_VERSION == ruby_version
+        return if RUBY_VERSION == HOMEBREW_REQUIRED_RUBY_VERSION
         return if Homebrew::EnvConfig.developer? && OS::Mac.prerelease?
 
         <<~EOS
           Ruby version #{RUBY_VERSION} is unsupported on #{MacOS.version}. Homebrew
-          is developed and tested on Ruby #{ruby_version}, and may not work correctly
+          is developed and tested on Ruby #{HOMEBREW_REQUIRED_RUBY_VERSION}, and may not work correctly
           on other Rubies. Patches are accepted as long as they don't cause breakage
           on supported Rubies.
         EOS
diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb
index c0e374bff3191aeab084e7b195d4a51f292b6aaa..3159ea53883c7e312381f22820af4df8aba4f736 100644
--- a/Library/Homebrew/global.rb
+++ b/Library/Homebrew/global.rb
@@ -37,6 +37,7 @@ HOMEBREW_CORE_DEFAULT_GIT_REMOTE = ENV["HOMEBREW_CORE_DEFAULT_GIT_REMOTE"]
 HOMEBREW_DEFAULT_CACHE = ENV["HOMEBREW_DEFAULT_CACHE"]
 HOMEBREW_DEFAULT_LOGS = ENV["HOMEBREW_DEFAULT_LOGS"]
 HOMEBREW_DEFAULT_TEMP = ENV["HOMEBREW_DEFAULT_TEMP"]
+HOMEBREW_REQUIRED_RUBY_VERSION = ENV["HOMEBREW_REQUIRED_RUBY_VERSION"]
 require "env_config"
 
 require "config"
diff --git a/Library/Homebrew/test/cleanup_spec.rb b/Library/Homebrew/test/cleanup_spec.rb
index c5cd2fb1652a7a54c648507055240443f6379978..cb15b9cb1bb4d55caecffe32ecf5b85e63f3e66f 100644
--- a/Library/Homebrew/test/cleanup_spec.rb
+++ b/Library/Homebrew/test/cleanup_spec.rb
@@ -36,11 +36,14 @@ describe Homebrew::Cleanup do
   around do |example|
     FileUtils.touch ds_store
     FileUtils.touch lock_file
+    FileUtils.mkdir_p HOMEBREW_LIBRARY/"Homebrew/vendor"
+    FileUtils.touch HOMEBREW_LIBRARY/"Homebrew/vendor/portable-ruby-version"
 
     example.run
   ensure
     FileUtils.rm_f ds_store
     FileUtils.rm_f lock_file
+    FileUtils.rm_rf HOMEBREW_LIBRARY/"Homebrew"
   end
 
   describe "::cleanup" do
diff --git a/Library/Homebrew/test/cmd/cleanup_spec.rb b/Library/Homebrew/test/cmd/cleanup_spec.rb
index a822752c3f7a7ab8f16b8d4254430ad2633e1d8b..54d66f74ec1822e4448521018036353aeaa735d7 100644
--- a/Library/Homebrew/test/cmd/cleanup_spec.rb
+++ b/Library/Homebrew/test/cmd/cleanup_spec.rb
@@ -7,6 +7,15 @@ describe "Homebrew.cleanup_args" do
 end
 
 describe "brew cleanup", :integration_test do
+  before do
+    FileUtils.mkdir_p HOMEBREW_LIBRARY/"Homebrew/vendor/"
+    FileUtils.touch HOMEBREW_LIBRARY/"Homebrew/vendor/portable-ruby-version"
+  end
+
+  after do
+    FileUtils.rm_rf HOMEBREW_LIBRARY/"Homebrew"
+  end
+
   describe "--prune=all" do
     it "removes all files in Homebrew's cache" do
       (HOMEBREW_CACHE/"test").write "test"
diff --git a/Library/Homebrew/utils/ruby.sh b/Library/Homebrew/utils/ruby.sh
index ba73affb9d379f1babc67980cc0c811fb41a3911..bcd18112018b785078970decec506ef16a699b48 100644
--- a/Library/Homebrew/utils/ruby.sh
+++ b/Library/Homebrew/utils/ruby.sh
@@ -1,12 +1,14 @@
+export HOMEBREW_REQUIRED_RUBY_VERSION=2.6.3
+
 test_ruby () {
   if [[ ! -x $1 ]]
   then
     return 1
   fi
 
-  "$1" --enable-frozen-string-literal --disable=gems,did_you_mean,rubyopt -rrubygems -e \
-    "abort if Gem::Version.new(RUBY_VERSION.to_s.dup).to_s.split('.').first(2) != \
-              Gem::Version.new('$required_ruby_version').to_s.split('.').first(2)" 2>/dev/null
+  "$1" --enable-frozen-string-literal --disable=gems,did_you_mean,rubyopt \
+    "$HOMEBREW_LIBRARY/Homebrew/utils/ruby_check_version_script.rb" \
+    "$HOMEBREW_REQUIRED_RUBY_VERSION" 2>/dev/null
 }
 
 find_ruby() {
@@ -47,7 +49,6 @@ setup-ruby-path() {
   local vendor_ruby_current_version
   # When bumping check if HOMEBREW_MACOS_SYSTEM_RUBY_NEW_ENOUGH (in brew.sh)
   # also needs to be changed.
-  local required_ruby_version="2.6"
   local ruby_exec
   local upgrade_fail
   local install_fail
@@ -59,12 +60,12 @@ setup-ruby-path() {
   else
     local advice="
 If there's no Homebrew Portable Ruby available for your processor:
-- install Ruby $required_ruby_version with your system package manager (or rbenv/ruby-build)
+- install Ruby $HOMEBREW_REQUIRED_RUBY_VERSION with your system package manager (or rbenv/ruby-build)
 - make it first in your PATH
 - try again
 "
     upgrade_fail="Failed to upgrade Homebrew Portable Ruby!$advice"
-    install_fail="Failed to install Homebrew Portable Ruby and cannot find another Ruby $required_ruby_version!$advice"
+    install_fail="Failed to install Homebrew Portable Ruby and cannot find another Ruby $HOMEBREW_REQUIRED_RUBY_VERSION!$advice"
   fi
 
   vendor_dir="$HOMEBREW_LIBRARY/Homebrew/vendor"
diff --git a/Library/Homebrew/utils/ruby_check_version_script.rb b/Library/Homebrew/utils/ruby_check_version_script.rb
new file mode 100755
index 0000000000000000000000000000000000000000..3b607cdb201c9234a9f112a8bbc8a8d8382a1503
--- /dev/null
+++ b/Library/Homebrew/utils/ruby_check_version_script.rb
@@ -0,0 +1,22 @@
+#!/usr/bin/env ruby --enable-frozen-string-literal --disable=gems,did_you_mean,rubyopt
+# frozen_string_literal: true
+
+HOMEBREW_REQUIRED_RUBY_VERSION = ARGV.first.freeze
+raise "No Ruby version passed!" if HOMEBREW_REQUIRED_RUBY_VERSION.to_s.empty?
+
+require "rubygems"
+
+ruby_version = Gem::Version.new(RUBY_VERSION)
+# This will only happen if the Ruby is too old anyway.
+abort unless ruby_version.respond_to?(:canonical_segments)
+
+homebrew_required_ruby_version = Gem::Version.new(HOMEBREW_REQUIRED_RUBY_VERSION)
+
+ruby_version_major, ruby_version_minor, = ruby_version.canonical_segments
+homebrew_required_ruby_version_major, homebrew_required_ruby_version_minor, =
+  homebrew_required_ruby_version.canonical_segments
+
+if ruby_version_major != homebrew_required_ruby_version_major ||
+   ruby_version_minor != homebrew_required_ruby_version_minor
+  abort
+end