Rails環境構築エラーメモ

# 環境

- Windows 11 (WSL2)
- シェル：Bash

# やりたいこと

大学の教育プログラムでRuby on Railsをいじることになった。
エラーが出たので解決法のメモ

```bash
rails new <app name>
```

# エラー内容

<details><summary>エラー内容</summary>

```
$ rails new pdiary --skip-git
Based on the specified options, the following options will also be activated:

  --skip-decrypted-diffs [due to --skip-git]

      create  
      create  README.md
      create  Rakefile
      create  .ruby-version
      create  config.ru
      create  Gemfile
      create  app
      create  app/assets/config/manifest.js
      create  app/assets/stylesheets/application.css
      create  app/channels/application_cable/channel.rb
      create  app/channels/application_cable/connection.rb
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/jobs/application_job.rb
      create  app/mailers/application_mailer.rb
      create  app/models/application_record.rb
      create  app/views/layouts/application.html.erb
      create  app/views/layouts/mailer.html.erb
      create  app/views/layouts/mailer.text.erb
      create  app/assets/images
      create  app/assets/images/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/rails
      create  bin/rake
      create  bin/setup
      create  Dockerfile
      create  .dockerignore
      create  bin/docker-entrypoint
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/cable.yml
      create  config/puma.rb
      create  config/storage.yml
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/assets.rb
      create  config/initializers/content_security_policy.rb
      create  config/initializers/cors.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/new_framework_defaults_7_1.rb
      create  config/initializers/permissions_policy.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/master.key
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/apple-touch-icon-precomposed.png
      create  public/apple-touch-icon.png
      create  public/favicon.ico
      create  public/robots.txt
      create  tmp
      create  tmp/.keep
      create  tmp/pids
      create  tmp/pids/.keep
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor
      create  vendor/.keep
      create  test/fixtures/files
      create  test/fixtures/files/.keep
      create  test/controllers
      create  test/controllers/.keep
      create  test/mailers
      create  test/mailers/.keep
      create  test/models
      create  test/models/.keep
      create  test/helpers
      create  test/helpers/.keep
      create  test/integration
      create  test/integration/.keep
      create  test/channels/application_cable/connection_test.rb
      create  test/test_helper.rb
      create  test/system
      create  test/system/.keep
      create  test/application_system_test_case.rb
      create  storage
      create  storage/.keep
      create  tmp/storage
      create  tmp/storage/.keep
      remove  config/initializers/cors.rb
      remove  config/initializers/new_framework_defaults_7_1.rb
         run  bundle install
Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies...
Fetching public_suffix 6.0.1
Fetching rexml 3.3.2
Fetching turbo-rails 2.0.6

Retrying download gem from https://rubygems.org/ due to error (2/4): Bundler::PermissionError There was an error while trying to write to `/var/lib/gems/3.0.0/cache/public_suffix-6.0.1.gem`. It is likely that you need to grant write permissions for that path.

Retrying download gem from https://rubygems.org/ due to error (2/4): Bundler::PermissionError There was an error while trying to write to `/var/lib/gems/3.0.0/cache/rexml-3.3.2.gem`. It is likely that you need to grant write permissions for that path.

Retrying download gem from https://rubygems.org/ due to error (2/4): Bundler::PermissionError There was an error while trying to write to `/var/lib/gems/3.0.0/cache/turbo-rails-2.0.6.gem`. It is likely that you need to grant write permissions for that path.

Retrying download gem from https://rubygems.org/ due to error (3/4): Bundler::PermissionError There was an error while trying to write to `/var/lib/gems/3.0.0/cache/public_suffix-6.0.1.gem`. It is likely that you need to grant write permissions for that path.

Retrying download gem from https://rubygems.org/ due to error (3/4): Bundler::PermissionError There was an error while trying to write to `/var/lib/gems/3.0.0/cache/rexml-3.3.2.gem`. It is likely that you need to grant write permissions for that path.

Retrying download gem from https://rubygems.org/ due to error (3/4): Bundler::PermissionError There was an error while trying to write to `/var/lib/gems/3.0.0/cache/turbo-rails-2.0.6.gem`. It is likely that you need to grant write permissions for that path.

Retrying download gem from https://rubygems.org/ due to error (4/4): Bundler::PermissionError There was an error while trying to write to `/var/lib/gems/3.0.0/cache/public_suffix-6.0.1.gem`. It is likely that you need to grant write permissions for that path.

Retrying download gem from https://rubygems.org/ due to error (4/4): Bundler::PermissionError There was an error while trying to write to `/var/lib/gems/3.0.0/cache/rexml-3.3.2.gem`. It is likely that you need to grant write permissions for that path.

Retrying download gem from https://rubygems.org/ due to error (4/4): Bundler::PermissionError There was an error while trying to write to `/var/lib/gems/3.0.0/cache/turbo-rails-2.0.6.gem`. It is likely that you need to grant write permissions for that path.



Bundler::PermissionError: There was an error while trying to write to
`/var/lib/gems/3.0.0/cache/public_suffix-6.0.1.gem`. It is likely that you need to grant write
permissions for that path.
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/shared_helpers.rb:107:in `rescue in
filesystem_access'
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/shared_helpers.rb:104:in `filesystem_access'
/var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/rubygems_integration.rb:445:in `block in
download_gem'
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/retry.rb:40:in `run'
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/retry.rb:30:in `attempt'
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/rubygems_integration.rb:437:in `download_gem'
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/source/rubygems.rb:489:in `download_gem'
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/source/rubygems.rb:446:in `fetch_gem'
/var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/source/rubygems.rb:430:in
`fetch_gem_if_possible'
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/source/rubygems.rb:162:in `install'
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/installer/gem_installer.rb:54:in `install'
/var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/installer/gem_installer.rb:16:in
`install_from_spec'
/var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/installer/parallel_installer.rb:132:in
`do_install'
/var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/installer/parallel_installer.rb:123:in `block
in worker_pool'
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/worker.rb:62:in `apply_func'
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/worker.rb:57:in `block in process_queue'
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/worker.rb:54:in `loop'
  /var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/worker.rb:54:in `process_queue'
/var/lib/gems/3.0.0/gems/bundler-2.5.16/lib/bundler/worker.rb:90:in `block (2 levels) in
create_threads'

An error occurred while installing public_suffix (6.0.1), and Bundler cannot continue.

In Gemfile:
  capybara was resolved to 3.40.0, which depends on
    addressable was resolved to 2.8.7, which depends on
      public_suffix
         run  bundle lock --add-platform=x86_64-linux
Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies...
Writing lockfile to /home/kuro/lab/rails-lesson/src/day2/myWebApp/pdiary/Gemfile.lock
         run  bundle binstubs bundler
Could not find turbo-rails-2.0.6, selenium-webdriver-4.23.0, rexml-3.3.2, public_suffix-6.0.1 in
locally installed gems
```

</details>

# 原因

エラーメッセージ抜粋

```
Retrying download gem from https://rubygems.org/ due to error (2/4): Bundler::PermissionError There was an error while trying to write to `/var/lib/gems/3.0.0/cache/public_suffix-6.0.1.gem`. It is likely that you need to grant write permissions for that path.
```

#### 意味
- /var/lib/gems/3.0.0に書き込み権限がないよ～


# 解決策

当該フォルダの書き込み権限付与

```bash
sudo chown -R $USER /var/lib/gems/3.0.0/
```

# P.S.
bundleがなかったら以下のコマンドでインストールすること。

```bash
sudo gem install bundler
```