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

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

【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

【Rails/Vue.js】'v-model.number'を定義したら「The input element's type ('number') does not support selection.」エラー発生時の対処方法

こんにちは。

最近布団が離してくれなくて、朝起きれません。

どうもハチマキです。

はじめに

Vue.jsでイベントハンドラ(v-model)を実装中、The input element's type ('number') does not support selection.のエラーが発生しました。
解決に少々苦戦したため、解決方法に関して書いていきたいと思います。

実現したいこと

  • AとBの合計金額を算出する

問題

  • 合計金額を算出する上で、'vue-model.number'をhtmlで定義したら、The input element's type ('number') does not support selection.が発生した

console.logのエラー確認

html

<input type="number" vue-model.number ="A"

ブラウザ
f:id:hachimaki37:20210126114951p:plain

調査

よく記載のあった解決方法は下記のような方法が確認できました。

  • input type="number"を定義する
  • number装飾子を追加する(vue-model.number)
  • JSファイルの対象の値を、parseInt()を指定して値を整数値で返す

これを試しましたが、なかなか解決できず苦戦しました。

解決方法

input typeを'text'に指定し、JS側で各値を整数値(parseInt())で返すことで解決に至りました。
htmlファイル

#total
 = ~~ do |i|
 
   %td= i.input :total, input_html: {type: 'text', 'vue-model' => 'A'}
   %td= i.input :total, input_html: {type: 'text', 'vue-model' => 'B'}

JSファイル(js.coffee)

init = -> 
 new Vue
 el: document.getElementById 'total'
 data:
  A: 0
  B: 0
 computed:
  total: ->
   return parseInt(A) + parseInt(B)  #parseInt(A + B)にするとABと文字列同士が演算されてしまう

これでエラー解決および演算の動作が正常になりました。

【Rails】バッチファイル(rake task)作成手順について

こんにちは。

家の作業場が外並みに寒い。。

どうもハチマキです。

はじめに

自動配信メール作成に伴い、バッチ処理を実装しました。
Railsバッチ処理を書く際によく使われる

  • rails runner
  • rake task
  • sidekiq

の中でも、今回はrake taskのビルドタスクバッチ手順について書いていこうと思います。(自分用のメモに近い内容になります)

では早速行きましょう!

本日の概要 : バッチファイル(rake task)作成手順について

TODO

  • taskの追加
  1. config/schedule.rbにバッチ処理を記述する
  2. lib/tasks/hoge_mail.rakeのファイルを作成し、送信処理を定義する
  3. lib/hoge_reminder/batch.rbファイルを作成し、送信条件を定義する
  • mailerの追加
  1. app/mailers/hoge_mailer.rbのmailer追加
  2. app/views/hoge_mailer/remind.html / text.hamlの追加
  • ymlファイルに追加
    • config/locales/ja.ymlに日本語名を追加
  • taskのテスト
    • ターミナルで実行しテストする

ステップ1:task追加

  • ゴール:ターミナルでbundle exec rake -Tコマンドを実行すると追加したタスクが表示される
ターミナル
$ bundle exec rake -T
....

rake hoge  # hogeの自動メール
rake piyo  # piyoの取り込み
1. config/schedule.rbにバッチ処理を記述する
#  hogeの自動メール(こちらのみ実装していきます)
every :day, at: '6:50 pm', roles: [:db] do
  rake 'yy:hoge_reminder:remind'
end if production?

# piyoの取り込み
every :day, at: '6:50 pm', roles: [:db] do
  rake 'yy:piyo_notifier:announce'
end if production?
2. lib/tasks/hoge_mail.rakeのファイルを作成し、送信処理を定義する
require Rails.root.join('lib/hoge_reminder/batch')

#namespaceを使用します
namespace :yy do
  namespace :hoge_reminder do
    desc 'Send hoge reminder'
    task remind: :environment do
      Hoge::Batch.new.run(dry_run: ENV['DRY_RUN'])
    end
  end
end 
3. lib/hoge_reminder/batch.rbファイルを作成し、送信条件を定義する
require 'batch_base'

module HogeReminder

class Batch < BatchBase
    def _run(*)
      target.each do |order|
        Hoge.remind(変数).deliver unless dry_run?
      end
    end

    def target
      変数 = 条件
      end
    end
  end
end 

ステップ2:mailerの追加

1. app/mailers/hoge_mailer.rbのmailer追加
class HogeMailer < ActionMailer::Base
  add_template_helper(ApplicationHelper)
  add_template_helper(ImageHelper)
  add_template_helper(ContentsHelper)
  add_template_helper(MailerHelper)
  add_template_helper(SearchHelper)

  layout 'hoge_mail'

  default from: Settings::MailAddress.noreply

  def remind(変数)
    subject = "#{t('hoge_mailer.remind_email.subject')}

    mail to: @user.email, subject: subject
  end
end
 
2. app/views/hoge_mailer/remind.html/ text.hamlの追加

text.html.haml

= "#{@user.name}さま"
\
リマインドメールです
\
= @user.id
の件につきましてご連絡させて頂きます。

ステップ3:ymlファイルに追加

config/locales/ja.ymlに日本名を追加
hoge_mailer:
    remind_email:
      subject: "リマインドメール"

ステップ4:taskのテスト

ターミナルで実行しテストする
$ bundle exec rake yy:hoge_reminder::remind
....

START
..
..

これでmailが飛ぶようになり、実装完了です。