LoginSignup
19
17

More than 3 years have passed since last update.

初心者がMacOSにRuby on Rails + PostgreSQL( + Fish Shell)の環境を構築してみた[2019/4/16]

Last updated at Posted at 2019-04-16

参考記事

環境

MacOS Mojave 10.14.3
Homebrew 2.1.1
Fish Shell 3.0.2(使っていなくても問題ありません)

大事なこと

  • この分野は急成長中であり常に変わっていきます。環境構築の際は記事の日付がなるべく新しいものを参考にしてください。
  • 出来る限り環境を合わせると良いでしょう。特にインストールするバージョンは記事と同じにすると上手くいくことが多いです。

手順

  1. Command Line Toolsをインストール
  2. Homebrewをインストール
  3. rbenvをインストール
  4. Rubyをインストール
  5. Bundlerをインストール
  6. Ruby on Railsをインストール
  7. PostgreSQLをインストール&設定
  8. Railsアプリの作成&設定

1~6まで

Ruby初学者のRuby On Rails 環境構築【Mac】 - Qiitaにて綺麗にまとめられているので引用させて頂きます。

Command Line Toolsのインストール

  1. Appleのdeveloperアカウントページにログイン
  2. メニューからDownloadsやDownload Toolsを探してダウンロードページへ
  3. See more downloadsからダウンロードリストページへ
  4. OSに対応したCommand Line Toolsを探してダウンロード
  5. ダウンロードされた.dmgファイルをインストール

Homebrewのインストール

Homebrewがインストールされているか確認

$ brew -v
Homebrew 1.4.2
Homebrew/homebrew-core (git revision a678; last commit 2018-01-02)

インストールされていたら、、、アップデートする

$ brew update

インストールされていなければ、、、Homebrew macOS 用パッケージマネージャーのサイトでの手順に従ってコマンドをターミナルにコピペしてインストール

rbenvのインストール

rubyのバージョン管理ができるようにrbenvをHomebrewでインストール
rbenvがインストールされているか確認

$ rbenv -v
rbenv 1.1.1

インストールされていたら、、、アップデートする

$ brew upgrade rbenv

インストールされていなければ、、、下記コマンドでインストール

rbenvのインストール
$ brew install rbenv ruby-build
.bash_profileの設定
$ echo 'export PATH="~/.rbenv/shims:/usr/local/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile

Fish Shellを使っている場合

上記の「.bash_profileの設定」の手順が変わります。

  1. ~/.config/fish/config.fishを開く(無い場合は新規作成する)
  2. status --is-interactive; and source (rbenv init -|psub)を追記する

ターミナルとしてFish Shellを使う際のPATH設定 - Qiitaを参考にして貰えると分かりやすいです。

Ruby2.5.0のインストール

インストール可能なRubyのバージョンを下記コマンドで確認

$ rbenv install --list

今回は2017年12月にリリースされた2.5.0をインストールする

Ruby2.5.0のインストール
$ rbenv install 2.5.0
$ rbenv global 2.5.0
$ rbenv rehash
$ ruby -v

インストール後に環境全体に有効なバージョンに2.5.0を指定しておき、バージョンが反映されているか確認する

Bundlerのインストール

Bundlerがインストールされているか確認

$ bundle -v
Bundler version 1.16.1

インストールされていたら、、、アップデートする

$ gem update bundler

インストールされていなければ、、、下記コマンドでインストールしてバージョンの確認

Bundlerのインストール
$ gem install bundler
$ bundle -v

Railsのインストール

Railsをインストールし作業するディレクトリを作成する

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

Rubyのバージョンを指定する(今後のバージョン切り替えを考慮して念のため)

Rubyのバージョン指定
$ rbenv local 2.5.0

作業ディレクトリに.ruby-versionファイルが作成される

bundle initでGemfileを作成する
$ bundle init

Gemfileを編集し# gem "rails"をコメント解除する

Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "rails"

Railsをインストールしてバージョン確認

Railsインストール
$ bundle install --path=vendor/bundle
$ bundle exec rails -v
Rails 5.1.4

最後は$ bundle install --path vendor/bundleの方が良さそうです。

以上でRailsを使う為の初期設定は終わりです。
改めて、引用させて頂いた@TAByasu様にお礼申し上げます。

7. PostgreSQLのインストール&設定

PostgreSQLのインストール
$ brew install postgresql
インストール出来ているかの確認
$ psql --version
psql (PostgreSQL) 11.2
初期設定
$ initdb /usr/local/var/postgres/ -E utf8
The files belonging to this database system will be owned by user "ユーザー名".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".
起動チェック
$ brew services start postgresql
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 17 (delta 0), reused 12 (delta 0), pack-reused 0
Unpacking objects: 100% (17/17), done.
Tapped 1 command (50 files, 62.5KB).
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

こうなればOKです。

DBの置き場所を環境変数に設定(bashの場合)
export PGDATA=/usr/local/var/postgres
DBの置き場所を環境変数に設定(fishの場合)
set -x PGDATA /usr/local/var/postgres

上記を~/.bash_profileまたは~/.config/fish/config.fishに追記してください。
その後、ターミナルの再起動を忘れずに。

PostgreSQLサーバーの起動
$ postgres -D /usr/local/var/postgres

サーバーが起動するので、そのウィンドウとは別のウィンドウを開いてください。

新しいユーザアカウントを追加
$ createuser -P 好きなユーザー名
Enter password for new role: 好きなパスワード
 Enter it again: 好きなパスワード(同上)

8. Railsアプリの作成&設定

Railsアプリの作成
$ bundle exec rails new 好きなアプリ名 -d postgresql

こうする事でPostgreSQLをRailsのDBに設定できます。

/config/database.yml
(前略)

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: 設定したアプリ名_development

(中略)

test:
  <<: *default
  database: 設定したアプリ名_test

(中略)

production:
  <<: *default
  database: 設定したアプリ名_production
  username: 設定したアプリ名
  password: <%= ENV['設定したアプリ名_DATABASE_PASSWORD'] %>

このようにアプリ内にデータベースの初期設定が行われます。

アプリ内に移動
$ cd 設定したアプリ名
データベースを作成
$ rails db:create

これで環境構築完了です!

19
17
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
19
17