気ままに気ままのエンジニアブログ

定期的に得た知見を気ままに発信中

【Rails/環境構築(M1)】Could not find a JavaScript runtime. (ExecJS::RuntimeUnavailable)エラーの対処方法

こんにちは。

最近SQLにハマってます。

どうもハチマキです。

はじめに

最近MacBook Air(M1,2020)を購入したため、開発環境の構築を行いました。その時に発生したエラーの対処方法についてです。

早速結論になりますが、この記事で対象にしているエラーExecJS::RuntimeUnavailableですが、下記記載した解消方法では根本的に解消できませんでした(サーバ起動時にエラーが発生する)

ですので、もしこの記事で書かれた内容で解消できない場合は、こちらの記事と合わせて参考にして頂けばと思います。
【Rails/環境構築(M1)】ローカルサーバ起動後にアクセスすると「dyld: lazy symbol binding failed: Symbol not found」エラーが発生し、サーバが落ちる時の対処方法 - めがね屋のエンジニアブログ

環境

問題

rspecのテストコマンドを実行すると、JS関連でエラーが発生する。

% bundle exec rspec

An error occurred while loading ./spec/features/~.rb
Failure/Error: require File.expand_path('../config/environment', __dir__)

ExecJS::RuntimeUnavailable:
  Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
# ./vendor/ruby/2.6.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect'
# ./vendor/ruby/2.6.0/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'
# ./vendor/ruby/2.6.0/gems/execjs-2.7.0/lib/execjs.rb:4:in `<main>'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
# ./vendor/ruby/2.6.0/gems/uglifier-4.2.0/lib/uglifier.rb:5:in `<main>'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
# ./config/application.rb:7:in `<top (required)>'
# ./config/environment.rb:2:in `require_relative'
# ./config/environment.rb:2:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `require'
# ./spec/rails_helper.rb:5:in `<top (required)>'
# ./spec/features/~.rb:1:in `require'
# ./spec/features/~.rb:1:in `<top (required)>'
No examples found.


Finished in 0.00004 seconds (files took 3.01 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

原因

Javascriptを実行するために必要なランタイム(ソフトウェアの実行に必要なプログラム)がないことでエラーが発生

解決方法

必要なランタイムをインストールすれば解消です。
rails newで生成されたGemfileのmini_racerコメントアウトを外し、bundle installを行うことで解消できます。

Gemfileファイル

#gem 'mini_racer', platforms: :ruby #コメントアウト外す
gem 'mini_racer', platforms: :ruby

ターミナル(コメントアウト外した後に)

% bundle install

*追記(20210317)
結局この対応方法では解消できませんでした。
解消するためにやったことを簡潔に申しますと、mini_racerの代わりとなる必要なランタイムをインストールする対応です。

▼こちらの記事も合わせて、ご参考にしてください。
【Rails/環境構築(M1)】ローカルサーバ起動後にアクセスすると「dyld: lazy symbol binding failed: Symbol not found」エラーが発生し、サーバが落ちる時の対処方法 - めがね屋のエンジニアブログ

【Rails/環境構築(M1)】Could not find Firefox binary (os=macosx)エラー解消方法

こんにちは。

春風邪でしょうか。急に体調が悪くなりました、。

どうもハチマキです。

はじめに

最近MacBook Air(M1,2020)を購入したため、開発環境の構築を行いました。
環境構築にいくつかはまった箇所がありましたが、今回はコマンド一つで解消できます。
忘れないようメモしていきます。

環境

やりたいこと

発生したCould not find Firefox binary (os=macosx)エラーを解消したい

問題

rspecのテストコマンドを実行すると、下記エラーが発生する。
▽エラー事例

 Selenium::WebDriver::Error::WebDriverError:
       Could not find Firefox binary (os=macosx). Make sure Firefox is installed or set the path manually with Selenium::WebDriver::Firefox::Binary.path=

解決方法

カスタムアプリパスを指定し、Firefoxをインストールすることで解決にいたりました。
※設定変更することで、chromeなどでも対応可

% brew  install --appdir="/Applications" firefox 

==> Downloading https://download-installer.cdn.mozilla.net/pub/firefox/releases/85.0.1/mac/
######################################################################## 100.0%
==> Installing Cask firefox
==> Moving App 'Firefox.app' to '/Applications/Firefox.app'
🍺  firefox was successfully installed!

参考資料

RSpecでCould not find Firefox binary (os=macosx)というエラーが発生した件 - Qiita

                                            • -

日々勉強です。
以上、ハチマキでした。

【Rails/環境構築(M1)】ローカルサーバ起動後にアクセスすると「dyld: lazy symbol binding failed: Symbol not found」エラーが発生し、サーバが落ちる時の対処方法

こんにちは。

天気がよくて気分が良い!花粉がなければ。。

どうもハチマキです。

はじめに

最近MacBook Air(M1,2020)を購入したため、開発環境の構築を行いました。その時に発生したエラーの対処方法についてです。

今回のエラーは結構強敵で、エラー解消に数日費やしました。。そのため、解消方法について書いていきたいと思います。

環境

やりたいこと

サーバを正常に動かしたい

問題

ローカルサーバ起動後にアクセスすると「dyld: lazy symbol binding failed: Symbol not found」エラーが発生し、サーバが落ちる。。

原因

今回の原因は、環境構築時に発生した (ExecJS::RuntimeUnavailable)を解消するためにインストールしたmini_racerが原因でサーバが落ちていました。

エラーログ
% rails s            
=> Booting Puma
=> Rails 5.2.4.5 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.6 (ruby 2.6.4-p104), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
I, [2021-02-13T22:36:34.461153 #83088]  INFO -- : Started GET "/" for ::1 at 2021-02-13 
・
・
dyld: lazy symbol binding failed: Symbol not found: __ZN2v82V813InitializeICUEPKc
  Referenced from: /Users/ユーザ名/development_of_app//vendor/bundle/ruby/2.6.0/gems/mini_racer-0.3.1/lib/mini_racer_extension.bundle
  Expected in: flat namespace

dyld: Symbol not found: __ZN2v82V813InitializeICUEPKc
  Referenced from: /Users/ユーザ名/development_of_app/vendor/bundle/ruby/2.6.0/gems/mini_racer-0.3.1/lib/mini_racer_extension.bundle
  Expected in: flat namespace

zsh: abort      rails s

解消方法

早速ですが、解決方法です。
インストールしたmini_racersをアンインストールし、その状態でbundle installするとExecJS::RuntimeUnavailableエラーが発生するので、 代わりのjs実行エンジンをインストールする必要があります。

ステップ1:インストールしたmini_racerを元に戻す

Gemfileファイル

#gem 'mini_racer', platforms: :ruby #コメントアウトを戻す
ステップ2:libv8をインストールする(環境に応じて必要なv8を提供してくれる)
 %  gem install libv8 -v '3.16.14.19' -- --with-system-v8
Building native extensions with: '--with-system-v8'
This could take a while...
Successfully installed libv8-3.16.14.19
Parsing documentation for libv8-3.16.14.19
Installing ri documentation for libv8-3.16.14.19
Done installing documentation for libv8 after 0 seconds
1 gem installed
ステップ3:v8をbundlerが読むように設定する
$ bundle config build.libv8 --with-system-v8
ステップ4:v8をインストールする(jsの実行エンジン)
% brew install v8
==> Downloading https://homebrew.bintray.com/bottles/v8-8.8.278.15.arm64_big_sur.bottle.tar
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/70e65168b14df8f2e40598467ae15c50
######################################################################## 100.0%
==> Pouring v8-8.8.278.15.arm64_big_sur.bottle.tar.gz
🍺  /opt/homebrew/Cellar/v8/8.8.278.15: 80 files, 39.9MB
Removing: /Users/ユーザ名/Library/Caches/Homebrew/v8--8.8.278.14.arm64_big_sur.bottle.tar.gz... (12.6MB)

ここまでたどり着くのが長かった。。

最後にbundle installしてからサーバを起動したところ、正常にサーバが起動しました。
お疲れ様でした。

【Rails/環境構築(M1)】Unknown command: caskのエラー解消方法

こんにちは。

最近進撃の巨人にハマり、ひとまずアニメは追い付きました。
日曜深夜が待ち遠しい。。

どうもハチマキです。

はじめに

最近MacBook Air(M1,2020)を購入したため、開発環境の構築を行いました。
caskを使用して、firefoxをインストールする際にError: Unknown command: caskが発生。
解消するためのインストールコマンドを書いていきたいと思います。

そもそもcaskとは?

Homebrew-CaskはHomebrewを拡張し、その優雅さ、シンプルさ、スピードをmacOSアプリケーションや大きなバイナリにもたらします。
あなたのシェルで1行だけで、5476人のコントリビューターによって管理されている3960個のCasksに到達します。

※公式ページ参照

homebrew-caskとは?

homebrewの拡張で、MacGUIアプリケーションをコマンド一発で管理してしまう優れものです

環境

問題

とにかくcaskコマンドを実行してもUnknown command: caskが返ってくる

% brew cask install --appdir="/Applications" firefox
Error: Unknown command: cask
% brew install caskroom/cask/brew-cask
Error: caskroom/cask was moved. Tap homebrew/cask instead.
% brew tap caskroom/versions
Error: caskroom/versions was moved. Tap homebrew/cask-versions instead.
% brew caskw
Error: Unknown command: caskw
% brew cask
Error: Unknown command: cask

原因

そもそもインストールしてなかった。。

解決方法

インストールコマンドを実行することで、caskがインストールされ解消に至りました。。確認不足です。

% brew install cask

参考資料

homebrew-caskとは - Qiita
homebrew-caskって何??? - Qiita

                                            • -

日々勉強です。
以上、ハチマキでした。

【Rails/環境構築(M1)】BUILD FAILED (macOS 11.1 using ruby-build 20210119)のエラー解消方法

こんにちは。

最近は運動不足解消のため、一駅前で降りて歩いて帰宅しております。

どうもハチマキです。

はじめに

最近MacBook Air(M1,2020)を購入したため、開発環境の構築を行いました。
今回rbenvでRuby2.6.4をインストールする際に発生したエラーの対処方法について書いていきます。

環境

やりたいこと

Ruby2.6.4にバージョン変更したい

問題

Ruby2.6.4をインストーするためにrbenv install 2.6.4を実行したが、初めて見たエラーに出くわしました。
▽エラー事例

% rbenv install 2.6.4
Downloading openssl-1.1.1i.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/e8be6a35fe41d10603c3cc635e93289ed00bf34b79671a3a4de64fcee00d5242
Installing openssl-1.1.1i...
Installed openssl-1.1.1i to /Users/user_name/.rbenv/versions/2.6.4

Downloading ruby-2.6.4.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.4.tar.bz2
Installing ruby-2.6.4...
ruby-build: using readline from homebrew

BUILD FAILED (macOS 11.1 using ruby-build 20210119)

Inspect or clean up the working tree at /var/folders/hq/9_xgdx314vjfbz6wnqc7jx280000gn/T/ruby-build.20210206115130.11119.X89in3
Results logged to /var/folders/hq/9_xgdx314vjfbz6wnqc7jx280000gn/T/ruby-build.20210206115130.11119.log

Last 10 log lines:
compiling ../.././ext/psych/yaml/dumper.c
linking shared-object -test-/string.bundle
compiling ../.././ext/psych/yaml/reader.c
compiling ../.././ext/psych/yaml/emitter.c
compiling ../.././ext/psych/yaml/parser.c
5 warnings generated.
linking shared-object zlib.bundle
1 warning generated.
linking shared-object psych.bundle
make: *** [build-ext] Error 2

解決方法

下記を実行することで無事に解消されました。(原因は正直いまいちわかっておりません。。)

% RUBY_CFLAGS=-DUSE_FFI_CLOSURE_ALLOC rbenv install 2.6.4
Downloading openssl-1.1.1i.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/e8be6a35fe41d10603c3cc635e93289ed00bf34b79671a3a4de64fcee00d5242
Installing openssl-1.1.1i...
Installed openssl-1.1.1i to /Users/user_name/.rbenv/versions/2.6.4

Downloading ruby-2.6.4.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.4.tar.bz2
Installing ruby-2.6.4...
ruby-build: using readline from homebrew
Installed ruby-2.6.4 to /Users/user_name/.rbenv/versions/2.6.4


% ruby -v          
ruby 2.6.4p62 (2019-04-16 revision 67580) [universal.arm64e-darwin20]
yutakayamaki@YAMAKInoMacBook-Air yy-sprout % rbenv local 2.6.4
yutakayamaki@YAMAKInoMacBook-Air yy-sprout % rbenv rehash
yutakayamaki@YAMAKInoMacBook-Air yy-sprout % rbenv local 2.6.4
yutakayamaki@YAMAKInoMacBook-Air yy-sprout % ruby -v          
ruby 2.6.4p104 (2019-08-28 revision 67798) [-darwin20]

その後、Ruby2.6.4に変更すれば完了です。

% ruby -v 
ruby 2.2.4
% rbenv local 2.6.4
% rbenv rehash
% ruby -v          
ruby 2.6.4p104 (2019-08-28 revision 67798) [-darwin20]

参考資料

M1 の MacBook で rbenv + Ruby 2.6.6 をビルドする - Secret Garden(Instrumental)

                                            • -

日々勉強です。
以上、ハチマキでした。

【Rails/環境構築(M1)】bundle installで発生した`gem install ffi -v '1.12.2' --sourceエラーの対処方法

こんにちは。

最近はリモートが増えたので、腰への負荷が抑えられるいい椅子が欲しい。

どうもハチマキです。

はじめに

最近MacBook Air(M1,2020)を購入したため、開発環境の構築を行いました。その時に発生したエラーの対処方法について書いていきます。

補足ですが、MacBook Air(M1,2020) の環境構築の記事をちょくちょく見ましたが、intel時と比較すると初めて出くわすエラーが多々あるみたいです。

環境

やりたいこと

bundle installを問題なく通したい

問題

bundle installを実行したことで、下記のようなエラーが発生した。
言われた通りにgem install ffi -v '1.12.2'のinstallを試みるも今後は別のエラーが発生

 % bundle install --path vendor/bundler
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Ignoring byebug-11.1.3 because its extensions are not built. Try: gem pristine byebug --version 11.1.3
Ignoring byebug-10.0.2 because its extensions are not built. Try: gem pristine byebug --version 10.0.2
Ignoring puma-3.12.6 because its extensions are not built. Try: gem pristine puma --version 3.12.6
Ignoring sassc-2.4.0 because its extensions are not built. Try: gem pristine sassc --version 2.4.0
Ignoring sqlite3-1.4.2 because its extensions are not built. Try: gem pristine sqlite3 --version 1.4.2
Fetching gem metadata from https://rubygems.org/..........
Using rake 13.0.1
Using concurrent-ruby 1.1.7
Using i18n 1.5.1
・
・
Using bootsnap 1.4.5
Fetching ffi 1.12.2
Installing ffi 1.12.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
・
・
1 error generated.
make: *** [Function.o] Error 1

make failed, exit code 2

Gem files will remain installed in
/Users/user_name/development_of_app/app-hoge/vendor/ruby/2.6.0/gems/ffi-1.12.2 for
inspection.
Results logged to
/Users/user_name/development_of_app/app-hoge/vendor/ruby/2.6.0/extensions/-darwin-20/2.6.0/ffi-1.12.2/gem_make.out

An error occurred while installing ffi (1.12.2), and Bundler cannot continue.
Make sure that `gem install ffi -v '1.12.2' --source 'https://rubygems.org/'` succeeds
before bundling.

In Gemfile:
  bootstrap-sass was resolved to 3.4.1, which depends on
    sassc was resolved to 2.4.0, which depends on
      ffi

言われた通りにgem install ffi -v '1.12.2'をinstallを試みる

% sudo gem install ffi -v '1.12.2'
Building native extensions. This could take a while...
ERROR:  Error installing ffi:
	ERROR: Failed to build gem native extension.
・
・
make failed, exit code 2

Gem files will remain installed in /Users/user_name/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/ffi-1.12.2 for inspection.
Results logged to /Users/user_name/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/extensions/-darwin-20/2.6.0/ffi-1.12.2/gem_make.out

原因

今回git cloneしてから環境構築を行ったこともあり、おそらくすでにあったGemfile.lockを見にいっていた

解決方法

Gemfile.lockを一度削除し、再度bundle installを実行することで解消しました。

% rm Gemfile.lock #削除

% bundle install --path vendor/bundler  
[DEPRECATED] The `--path` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set --local path 'vendor/bundler'`, and stop using this flag
Fetching gem metadata from https://rubygems.org/..........
Using rake 13.0.3

・・
・・
・
Bundle complete! 39 Gemfile dependencies, 144 gems now installed.
Bundled gems are installed into `./vendler/bundler`
Post-install message from sass:

Ruby Sass has reached end-of-life and should no longer be used.

* If you use Sass as a command-line tool, we recommend using Dart Sass, the new
  primary implementation: https://sass-lang.com/install

* If you use Sass as a plug-in for a Ruby web framework, we recommend using the
  sassc gem: https://github.com/sass/sassc-ruby#readme

* For more details, please refer to the Sass blog:
  https://sass-lang.com/blog/posts/7828841

参考資料

Ruby - ffi gemをインストールしたい。|teratail

                                            • -

日々勉強です。
以上、ハチマキでした。

【Rails/環境構築(M1)】HomebrewインストールでInstallation successful!になったのにbrewコマンドがエラーになるときの対処方法

こんにちは。

最近徐々に天敵を感じるようになってきました。そう花粉です。

どうもハチマキです。

はじめに

最近MacBook Air(M1,2020)を購入したため、開発環境の構築を行いました。その時に発生したエラーの対処方法について書いていきます。

環境

やりたいこと

Homebrewをインストールして、brewコマンドを使えるようにしたい

問題

Homebrewからインストールコマンドをコピペして、ターミナルで実行しました。
Installation successful!と無事にインストールできたと思いきや、brewコマンドを実行するとzsh: command not found: brewとコマンドが見つからない。。

% /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Password:
==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew
/opt/homebrew/share/man/man1/brew.1
/opt/homebrew/share/zsh/site-functions/_brew
/opt/homebrew/etc/bash_completion.d/brew
/opt/homebrew

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /usr/sbin/chown -R user-name:admin /opt/homebrew
==> Downloading and installing Homebrew...
HEAD is now at 0d1aa33f8 Merge pull request #10525 from samford/livecheck-check-stable-before-head
Warning: /opt/homebrew/bin is not in your PATH.
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (or will be during this `install` run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Add Homebrew to your PATH in /Users/user-name/.zprofile:
    echo 'eval $(/opt/homebrew/bin/brew shellenv)' >> /Users/user-name/.zprofile
    eval $(/opt/homebrew/bin/brew shellenv)
- Run `brew help` to get started
- Further documentation: 
    https://docs.brew.sh

これで簡単にインストール完了!と思ったのもつかの間、% brew -vを実行しても下記のようにbrewコマンドが見つからない。。
今インストールしたのに?と思いながら調査を進めました。

% brew -v
 zsh: command not found: brew

原因

単純にPATHの通し忘れでした。
インストールコマンドを実行した際に、下記のようにNext steps:の記載がありましたね。。
ちゃんと読んでおけば解決できた内容でした、、

==> Next steps:

  • Add Homebrew to your PATH in /Users/user-name/.zprofile:

解決方法

下記を実行することで無事に解消されました。

% echo 'eval $(/opt/homebrew/bin/brew shellenv)' >> /Users/user-name/.zprofile 
% eval $(/opt/homebrew/bin/brew shellenv)

% brew helpなどを実行して、インストールが無事にできていることを確認してみましょう。

% brew help
Example usage:
  brew search [TEXT|/REGEX/]
  brew info [FORMULA...]
  brew install FORMULA...
  brew update
  brew upgrade [FORMULA...]
  brew uninstall FORMULA...
  brew list [FORMULA...]

Troubleshooting:
  brew config
  brew doctor
  brew install --verbose --debug FORMULA

Contributing:
  brew create [URL [--no-fetch]]
  brew edit [FORMULA...]

Further help:
  brew commands
  brew help [COMMAND]
  man brew
  https://docs.brew.sh