运行 rails 新命令时遇到错误

Error encountered when running rails new command

我想在 Rails 开始学习 Ruby。我已经通过 RVM 安装了 ruby 2.7.0。 SQLite3 也默认安装在 ubuntu 20.04.3 LTS 中。我的 rails 版本是 7.0.2.2。当我 运行 命令时:

rails new blog

来自本指南: https://guides.rubyonrails.org/getting_started.html

它抛出一个错误:

lou@lou-VirtualBox:~$ rails new blog
      create  
      create  README.md
      create  Rakefile
      create  .ruby-version
      create  config.ru
      create  .gitignore
      create  .gitattributes
      create  Gemfile
         run  git init from "."
Traceback (most recent call last):
    27: from /home/lou/.rvm/gems/ruby-2.7.0/bin/ruby_executable_hooks:24:in `<main>'
    26: from /home/lou/.rvm/gems/ruby-2.7.0/bin/ruby_executable_hooks:24:in `eval'
    25: from /home/lou/.rvm/gems/ruby-2.7.0/bin/rails:23:in `<main>'
    24: from /home/lou/.rvm/gems/ruby-2.7.0/bin/rails:23:in `load'
    23: from /home/lou/.rvm/gems/ruby-2.7.0/gems/railties-7.0.2.2/exe/rails:10:in `<top (required)>'
    22: from /home/lou/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in `require'
    21: from /home/lou/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in `require'
    20: from /home/lou/.rvm/gems/ruby-2.7.0/gems/railties-7.0.2.2/lib/rails/cli.rb:18:in `<top (required)>'
    19: from /home/lou/.rvm/gems/ruby-2.7.0/gems/railties-7.0.2.2/lib/rails/command.rb:48:in `invoke'
    18: from /home/lou/.rvm/gems/ruby-2.7.0/gems/railties-7.0.2.2/lib/rails/command/base.rb:87:in `perform'
    17: from /home/lou/.rvm/gems/ruby-2.7.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
    16: from /home/lou/.rvm/gems/ruby-2.7.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
    15: from /home/lou/.rvm/gems/ruby-2.7.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
    14: from /home/lou/.rvm/gems/ruby-2.7.0/gems/railties-7.0.2.2/lib/rails/commands/application/application_command.rb:26:in `perform'
    13: from /home/lou/.rvm/gems/ruby-2.7.0/gems/thor-1.2.1/lib/thor/base.rb:485:in `start'
    12: from /home/lou/.rvm/gems/ruby-2.7.0/gems/thor-1.2.1/lib/thor/group.rb:232:in `dispatch'
    11: from /home/lou/.rvm/gems/ruby-2.7.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `invoke_all'
    10: from /home/lou/.rvm/gems/ruby-2.7.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `map'
     9: from /home/lou/.rvm/gems/ruby-2.7.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `each'
     8: from /home/lou/.rvm/gems/ruby-2.7.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `block in invoke_all'
     7: from /home/lou/.rvm/gems/ruby-2.7.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
     6: from /home/lou/.rvm/gems/ruby-2.7.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
     5: from /home/lou/.rvm/gems/ruby-2.7.0/gems/railties-7.0.2.2/lib/rails/generators/rails/app/app_generator.rb:328:in `create_root_files'
     4: from /home/lou/.rvm/gems/ruby-2.7.0/gems/railties-7.0.2.2/lib/rails/generators/app_base.rb:134:in `build'
     3: from /home/lou/.rvm/gems/ruby-2.7.0/gems/railties-7.0.2.2/lib/rails/generators/app_base.rb:134:in `public_send'
     2: from /home/lou/.rvm/gems/ruby-2.7.0/gems/railties-7.0.2.2/lib/rails/generators/rails/app/app_generator.rb:76:in `version_control'
     1: from /home/lou/.rvm/gems/ruby-2.7.0/gems/railties-7.0.2.2/lib/rails/generators/rails/app/app_generator.rb:258:in `user_default_branch'
/home/lou/.rvm/gems/ruby-2.7.0/gems/railties-7.0.2.2/lib/rails/generators/rails/app/app_generator.rb:258:in ``': No such file or directory - git (Errno::ENOENT)

我找不到与我相关的问题,所以我希望有人能帮助我。

我解决了我的问题。看起来它还需要安装 git。

添加此答案以提供一些背景知识。

要解决此问题,请安装 git

Rails 尝试 运行 git init from "." 初始化当前工作目录中的存储库。

Rails reads the git default branch, see this post on superuser.com about the default branch.

要在git > 2.28中配置init.defaultBranch参数,可以运行 git config --global init.defaultBranch main.

不确定为什么要这样做,但您可以使用以下控制台选项跳过此步骤:
-G, [--skip-git], [--no-skip-git]
-p, [--pretend], [--no-pretend]

请参阅 Rails code 和 运行 rails new --help 以了解更多信息。