m1 mac: rails6.1でbin/rails server が成功するまでの作業ログ

公式を見ながらインストールしていきます。

https://guides.rubyonrails.org/getting_started.html#installing-ruby

rubyのバージョン確認

~ takayuki$ ruby -v
ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.x86_64-darwin21]

下記をみるとバージョンは大丈夫
https://guides.rubyonrails.org/getting_started.html#installing-ruby

念の為gemのバージョン

~ takayuki$ gem --version
Ignoring ffi-1.15.0 because its extensions are not built. Try: gem pristine ffi --version 1.15.0
3.0.3.1

flutterというか、podsでも登場したffi。指示通りのコマンドを打ってみる。

~ takayuki$ gem pristine ffi --version 1.15.0
Ignoring ffi-1.15.0 because its extensions are not built. Try: gem pristine ffi --version 1.15.0
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

sudoつけ忘れ

~ takayuki$ sudo gem pristine ffi --version 1.15.0
Password:
Ignoring ffi-1.15.0 because its extensions are not built. Try: gem pristine ffi --version 1.15.0
Restoring gems to pristine condition...
Building native extensions. This could take a while...
Restored ffi-1.15.0
~ takayuki$ gem --version                    
3.0.3.1

今度は成功。ちなみにターミナルはrossetaで動かしてます。 ffiのissuesを見ても、まだrossetaがないと動かなそう

https://github.com/ffi/ffi/issues?q=is%3Aissue+is%3Aopen+m1

bundler

~ takayuki$ sudo gem install bundler
Password:
Fetching bundler-2.2.31.gem
Successfully installed bundler-2.2.31
Parsing documentation for bundler-2.2.31
Installing ri documentation for bundler-2.2.31
Done installing documentation for bundler after 2 seconds
1 gem installed

リファレンスはここ

https://bundler.io/v2.2/guides/rails.html

そして、rails newでエラー

/Library/Ruby/Gems/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/message_encryptor.rb:170:in `auth_data=': couldn't set additional authenticated data (OpenSSL::Cipher::CipherError)
    from /Library/Ruby/Gems/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/message_encryptor.rb:170:in `_encrypt'
    from /Library/Ruby/Gems/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/message_encryptor.rb:148:in `encrypt_and_sign'
    from /Library/Ruby/Gems/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/encrypted_file.rb:88:in `encrypt'
    from /Library/Ruby/Gems/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/encrypted_file.rb:61:in `write'
    from /Library/Ruby/Gems/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/encrypted_configuration.rb:29:in `write'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/generators/rails/credentials/credentials_generator.rb:30:in `add_credentials_file_silently'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/generators/rails/app/app_generator.rb:194:in `credentials'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/generators/app_base.rb:165:in `public_send'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/generators/app_base.rb:165:in `build'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/generators/rails/app/app_generator.rb:386:in `create_credentials'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/invocation.rb:134:in `block in invoke_all'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/invocation.rb:134:in `each'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/invocation.rb:134:in `map'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/invocation.rb:134:in `invoke_all'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/group.rb:232:in `dispatch'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/base.rb:485:in `start'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/commands/application/application_command.rb:26:in `perform'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor.rb:392:in `dispatch'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/command/base.rb:69:in `perform'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/command.rb:48:in `invoke'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/cli.rb:18:in `<top (required)>'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/exe/rails:10:in `<top (required)>'
    from /usr/bin/rails:22:in `load'
    from /usr/bin/rails:22:in `<main>'

https://github.com/ruby/openssl/blob/5c85b4385f114400d901ed7dd89ce43489b9bceb/ext/openssl/ossl_cipher.c#L577

https://github.com/openssl/openssl/blob/0f70d6013435308ada5d0eb662b31f370b07ebd7/crypto/evp/evp_enc.c#L401

ここまで深く読んでもよくわからないので、railsのバージョンを下げることに。

sudo gem uninstall rails
sudo gem uninstall railties -v 6.1.4.1

sudo gem install rails -v 6.0.4.1

でも6.0.4.1でもだめ。

rubyのバージョンを上げる。ためrvmをインストール。

https://rvm.io/

takayuki$ sudo port install gnupg2
Password:
Error: Current platform "darwin 21" does not match expected platform "darwin 20"
Error: If you upgraded your OS, please follow the migration instructions: https://trac.macports.org/wiki/Migration
OS platform mismatch
    while executing
"mportinit ui_options global_options global_variations"
Error: /opt/local/bin/port: Failed to initialize MacPorts, OS platform mismatch

MacPorts Migration

https://trac.macports.org/wiki/Migration

  • xcodeのバージョンはApp Storeで確認したので最新のはず
  • MacPortsのMonterey v12用のpkgをダウンロード。

Reinstall your ports

~ takayuki$ port -qv installed > myports.txt
~ takayuki$ port echo requested | cut -d ' ' -f 1 | uniq > requested.txt
~ takayuki$ sudo port -f uninstall installed
Password:
--->  Deactivating openjdk8 @8u292_0
--->  Cleaning openjdk8
--->  Uninstalling openjdk8 @8u292_0
--->  Cleaning openjdk8
~ takayuki$ sudo port reclaim
--->  Checking for unnecessary unrequested ports
Found no unrequested ports without requested dependents.
--->  Checking for inactive ports
Found no inactive ports.
--->  Building list of distfiles still in use
--->  Searching for unused distfiles
Found 1 files (total 98.98 MiB) that are no longer needed and can be deleted.
[l]ist/[d]elete/[K]eep: d
Deleting...
--->  Build location: /opt/local/var/macports/build
This appears to be the first time you have run 'port reclaim'. Would you like to be reminded to run it every two weeks? [Y/n]: n
Reminders disabled. Run 'port reclaim --enable-reminders' to enable.

openjdk8しかなかった。

ファイル移動

~ takayuki$ mkdir MigratingMacPorts 
~ takayuki$ mv myports.txt MigratingMacPorts 
~ takayuki$ mv requested.txt MigratingMacPorts 

restore

~ takayuki$ cd MigratingMacPorts 
~/MigratingMacPorts takayuki$ curl --location --remote-name https://github.com/macports/macports-contrib/raw/master/restore_ports/restore_ports.tcl
chmod +x restore_ports.tcl
xattr -d com.apple.quarantine restore_ports.tcl
sudo ./restore_ports.tcl myports.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   164  100   164    0     0    518      0 --:--:-- --:--:-- --:--:--   532
100  9934  100  9934    0     0  11422      0 --:--:-- --:--:-- --:--:--     0
xattr: restore_ports.tcl: No such xattr: com.apple.quarantine
Password:
--->  Computing dependencies for openjdk8
--->  Dependencies to be installed: openjdk8-zulu
--->  Fetching archive for openjdk8-zulu
--->  Attempting to fetch openjdk8-zulu-8.58.0.13_0.darwin_21.arm64.tbz2 from https://packages.macports.org/openjdk8-zulu
--->  Attempting to fetch openjdk8-zulu-8.58.0.13_0.darwin_21.arm64.tbz2 from https://nue.de.packages.macports.org/openjdk8-zulu
--->  Attempting to fetch openjdk8-zulu-8.58.0.13_0.darwin_21.arm64.tbz2 from http://atl.us.packages.macports.org/openjdk8-zulu
--->  Fetching distfiles for openjdk8-zulu
--->  Attempting to fetch zulu8.58.0.13-ca-jdk8.0.312-macosx_aarch64.tar.gz from https://cdn.azul.com/zulu/bin/
--->  Verifying checksums for openjdk8-zulu
--->  Extracting openjdk8-zulu
--->  Configuring openjdk8-zulu
--->  Building openjdk8-zulu
--->  Staging openjdk8-zulu into destroot
Warning: openjdk8-zulu installs files outside the common directory structure.
--->  Installing openjdk8-zulu @8.58.0.13_0
--->  Activating openjdk8-zulu @8.58.0.13_0
--->  Cleaning openjdk8-zulu
--->  Fetching archive for openjdk8
--->  Attempting to fetch openjdk8-8u302_0.darwin_21.arm64.tbz2 from https://packages.macports.org/openjdk8
--->  Attempting to fetch openjdk8-8u302_0.darwin_21.arm64.tbz2 from https://nue.de.packages.macports.org/openjdk8
--->  Attempting to fetch openjdk8-8u302_0.darwin_21.arm64.tbz2 from http://atl.us.packages.macports.org/openjdk8
--->  Fetching distfiles for openjdk8
--->  Verifying checksums for openjdk8
--->  Extracting openjdk8
--->  Configuring openjdk8
--->  Building openjdk8
--->  Staging openjdk8 into destroot
--->  Installing openjdk8 @8u302_0
--->  Activating openjdk8 @8u302_0
--->  Cleaning openjdk8
sudo port unsetrequested installed
xargs sudo port setrequested < requested.txt

sudo port install gnupg2ログが長いので割愛

下記の手順に戻って、上から実行。bashのままでも実行できる
https://rvm.io/

インストール成功

~ takayuki$ rvm -v
rvm 1.29.12 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
~ takayuki$ rvm install 2.7.4
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/12.0/x86_64/ruby-2.7.4.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Installing requirements for osx.
Updating system - please wait
takayuki password required for 'port -dv selfupdate': 
Installing required packages: autoconf, automake, gdbm, libtool, libyaml, pkgconfig, openssl - please wait
There were package installation errors, make sure to read the log.
Error running 'requirements_osx_port_libs_install autoconf automake gdbm libtool libyaml pkgconfig openssl',
please read /Users/takayuki/.rvm/log/1637541408_ruby-2.7.4/package_install_autoconf_automake_gdbm_libtool_libyaml_pkgconfig_openssl.log
Requirements installation failed with status: 1.
Error: Failed to build openssl3: command execution failed
148 Error: See /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_devel_openssl3/openssl3/main.log for details.
149 Error: rev-upgrade failed: Error rebuilding openssl3
150 Error: Follow https://guide.macports.org/#project.tickets if you believe there is a bug.
5188 :error:build See /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_devel_openssl3/openssl3/main.log for details.

ログを見てもよくわからないので、今度はportでruby27をインストール

sudo port select --set ruby ruby27

ターミナル再起動

~ takayuki$ ruby -v
ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-darwin21]

改めてrailsインストール。古いバージョンがある場合は、ターミナルを再起動

~ takayuki$ rails -v
Rails 6.1.4.1

sqlite3関連でエラーが出たのでsudo port install sqlite3でインストールしたけど、 develも足りない。

portでrb-sqlite3というのがあるのでインストールしてみることに。 ruby18もインストールされたけど、それは無視して、再度bundle installするとsqlite3のgemのインストールに 成功した。

なので、bin/rails serverを実行すると...

/opt/local/lib/ruby2.7/gems/2.7.0/gems/webpacker-5.4.3/lib/webpacker/configuration.rb:103:in `rescue in load': Webpacker configuration file not found /Users/takayuki/github/rails6_1-practice/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_check_realpath_internal - /Users/takayuki/github/rails6_1-practice/config/webpacker.yml (RuntimeError)
~/github/rails6_1-practice takayuki$ rails webpacker:install
Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/
Exiting!
~/github/rails6_1-practice takayuki$ npm install --global yarn

さらにrails webpacker:install。これでようやくbin/rails serverが成功しました。