LoginSignup
0
1

More than 1 year has passed since last update.

はじめてのRuby on Railsの環境構築 まとめ

Last updated at Posted at 2021-10-09

1. はじめに

以前はDocker上で環境構築を行いましたが今回は前回できなかった自分のPCに環境構築できたのでメモ。

2. インストールする環境

PC: MacBookAir(Early 2015)
OS: BigSur 11.1
CPU:1.6 GHz デュアルコアIntel Core i5

3. Homebrewをインストール

Homebrewの公式サイトにコマンドが表示されていますが、今回はこちらのコマンドを入力します。

% /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

すると以下のような警告が出ますがダウンロードが進行します。
Warning: The Ruby Homebrew installer is now deprecated and has been rewritten in
Bash. Please migrate to the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

↑こちらが最新(2021/010/09時点)のコマンド

Updated 1 tap (homebrew/core).
==> Installation successful!

→Homebrewのインストールが完了

4. Rubyをインストール

% brew install ruby

Warning: ruby 3.0.2 is already installed and up-to-date.
To reinstall 3.0.2, run:
brew reinstall ruby

既にインストールされてますが再インストールします。

% brew reinstall ruby

==> Caveats
By default, binaries installed by gem will be placed into:
/usr/local/lib/ruby/gems/3.0.0/bin

デフォルトと違う場所になっているという警告

ruby-build installs a non-Homebrew OpenSSL for each Ruby version installed and these are never upgraded.

rubyがHomebrewを使用せずにインストールされているのでアップグレードがされないという警告

警告文
You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH, run:
  echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc

For compilers to find ruby you may need to set:
  export LDFLAGS="-L/usr/local/opt/ruby/lib"
  export CPPFLAGS="-I/usr/local/opt/ruby/include"

For pkg-config to find ruby you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"
ruby-build installs a non-Homebrew OpenSSL for each Ruby version installed and these are never upgraded.

To link Rubies to Homebrew's OpenSSL 1.1 (which is upgraded) add the following
to your ~/.zshrc:
  export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"

【対処法】 .zshrcファイルを編集

rubyをインストールした際に出ていた警告文の中にあったexport〜で始まる文を追記する

% echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc
% export LDFLAGS="-L/usr/local/opt/ruby/lib"
% export CPPFLAGS="-I/usr/local/opt/ruby/include"
% export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"
% export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"

または直接書き込みしてもOK
完成図
スクリーンショット 2021-10-09 11.02.00.png

→rubyのインストールが完了

5. Railsをインストール

% sudo gem install rails

Successfully installed rails-6.1.4.1
→Railsのインストールが完了

6. アプリを作成

作成したい場所に移動

% cd /Users/ユーザー名/アプリの保存したい場所
% rails new RailsApp(アプリ名)

エラーが発生…

rails aborted!
LoadError: cannot load such file -- nokogiri/nokogiri
/Library/Ruby/Gems/2.6.0/gems/nokogiri-1.12.5-arm64-darwin/lib/nokogiri/extension.rb:30:in rescue in [main]
/Library/Ruby/Gems/2.6.0/gems/nokogiri-1.12.5-arm64-darwin/lib/nokogiri/extension.rb:4:in [main]
/Library/Ruby/Gems/2.6.0/gems/nokogiri-1.12.5-arm64-darwin/lib/nokogiri.rb:11:in [main]

対処法

nokogiriファイルが読み込めてないようなので、不要なバージョンのnokogiriファイルを削除します。

作成したrailsアプリのフォルダを開き、Gemfile.Lockファイルを開く
私は念の為以前作成したDocker+Railsのフォルダを見比べながら編集
スクリーンショット 2021-10-09 11.50.12.png
今回は下記2つを削除

  • nokogiri (1.12.5-arm64-darwin)
    nokogiri1-12.5-arm64-darwin.png

  • nokogiri (~> 1.6)
    nokogiri1-6.png

2021年10月10日(追記)
nokogiriファイル整理後にrails serverすると以下のエラーが出ます
Webpacker configuration file not found

Webpackerが無いのでダウンロードします。

rails webpacker:install

参考させていただいたサイト
[Tips] Rails6起動時のError対応

もう一度トライ

% rails server

できました!
スクリーンショット 2021-10-09 11.41.04.png

0
1
1

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