创建Rails应用遇Ruby版本不匹配错误,修改后是否正常?
Ruby版本不匹配导致Rails项目创建错误的解决与验证
问题描述
安装Ruby和Rails后,执行rails new <项目名称>创建应用时,项目虽创建完成,但末尾出现版本不匹配错误:
Bundle complete! 15 Gemfile dependencies, 73 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed. run bundle binstubs bundler rails importmap:install C:/Ruby32-x64/lib/ruby/3.2.0/bundler/definition.rb:442:in `validate_ruby!': Your Ruby version is 3.2.2, but your Gemfile specified 3.1.2 (Bundler::RubyVersionMi smatch) from C:/Ruby32-x64/lib/ruby/3.2.0/bundler/definition.rb:417:in `validate _runtime!' from C:/Ruby32-x64/lib/ruby/3.2.0/bundler.rb:165:in `setup' from C:/Ruby32-x64/lib/ruby/3.2.0/bundler/setup.rb:23:in `block in <top (required)>' from C:/Ruby32-x64/lib/ruby/3.2.0/bundler/ui/shell.rb:159:in `with_level ' from C:/Ruby32-x64/lib/ruby/3.2.0/bundler/ui/shell.rb:111:in `silence' from C:/Ruby32-x64/lib/ruby/3.2.0/bundler/setup.rb:23:in `<top (required )>' from <internal:C:/Ruby32-x64/lib/ruby/3.2.0/rubygems/core_ext/kernel_req uire.rb>:85:in `require' from <internal:C:/Ruby32-x64/lib/ruby/3.2.0/rubygems/core_ext/kernel_req uire.rb>:85:in `require' from D:/railsfriends/friends/config/boot.rb:3:in `<top (required)>' from bin/rails:3:in `require_relative' from bin/rails:3:in `<main>' rails turbo:install stimulus:install C:/Ruby32-x64/lib/ruby/3.2.0/bundler/definition.rb:442:in `validate_ruby!': Your Ruby version is 3.2.2, but your Gemfile specified 3.1.2 (Bundler::RubyVersionMi smatch) from C:/Ruby32-x64/lib/ruby/3.2.0/bundler/definition.rb:417:in `validate _runtime!' from C:/Ruby32-x64/lib/ruby/3.2.0/bundler.rb:165:in `setup' from C:/Ruby32-x64/lib/ruby/3.2.0/bundler/setup.rb:23:in `block in <top (required)>' from C:/Ruby32-x64/lib/ruby/3.2.0/bundler/ui/shell.rb:159:in `with_level ' from C:/Ruby32-x64/lib/ruby/3.2.0/bundler/ui/shell.rb:111:in `silence' from C:/Ruby32-x64/lib/ruby/3.2.0/bundler/setup.rb:23:in `<top (required )>' from <internal:C:/Ruby32-x64/lib/ruby/3.2.0/rubygems/core_ext/kernel_req uire.rb>:85:in `require' from <internal:C:/Ruby32-x64/lib/ruby/3.2.0/rubygems/core_ext/kernel_req uire.rb>:85:in `require' from D:/railsfriends/friends/config/boot.rb:3:in `<top (required)>' from bin/rails:3:in `require_relative' from bin/rails:3:in `<main>'
疑问
- 该错误如何解决?
- 此错误会造成什么影响?
操作与后续验证
修改项目Gemfile中的ruby "3.1.2"为ruby "3.2.2",删除原项目后重新执行rails new <项目名称>,得到输出:
Bundle complete! 15 Gemfile dependencies, 73 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed. run bundle binstubs bundler rails importmap:install Add Importmap include tags in application layout insert app/views/layouts/application.html.erb Create application.js module as entrypoint create app/javascript/application.js Use vendor/javascript for downloaded pins create vendor/javascript create vendor/javascript/.keep Ensure JavaScript files are in the Sprocket manifest append app/assets/config/manifest.js Configure importmap paths in config/importmap.rb create config/importmap.rb Copying binstub create bin/importmap rails turbo:install stimulus:install Import Turbo append app/javascript/application.js Pin Turbo append config/importmap.rb Create controllers directory create app/javascript/controllers create app/javascript/controllers/index.js create app/javascript/controllers/application.js create app/javascript/controllers/hello_controller.js Import Stimulus controllers append app/javascript/application.js Pin Stimulus Appending: pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true" append config/importmap.rb Appending: pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true append config/importmap.rb Pin all controllers Appending: pin_all_from "app/javascript/controllers", under: "controllers" append config/importmap.rb Run turbo:install:redis to switch on Redis and use it in development for turbo streams
当前项目Gemfile已显示ruby "3.2.2",疑问:当前应用是否已正常运行?
解答
错误原因与解决方法
这个错误是Bundler版本校验不匹配:本地安装的Ruby版本为3.2.2,但Rails生成的Gemfile指定了3.1.2,导致Bundler在执行后续的importmap:install和turbo:install stimulus:install命令时失败。
两种有效解决方式:
- 方式一:修改现有项目Gemfile中的Ruby版本为本地的3.2.2,执行
bundle install,再手动运行rails importmap:install和rails turbo:install stimulus:install补全缺失的初始化文件。 - 方式二:删除原项目,重新创建时通过
rails new <项目名> --ruby=3.2.2指定版本,确保生成的Gemfile版本与本地一致。
错误的影响
该错误会导致Rails默认前端工具链(Importmap、Turbo、Stimulus)未完成安装,项目缺少对应配置文件和初始化代码,直接启动项目会出现前端功能失效,甚至启动报错的问题。
当前应用状态验证
从重新创建项目后的输出来看,所有初始化命令均执行成功:Importmap和Turbo/Stimulus的配置文件、目录已全部创建,无版本错误提示,且Gemfile版本与本地Ruby一致。
此时可进入项目目录执行rails server启动服务器,访问http://localhost:3000,若能看到Rails默认欢迎页面,说明应用已正常运行。
内容的提问来源于stack exchange,提问作者yasuo
相关产品推荐
相关产品推荐

