見出し画像

macにrails6の環境構築

どうもこんばんは。

新しくmacを購入したので、rails(しかもversion6!)の環境構築をしました。
Windowsと違って簡単ですね。助かりました。

環境

macOS Mojave 10.14.6

homebrewのインストール

http://brew.sh/index_ja.htmlから以下のコマンドを貼り付け。

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

#インストール確認
$ brew -v

rbenvのインストール

$ brew install rbenv ruby-build

#インストール確認
$ rbenv --version

#.bash_profileの設定
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

# 設定の反映
$ source ~/.bash_profile

Rubyのインストール

#インストールできるバージョンの確認
$ rbenv install --list

#今回は2.5.3をインストール
$ rbenv install 2.5.3
$ rbenv global 2.5.3
$ rbenv rehash

#バージョン確認
$ ruby -v
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]

bundleのインストール

$ gem install bundler

#バージョン確認
$ bundle -v
Bundler version 2.0.2

Railsのインストール

#作業ディレクトリの作成
$ mkdir ~/workspace
$ cd workspace/

$ bundle init
$ vi Gemfile
→一番下にある以下の文字のコメントアウトを外す
gem "rails"

$ bundle install --path=vendor/bundle
$ bundle exec rails -v
Rails 6.0.0

アプリケーション作成起動

$ bundle exec rails new [アプリ名]
$ bundle exec rails s

いくつかエラーが出た。
まずは、Node.jsがなくて怒られたのでインストールする。

Node.jsのインストール

$ brew install nodebrew

$ nodebrew -v
nodebrew 1.0.1

#インストールできるバージョンを表示
$ nodebrew ls-remote

#今回は安定版をインストールする
$ mkdir -p ~/.nodebrew/src
$ nodebrew install-binary stable

#バージョン確認
$ nodebrew ls
v12.10.0

$ nodebrew use stable

#.bach_profileにパスを記載
$ echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profile
$ source ~/.bash_profile

#バージョン確認
$ node -v
v12.10.0

エラー対処

以下のようなエラーがでる。

$ bundle exec rails s
=> Booting Puma
=> Rails 6.0.0 application starting in development 
=> Run `rails server --help` for more startup options
RAILS_ENV=development environment is not defined in config/webpacker.yml, falling back to production environment
#上記のエラーの解決策が以下のコマンド・・・エラーになる
$ bundle exec rails webpacker:install
Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/

yarnのインストール

$ brew install yarn

webpackerのインストール

bundle exec rails webpacker:install

改めてアプリケーションの起動

$ bundle exec rails s

ブラウザからlocalhost:3000に接続
Yay! You're on Rails!が表示された!

この記事が気に入ったらサポートをしてみませんか?