1
0

More than 3 years have passed since last update.

Railsプロジェクトを作ろうとしたら躓いた人のメモ

Last updated at Posted at 2020-08-29

(筆者の現環境がMacなので一旦Mac編のみ。Windowsに関しては追々書くかも)

0. 結論

  • 何はともあれrbenvをインストール。話はそれから。

1. プロジェクト作成 (参考)

Terminalで
$ rails new {プロジェクト名}
を実行すると、Railsアプリの型となるファイルが生成されたプロジェクトディレクトリが作成される

2. 各種gemをインストール(gemとは

1.で作成したプロジェクトディレクトリに入り
$ cd ./{プロジェクト名}
↓のコマンドを実行
$ bundle install --path vendor/bundle

私はここでエラーが出た。
エラー文はこちら(エラー部分だけ抜粋)

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

nokogiriなるもののインストールでエラーが出ているらしい(nokogiriとは

3. エラー解決方法

  • 単純にgem install nokogiriしてみる

    • これもエラー You have to install development tools first.と出る
  • 開発者ツールが必要ということで、まずXcodeをAppStoreからインストールしてみる(結構時間かかる)

  • Xcodeをインストールしたら起動して、
    「Xcode」-> 「Preference」->「Location」で「Command Line Tools」を設定する
     (ここみると、Terminalからでもこの設定できるっぽい?)

  • Terminalを再起動して、改めてnokogiriをインストールしようとすると、、

    • またエラー
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /usr/bin directory.

/usr/binの書き込み権限が無いようだが、sudoでもchmodで権限変更しようとしても駄目

  • どうやらシステムのRubyやgemは使い勝手が悪そうなので、rbenv(rbenvとは)で環境を作った方が良さそう(そもそもこれを最初にやるべきだった感)

  • rbenvやそのインストーラーのbrewのインストール方法は ここ 参照

  • 既にプロジェクトディレクトリができている場合、Rubyのバージョンが設定されている筈なので、設定されたバージョンをrbenvでインストールする。(設定されたバージョンは{プロジェクト名}/.ruby-versionの中身を見て確認できる)

  • 三度目の正直、gem install nokogiri...

Fetching mini_portile2-2.4.0.gem
Fetching nokogiri-1.10.5.gem
Successfully installed mini_portile2-2.4.0
Building native extensions. This could take a while...
Successfully installed nokogiri-1.10.5
Parsing documentation for mini_portile2-2.4.0
Installing ri documentation for mini_portile2-2.4.0
Parsing documentation for nokogiri-1.10.5
Installing ri documentation for nokogiri-1.10.5
Done installing documentation for mini_portile2, nokogiri after 1 seconds
2 gems installed

できた!

4. エラー解消後、gemを再インストール

$ bundle install --path vendor/bundle
を再度実行

Bundle complete! 17 Gemfile dependencies, 74 gems now installed.
Bundled gems are installed into `./vendor/bundle`

のような画面が出たらインストール成功

5. まとめ

  • rbenvを使いましょう

【参考】
- https://qiita.com/___xxx_/items/b18795056e98243568fe

1
0
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
1
0