LoginSignup
4
5

More than 3 years have passed since last update.

【エラー】Ruby on Rails のoss(オープンソース)を使うために、環境構築 切り替え

Last updated at Posted at 2020-07-31

はじめに

OSSから知識をパク...、引き出しを増やす!!

独学で知識を広げるために、先人が開発したオープンソースから技術を学ぶことにしました。
そのため、ruby環境を切り替えなどが必要になりました。dockerで気軽に遊べるので、ローカル環境で直接環境を切り替えるのは、普段避けてきました。

しかしこの際、ローカル環境でやってしまおう!と思い試みました。
結果的に理解が深まり、自分のためになりました。

早速やっていく

まず、Railsで開発されているOSSの記事より、面白そうで、美味しそうなOSSをニヤニヤ眺め、獲物を定めます。

業務システム?
なんとも美味しそうだ

明細を出すシステム? おお、管理画面含めて面白そうかもしれない。
https://github.com/vteams/open-source-billing/
ただ単に明細を出すなら調べて出るが、UIまで学べるのは美味しい

君に決めた!!

$ git clone https://github.com/vteams/open-source-billing.git

相当なコード量なのでしょう。
ダウンロードに恐ろしく時間がかかります。

楽しみでならない。
ウハウハしながら待ちます。

ついに、ダウンロードできた!!

よし、db:createだ!!!

open-source-billing owner$ rake db:create
rake aborted!
LoadError: cannot load such file -- bundler/setup
/Users/owner/projects/oss/open-source-billing/config/boot.rb:3:in `<top (required)>'
/Users/owner/projects/oss/open-source-billing/config/application.rb:1:in `<top (required)>'
/Users/owner/projects/oss/open-source-billing/Rakefile:5:in `<top (required)>'
(See full trace by running task with --trace)

うぇーい
エラー 出ますやん。

LoadError: cannot load such file -- bundler/setup

ままま、 bundlerのバージョンが違うとかだろ。
わかってるよ。

$ bundle -v

これで、バージョン確認だ!!

open-source-billing owner$ bundle -v
Traceback (most recent call last):
    2: from /Users/owner/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'
    1: from /Users/owner/.rbenv/versions/2.5.1/lib/ruby/2.5.0/rubygems.rb:308:in `activate_bin_path'
/Users/owner/.rbenv/versions/2.5.1/lib/ruby/2.5.0/rubygems.rb:289:in `find_spec_for_exe': can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException)

ははーん、バージョン見せてくれませんか
なるほど。

じゃあ、Gemfile.lockから見てみよう

Gemfile.lock
BUNDLED WITH
   2.1.4

やっぱり、載ってますやーん
2.1.4を利用しているのね。

じゃあ、今自分のmacのbundlerは、、、、、

~ owner$ bundle -v
Bundler version 2.0.2

2.0.2を使っているので、だからエラーが起こっているのか。

あれ? 過去に2.1.4使ってなかったっけ?

$ gem list bundler

これで確認だ!!

~ owner$ gem list bundler

*** LOCAL GEMS ***

bundler (2.0.2, 1.3.0)
capistrano-bundler (1.6.0)

使ってないわ笑
思い過ごしか。

じゃあ、2.1.4をインストールしよう!!

bundlerをインストール!!!

~ owner$ gem install bundler -v 2.1.4

これでインストールだ!!

~ owner$ gem install bundler -v 2.1.4
ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied @ rb_sysopen - /Users/owner/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-2.1.4/CHANGELOG.md

なんだって。。。。

Permission denied

Permissionだから、許可されてねーのか。権限関連みたいだな

それより、

ERROR:  While executing gem ... (Errno::EACCES)

このエラーは、なんだろうか???
早速ググってみよう。

ERROR: While executing gem ... (Errno::EACCES)が出た時の解決法
先人の方、ありがとうございます!!!

どれどれ
うんうん、よくわからん。
この人が参考にした元の記事を見よう

$sudo chown -R (ユーザ名):staff /Users/(ユーザ名)/.rbenv

このコマンドをすればいいのね
ownerアカウントを使っているから

$sudo chown -R owner:staff /Users/owner/.rbenv

でいいのかな
早速実行だ

MacBook-Air:~ owner$ sudo chown -R owner:staff /Users/owner/.rbenv
MacBook-Air:~ owner$ gem install bundler -v 2.1.4


Successfully installed bundler-2.1.4
Parsing documentation for bundler-2.1.4
Done installing documentation for bundler after 4 seconds
1 gem installed

成功したやん!!
先人の方、ありがとおおおおう!!

LGTM押しておこう

じゃあ、もう一度 db:createだ!

$ rake db:create

実行!!

MacBook-Air:open-source-billing owner$ rake db:create
Your Ruby version is 2.5.1, but your Gemfile specified 2.7.1

あれま、rubyのバージョンも違うのか。
自分が使っているのが、2.5.1 で, このOSSは2.7.1を利用しているのか。

じゃあ、ruby 2.7.1をインストールだ!!!

とりあえず、どのバージョンをインストールできるか確認してみようか

$ rbenv install -l

で確認っと。

OwnernoMacBook-Air:~ owner$ rbenv install -l
2.5.8
2.6.6
2.7.1
jruby-9.2.11.1
maglev-1.0.0
mruby-2.1.0
rbx-4.15
truffleruby-20.1.0

Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all' to show all local versions.

しっかり、2.7.1ありますね

じゃあ、2.7.1を指定して、インストールっと。

$ rbenv install 2.7.1

なかなかに長い。。。。

OwnernoMacBook-Air:~ owner$ rbenv install 2.7.1
Downloading openssl-1.1.1g.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/ddb04774f1e32f0c49751e21b67216ac87852ceb056b75209af2443400636d46
Installing openssl-1.1.1g...
Installed openssl-1.1.1g to /Users/owner/.rbenv/versions/2.7.1

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

ようやく終わった!!!

念のためにリフレッシュして、再読み込みさせておきますかね

再読み込み
$rbenv rehash

あとは、最近はdockerを使っているので、
標準利用しているバージョンをossに合わせよう

rubyは2.7.1を基本的に利用
rbenv global 2.7.1

じゃあ、これで db:createやってみますかね

open-source-billing owner$ rake db:create

Could not find rake-10.5.0 in any of the sources
Run `bundle install` to install missing gems.

ほうほう、Run'bundle install'`と出てるから、

$ bundle install

すればいいのね

bundle install 実行!!

.
.
.
Installing libv8 3.16.14.19 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
.
.
.
An error occurred while installing libv8 (3.16.14.19), and Bundler cannot continue.
Make sure that `gem install libv8 -v '3.16.14.19' --source 'https://rubygems.org/'` succeeds before bundling.

え、、、、。
まだ、エラー出るの???

よくみたら二つエラーがあるみたい。
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
これからググって見よう

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
先人ありがとう><

どうも、このエラーの直前のgemでエラーの原因のようだ

Installing libv8 3.16.14.19 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

この場合だと
libv8が原因みたいだね

さっきのもう一つのエラーもlibv8だったような

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

あーー、やっぱりそうだね。

$ gem install libv8 -v '3.16.14.19' --source 'https://rubygems.org/'

上記のコマンドしろって命が書かれているから、まずはこっちからやってみよう。

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

変わらないかあT^T

どうもよくあるエラーっぽいなあ

In Gemfile:
  therubyracer was resolved to 0.12.3, which depends on
    libv8

うんうん、therubyracerが原因みたいだ

このサイトに同じ症状が載っていたので、実行してみよう

ジッッ香

$ bundle config --local build.libv8 --with-system-v8 You are replacing the current $ bundle config --local build.therubyracer --with-v8-dir=$(brew --prefix v8-315)

お、エラーも何もない

じゃあ、`bundle installしてみましょうかねええ

open-source-billing owner$ bundle install

ん??

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

今度は、rmagickのエラーですか。
もしかして、、、いろんなgemこれの繰り返しなんじゃ。。。。。

とにかくやってみよう

$gem install rmagick -v '2.15.4' --source 'https://rubygems.org/'
open-source-billing owner$ gem install rmagick -v '2.15.4' --source 'https://rubygems.org/'



Building native extensions. This could take a while...
ERROR:  Error installing rmagick:
    ERROR: Failed to build gem native extension.

    current directory: /Users/owner/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rmagick-2.15.4/ext/RMagick
/Users/owner/.rbenv/versions/2.7.1/bin/ruby -I /Users/owner/.rbenv/versions/2.7.1/lib/ruby/2.7.0 -r ./siteconf20200730-12130-sp4zhp.rb extconf.rb
checking for clang... yes
checking for Magick-config... no
checking for pkg-config... yes
checking for outdated ImageMagick version (<= 6.4.9)... no
checking for Ruby version >= 1.8.5... yes
checking for stdint.h... yes
checking for sys/types.h... yes
checking for wand/MagickWand.h... no

Can't install RMagick 2.15.4. Can't find MagickWand.h.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

おーーーー、、、、やはり出たか

んーーーーー。なんだ
よくわからん。

ひとまず
ERROR: Error installing rmagick:
で検索してみよう。

gem install rmagick でインストール失敗

先人ありがとうううう!!!

どれどれ、
うんうん、ダウンロードして、直接macに入れる...。
よし、却下だ。
絶対にコマンドでいけるでしょ。
だって、コマンドでもダウンロードできるのだから。

他に良い記事はないかなと、、、

gem install rmagickでchecking for Magick-config... noがでる解決方法

hecking for Magick-config... noが怪しい?
どれどれ

ERROR:  Error installing rmagick:
    ERROR: Failed to build gem native extension.

    current directory: /Users/owner/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rmagick-2.15.4/ext/RMagick
/Users/owner/.rbenv/versions/2.7.1/bin/ruby -I /Users/owner/.rbenv/versions/2.7.1/lib/ruby/2.7.0 -r ./siteconf20200730-12130-sp4zhp.rb extconf.rb
checking for clang... yes
checking for Magick-config... no
checking for pkg-config... yes
checking for outdated ImageMagick version (<= 6.4.9)... no
checking for Ruby version >= 1.8.5... yes
checking for stdint.h... yes
checking for sys/types.h... yes
checking for wand/MagickWand.h... no

あ、このエラーのyesは問題なくて、noに問題があるのか!!!
なるほど!!!

で、Magick-configImageMagickが怪しいと!!
なるほどねえええ!!

じゃあ、解決策を参考にさせていただきます。

sudo: yum: command not found

あ、使えないのか

command not foundのエラーの解消方法
何気なくコマンド使っていたけど、こういう違いがあったのか、勉強になりました。
しかも、この人おそらく同じエラーじゃないですか

$ brew install ImageMagick

これでやってみようかな

$ brew install ImageMagick
Error: imagemagick 7.0.10-18 is already installed
To upgrade to 7.0.10-24, run `brew upgrade imagemagick`.

うんうん、すでに入っているみたいですねえ。そうですよねえ、過去に使ったことありますもんねええ

$ brew upgrade imagemagick

ひとまずアップデートしてみます

Error: Your Xcode (10.3) is too outdated.
Please update to Xcode 11.5 (or delete it).
Xcode can be updated from the App Store.

Error: Could not find an SDK that supports macOS 10.15.
You may have have an outdated or incompatible Xcode.
Homebrew found the following SDKs in the Xcode install:
  10.14

なんだか、これ以上やると環境を破壊する気配がする。。。
いや、それはないな。

そもそも、xcodeってiOSのシュミレーター用に入れたアプリだよね

Xcode can be updated from the App Store.

あー、やっぱりそうだよねえ。

ということは、最新バージョンのXcodeにアップデートしなはれと言われているのか。
そういえば、macアップデートしてから、xcodeアップデートしてなかったもんなあ

ということで、Appp Storeから最新版のXcodeをインストール
その後に

$brew reinstall imagemagick@6

$open-source-billing owner$ gem install rmagick
Building native extensions. This could take a while...
Successfully installed rmagick-4.1.2
Parsing documentation for rmagick-4.1.2
Done installing documentation for rmagick after 1 seconds
1 gem installed

お、成功しました!!

これで、bundle installしてみると、、、

Fetching rmagick 2.15.4
Installing rmagick 2.15.4 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.



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

やっぱり、ダメですね

ん???

よくみるとエラーで要求されているのはrmagick (2.15.4)で、先ほど入れたrmagickはrmagick-4.1.2でしたから、バージョンが違うことでエラーが起こっているかもしれない。

Gemfile
gem 'rmagick', '4.1.2' 

に変更します。

これでいけるかなあ?とbundle install

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

therubyracerまた君かい(笑)
さっき解決したでしょう(笑)

therubyracerも今の環境に合わせて、バージョンを指定すればいけるかな?
と思いぐぐるとある記事に遭遇する

時代はmini_racerだった

ッッハ!!

gem: 'therubyracer'は削除!!

Gemfile
gem 'mini_racer'

代わりにmini_racer足しましたよ

これでどうだ!!

$ bundle install

Using slim-rails 3.1.0
Using stripe 1.56.0
Fetching tinymce-rails 4.3.13
Installing tinymce-rails 4.3.13
Fetching to_words 1.1.1
Installing to_words 1.1.1
Fetching trackstamps 0.0.9
Installing trackstamps 0.0.9
Fetching twitter-bootstrap-rails 2.1.9
Installing twitter-bootstrap-rails 2.1.9
Fetching uglifier 3.0.0
Installing uglifier 3.0.0
Fetching unix_utils 0.0.15
Installing unix_utils 0.0.15
Fetching whenever 0.9.7
Installing whenever 0.9.7
Fetching wicked_pdf 2.0.2
Installing wicked_pdf 2.0.2
Fetching wkhtmltopdf-binary 0.9.9.3
Installing wkhtmltopdf-binary 0.9.9.3
Fetching xlsx_writer 0.4.4
Installing xlsx_writer 0.4.4
Fetching yajl-ruby 1.3.1
Installing yajl-ruby 1.3.1 with native extensions
Bundle complete! 104 Gemfile dependencies, 230 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Post-install message from twitter-bootstrap-rails:
Important: You may need to add a javascript runtime to your Gemfile in order for bootstrap's LESS files to compile to CSS. 

**********************************************

ExecJS supports these runtimes:

therubyracer - Google V8 embedded within Ruby

therubyrhino - Mozilla Rhino embedded within JRuby

Node.js

Apple JavaScriptCore - Included with Mac OS X

Microsoft Windows Script Host (JScript)

**********************************************

いちよう、成功したみたいです。

ただ、

Important: You may need to add a javascript runtime to your Gemfile in order for bootstrap's LESS files to compile to CSS. 
#重要: bootstrapのLESSファイルをCSSにコンパイルするために、javascriptランタイムをGemfileに追加する必要があるかもしれません。

**********************************************

ExecJS supports these runtimes:

therubyracer - Google V8 embedded within Ruby

therubyrhino - Mozilla Rhino embedded within JRuby

Node.js

Apple JavaScriptCore - Included with Mac OS X

Microsoft Windows Script Host (JScript)

**********************************************

jsが使えるかどうか、確認する必要はあるみたいですね

ExecJS::RubyRacerRuntime is not supported. Please replace therubyracer with mini_racer in your Gemfile or use Node.js as ExecJS runtime.
#ExecJS::RubyRacerRuntimeはサポートされていません。Gemfileでtherubyracerをmini_racerに置き換えるか、ExecJSランタイムとしてNode.jsを使用してください。

モノによっては、上記のよ8うにtherubyracerをmini_racerに置き換えるように指示がある程ですし、問題ないでしょう。


$rails s

ひとまず、サーバー起動できるか確認してみます。


Change:
  >> #<#<Class:0x00007fe292f30580>:0x00007fe29a81d588>.call(template)
To:
  >> #<#<Class:0x00007fe292f30580>:0x00007fe29a81d588>.call(template, source)
 (called from <top (required)> at /Users/owner/projects/oss/open-source-billing/config/application.rb:8)
[DEPRECATION] This gem has been renamed to hashie-forbidden_attributes and will no longer be supported. Please switch to hashie-forbidden_attributes as soon as possible.

何かのgemがサポート終了しているので、hashie-forbidden_attributesに書き換えろと言われています。

怪しいのはこれ

gem 'hashie_rails', '0.0.4'

gem 'hashie_rails'のgithubをみてみます。

HashieRails
Moved to HashieForbiddenAttributes.

お!、やっぱり、hashie-forbidden_attributesに変わっていますね!!

gem 'hashie-forbidden_attributes'

これに、書き換えて、bundle installします。

$ rails s

これで起動できるかな????

$ rails s
DEPRECATION WARNING: Single arity template handlers are deprecated. Template handlers must
now accept two parameters, the view object and the source for the view object.
Change:
  >> #<#<Class:0x00007f9aa1b5e5f8>:0x00007f9aa1b574b0>.call(template)
To:
  >> #<#<Class:0x00007f9aa1b5e5f8>:0x00007f9aa1b574b0>.call(template, source)
 (called from <top (required)> at /Users/owner/projects/oss/open-source-billing/config/application.rb:8)
=> Booting Puma
=> Rails 6.0.2.2 application starting in development 
=> Run `rails server --help` for more startup options

ダメですねえ

DEPRECATION WARNING: Single arity template handlers are deprecated. Template handlers must
now accept two parameters, the view object and the source for the view object.

警告: シングルアリティのテンプレートハンドラは非推奨です。テンプレートハンドラは
ビューオブジェクトとビューオブジェクトのソースの2つのパラメータを受け付けるようになりました。
という意味みたいです。

とりあえず検索してみます。

【Rails6】DEPRECATION WARNING: Single arity template handlers are deprecated. Template handlers must now accept two parameters, the view object and the source for the view object.
出てきましたね

本当にありがたい。

いったん、試してみます

$ bundle update slim

これでダメなら、
https://github.com/rails/rails/issues/35505
の方法を試していきたいと思います。

$ rails s
DEPRECATION WARNING: Single arity template handlers are deprecated. Template handlers must
now accept two parameters, the view object and the source for the view object.
Change:
  >> #<#<Class:0x00007f8216263780>:0x00007f8211d3abe0>.call(template)
To:
  >> #<#<Class:0x00007f8216263780>:0x00007f8211d3abe0>.call(template, source)
 (called from <top (required)> at /Users/owner/projects/oss/open-source-billing/config/application.rb:8)
=> Booting Puma
=> Rails 6.0.2.2 application starting in development 
=> Run `rails server --help` for more startup options
Exiting

ダメですね

Gemfile
gem 'slim', '3.0.7'

これを削除してみます。

$ rails s
DEPRECATION WARNING: Single arity template handlers are deprecated. Template handlers must
now accept two parameters, the view object and the source for the view object.
Change:
  >> #<#<Class:0x00007fe53a1117b0>:0x00007fe53a132ff0>.call(template)
To:
  >> #<#<Class:0x00007fe53a1117b0>:0x00007fe53a132ff0>.call(template, source)
 (called from <top (required)> at /Users/owner/projects/oss/open-source-billing/config/application.rb:8)

ダメですね

じゃあ、この記事を参考にしていきます。

Gemfile
# gem 'jbuilder', '~> 2.0'
gem 'jbuilder', github: 'rails/jbuilder', branch: 'master'

これでやってみましょう
bundle updateするように指示があるので、先にしておきます。

$ rails s
$ rails s
=> Booting Puma
=> Rails 6.0.2.2 application starting in development 
=> Run `rails server --help` for more startup options
Exiting
Traceback (most recent call last):
    50: from bin/rails:4:in `<main>'
    49: from bin/rails:4:in `require'

先ほどのエラーはなくなりましたが、それでもまだ読み込めない様子です。

エラー
`database_configuration': Cannot load database configuration: (RuntimeError)
Could not load database configuration. No such file - ["config/database.yml"]

どうもconfigフォルダにdatabase.ymlが入っていないようです。

database.ymlを追加すれば改善するでしょう。
その前に

先ほど消したslimを戻して検証しておきましょう

Gemfile
gem: 'slim'

これで起動っと

$ rails s
=> Booting Puma
=> Rails 6.0.2.2 application starting in development 
=> Run `rails server --help` for more startup options
Exiting
Traceback (most recent call last):

うんうん、先ほどの

Change:
  >> #<#<Class:0x00007fe53a1117b0>:0x00007fe53a132ff0>.call(template)

がないことから、slimは関係なさそうです。
どうも,rails 6のjbuilderのバグのようですね

では続けてdatabase.ymlを作成していきます。

config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: 
  socket: /tmp/mysql.sock
  host: localhost

development:
  <<: *default
  database: open-source-billing_development

test:
  <<: *default
  database:  open-source-billing_test

これで、ひとまずrails sしてみます。

/Users/owner/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych.rb:577:in `initialize': No such file or directory @ rb_sysopen - /Users/owner/projects/oss/open-source-billing/config/config.yml (Errno::ENOENT)


ひとまずNo such file or directory @ rb_sysopenでググると記事が出てきました

何か記述が足りないようですね
今回の場合config/config.ymlがないため、発生しているようです。
ただ、config/config.copy.ymlというファイルが用意されていたので、copyの記述を消して、cofig.ymlとしました。

$ rails db:create
Created database 'open-source-billing_development'
Created database 'open-source-billing_test'

$rails db:migrate
ズガガガガガガガ.....
$rails s

Image from Gyazo

でたああああああああああああああああああああああああああ
ようやくですよおお

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5