前提としてRuby等はどれも最新バージョンを用いることとします。
ソフトウェア | バージョン |
rbenv | v1.5.1 |
Ruby | 3.3.5-1 |
Rails | 7.2.1.2 |
bundler | 2.5.22 |
流れ
1. PowerShellインストール
C:\Users\ユーザ名>winget search Microsoft.PowerShell 'msstore' ソースでは、使用する前に次の契約を表示する必要があります。 Terms of Transaction: https://aka.ms/microsoft-store-terms-of-transaction ソースが正常に機能するには、現在のマシンの 2 文字の地理的リージョンをバックエンド サービスに送信する必要があります (例: "US")。 すべてのソース契約条件に同意しますか? [Y] はい [N] いいえ: Y 名前 ID バージョン ソース ------------------------------------------------------------------ PowerShell Microsoft.PowerShell 7.4.6.0 winget PowerShell Preview Microsoft.PowerShell.Preview 7.5.0.5 winget C:\Users\ユーザ名>
2. rbenv for Windowsインストール
ここ
からFor common usersに従い、rbenvのインストールを行う
PS C:\Users\ユーザ名> $env:RBENV_ROOT = "C:\rbenv" PS C:\Users\ユーザ名> iwr -useb "https://github.com/RubyMetric/rbenv-for-windows/raw/main/tool/install.ps1" | iex ディレクトリ: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2024/10/26 11:24 rbenv Cloning into 'rbenv'... remote: Enumerating objects: 1658, done. remote: Counting objects: 100% (491/491), done. remote: Compressing objects: 100% (190/190), done. remote: Total 1658 (delta 294), reused 474 (delta 285), pack-reused 1167 (from 1) Receiving objects: 100% (1658/1658), 454.85 KiB | 6.23 MiB/s, done. Resolving deltas: 100% (1070/1070), done. Downloading pre-compiled binaries from GitHub... Finished Checking rbenv-binary-version.txt from GitHub... OK Update the local rbenv-binary-version.txt rbenv-installer: Installation complete! PS C:\Users\ユーザ名>
3. プロファイルに追記
PS C:\Users\ユーザ名> Test-Path $profile False PS C:\Users\ユーザ名>
Falseの場合
PS C:\Users\ユーザ名> New-Item -path $profile -type file -force ディレクトリ: C:\Users\ユーザ名\Documents\WindowsPowerShell Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2024/10/26 16:00 0 Microsoft.PowerShell_profile.ps1 PS C:\Users\ユーザ名>
Trueの場合
PS C:\Users\ユーザ名> Test-Path $profile True PS C:\Users\ユーザ名>
PS C:\Users\ユーザ名> notepad $profile 編集後にターミナル再起動して反映 ===このシステムではスクリプトの実行が無効になっているため、ファイル~~とか出た場合=== PS C:\Users\ユーザ名> Get-ExecutionPolicy Restricted PS C:\Users\ユーザ名> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser PS C:\Users\ユーザ名> Get-ExecutionPolicy RemoteSigned PS C:\Users\ユーザ名> ===
※次回起動時に以下のように自動DLが始まる
Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. 新機能と改善のために最新の PowerShell をインストールしてください!https://aka.ms/PSWindows Seems you have just installed rbenv, auto install MSYS2 for you MSYS2 is must-have if you want to install gems with C extensions Begin downloading ... => https://github.com/oneclick/rubyinstaller2/releases/download/RubyInstaller-3.3.5-1/rubyinstaller-devkit-3.3.5-1-x64.exe rubyinstaller-devkit-3.3.5-1-x64.exe (130.8 MB) [=============================================================] 100% Installing rubyinstaller-devkit-3.3.5-1-x64.exe(MSYS2) ... Moving the shared MSYS2 ... rbenv: Rehash all 13 executables in '3.3.5-1' rbenv: Remove version 3.3.5-1 registry info rbenv: The shared MSYS2 was installed successfully! rbenv: In addition, the newest version '3.3.5-1' was installed for you! rbenv: Change to global version '3.3.5-1' rbenv: Rehash all 13 executables in '3.3.5-1' パーソナル プロファイルとシステム プロファイルの読み込みにかかった時間は 70544 ミリ秒です。 PS C:\Users\ユーザ名>
4. Rubyとrbenvのバージョンを確認
PS C:\Users\ユーザ名> ruby -v ruby 3.3.5-1 (set by C:\rbenv\global.txt) PS C:\Users\ユーザ名>
rbenvからも確認できる
PS C:\Users\ユーザ名> rbenv version 3.3.5-1 (set by C:\rbenv\global.txt) PS C:\Users\ユーザ名>
- rbenv
PS C:\Users\ユーザ名> rbenv -v rbenv v1.5.1 PS C:\Users\ユーザ名>
5. SQLite3のインストール
SQLite Download Page
からPrecompiled Binaries for Windowsのsqlite-tools、sqlite-dllのzipをDLし、解凍して中身の
sqlite3.dll
と
sqlite3.exe
をC:\rbenv\バージョン\bin
以下に移動
6. Railsインストール
PS C:\Users\ユーザ名> gem install rails Fetching webrick-1.8.2.gem Fetching rackup-2.1.0.gem Fetching rack-3.1.8.gem Fetching thor-1.3.2.gem Fetching zeitwerk-2.7.1.gem Fetching concurrent-ruby-1.3.4.gem ... Done installing documentation for zeitwerk, thor, webrick, rack, rackup, concurrent-ruby, tzinfo, i18n, connection_pool, activesupport, useragent, nokogiri, crass, loofah, rails-html-sanitizer, rails-dom-testing, rack-test, rack-session, erubi, builder, actionview, actionpack, railties, marcel, activemodel, activerecord, globalid, activejob, activestorage, actiontext, mini_mime, mail, actionmailer, actionmailbox, websocket-extensions, websocket-driver, nio4r, actioncable, rails after 38 seconds 39 gems installed A new release of RubyGems is available: 3.5.16 → 3.5.22! Run `gem update --system 3.5.22` to update your installation. PS C:\Users\ユーザ名>
バージョン確認
PS C:\Users\ユーザ名> rails --version Rails 7.2.1.2
ここまでで一通りの環境構築は完了している
Windows特有のエラー等
1. インストールしただけだとrails g new等が使えない
すげえ致命的☹️
そのまま実行すると、以下のようなエラーが出る
PS C:\Users\ユーザ名\Desktop\work\csvtest\csvtest> rails new csvtest -G 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 ... Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: C:/rbenv/3.3.5-1/lib/ruby/gems/3.3.0/gems/psych-5.1.2/ext/psych C:/rbenv/3.3.5-1/bin/ruby.exe extconf.rb checking for pkg-config for yaml-0.1... not found checking for yaml.h... no yaml.h not found *** extconf.rb failed *** ... To see why this extension failed to compile, please check the mkmf.log which can be found here: C:/rbenv/3.3.5-1/lib/ruby/gems/3.3.0/extensions/x64-mingw-ucrt/3.3.0/psych-5.1.2/mkmf.log extconf failed, exit code 1 Gem files will remain installed in C:/rbenv/3.3.5-1/lib/ruby/gems/3.3.0/gems/psych-5.1.2 for inspection. Results logged to ... An error occurred while installing psych (5.1.2), and Bundler cannot continue. In Gemfile: debug was resolved to 1.9.2, which depends on irb was resolved to 1.14.1, which depends on rdoc was resolved to 6.7.0, which depends on psych run bundle lock --add-platform=x86_64-linux Fetching gem metadata from https://rubygems.org/.......... Resolving dependencies... Writing lockfile to C:/Users/ユーザ名/Desktop/work/csvtest/csvtest/csvtest/Gemfile.lock run bundle binstubs bundler Could not find importmap-rails-2.0.3, turbo-rails-2.0.11, stimulus-rails-1.3.4, debug-1.9.2, rubocop-rails-omakase-1.0.0, web-console-4.2.1, irb-1.14.1, rdoc-6.7.0 in locally installed gems rails importmap:install Could not find importmap-rails-2.0.3, turbo-rails-2.0.11, stimulus-rails-1.3.4, debug-1.9.2, rubocop-rails-omakase-1.0.0, web-console-4.2.1, irb-1.14.1, rdoc-6.7.0 in locally installed gems Run `bundle install` to install missing gems. rails turbo:install stimulus:install Could not find importmap-rails-2.0.3, turbo-rails-2.0.11, stimulus-rails-1.3.4, debug-1.9.2, rubocop-rails-omakase-1.0.0, web-console-4.2.1, irb-1.14.1, rdoc-6.7.0 in locally installed gems Run `bundle install` to install missing gems. PS C:\Users\ユーザ名\Desktop\work\csvtest\csvtest>
mkmf.logを見ろと書いてあるので確認すると、以下のメッセージがあった
pkg_config: checking for pkg-config for yaml-0.1... -------------------- not found
これに関してはまんまぴったりの解決策があった
bundle install の際 psychのインストールに失敗する 【Windows + Ruby3.2以降 + Rails7】 | RYOZEN - Creative Studio
上記リンクに従い、
C:\rbenv\バージョン\lib
に
libyaml.a
と
libyaml.dll.a
を配置すればOK
再度rails new すればうまくいく
2. bin/delayed_jobコマンドが使えない
PS C:\Users\ユーザ名\Desktop\work\csvtest> ruby bin/delayed_job start C:/rbenv/3.3.5-1/lib/ruby/gems/3.3.0/gems/daemons-1.4.1/lib/daemons/daemonize.rb:10:in `fork': fork() function is unimplemented on this machine (NotImplementedError) from C:/rbenv/3.3.5-1/lib/ruby/gems/3.3.0/gems/daemons-1.4.1/lib/daemons/daemonize.rb:10:in `safefork'
代わりに、以下コマンドを使う
rake jobs:work
3. Wheneverが使えない
Rake Task作成後に定期実行するやつ。crontabがWindowsにないのが悪い
代わりに、clockworkを使う
clockwork .\config\clock.rb
もしくは、Rake Taskを作成したうえでタスクスケジューラに登録する