1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails環境構築をやってみた【Rails5.2.2】

1
Posted at

前提条件

  • Homebrewインストール済み
  • rbenvインストール済み(本記事ではバージョン2.6.0のrubyを使用します)

1. Bundlerをインストール

以下のコマンドを実行

$ gem install bundler

Users/ユーザー名/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gemsに最新のbundlerがインストールされます。(rbenvでバージョン2.6.0をインストールした際、bundlerもインストールされているようなので、このコマンドが必須なのかは不明)

2. アプリディレクトリの作成・移動

今回はUsers/ユーザー名/Documents/workspacetest_appディレクトリを作成します。
その後以下のコマンドを入力し、test_appディレクトリに移動します。
(本記事ではtest_appとしていますが、任意のディレクトリ名で大丈夫です。本記事では説明のためtest_appを作成・使用します)

$ cd Users/ユーザー名/Documents/workspace/test_app

3. Gemfileの作成

以下のコマンドを入力

$ bundle init

test_appディレクトリにGemfileが作成されます。

4. Railsのインストール

Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

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

# gem "rails"

4行目の# gem "rails"#を削除して保存します。
その後以下のコマンドを入力します。

$ bundle install --path vendor/bundle

Users/ユーザー名/Documents/workspace/test_app/vendor/bundle/ruby/2.6.0/gemsにrailsおよび関係するgemがインストールされます。

5. Railsアプリの作成

以下のコマンドを実行

$ bundle exec rails new . --skip-bundle

test_appディレクトリにappbinディレクトリなどが作成されます。
(test_appアプリが作成される)

上記のコマンドを入力した際、Gemfileを上書きするか聞かれるため、あらかじめ古いGemfileは削除しておいても良いかもしれません。

6. bundle installの実行

test_appディレクトリに新しいGemfileが作成されているので

$ bundle install

を入力し、Gemfile内に記述されているgemをインストールします。
(4. で$ bundle install --path vendor/bundleを入力したのでUsers/ユーザー名/Documents/workspace/test_app/vendor/bundle/ruby/2.6.0/gemsにgemがインストールされます)
test_appディレクトリの.bundleファイルにBUNDLE_PATH: "vendor/bundle"と記載されています)

7. Railsサーバーの起動

$ bundle exec rails s

を実行します。
その後、http://localhost:3000にアクセスすると...

rails_ss.png

環境構築成功です!!

※投稿時点(2019年3月上旬)時点で、sqlite3のバージョンによるエラーが確認されています。Gemfileのsqlite3部分をgem 'sqlite3', '~> 1.3.6'とすると解決するようです。

なぜbundle execが必要なのか?

bundle execを書くことで、bundlerでインストールされたgemを使用することができます。
bundle execを書かない場合、システム側(Users/ユーザー名/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems)のgemを参照してしまいます。
今回、システム側にはrailsのgemをインストールしていないため、bundle exec無しではrailsのコマンドを使用することができません。(たぶん...)

備考

「3. Gemfileの作成」以降のコマンドは全てtest_appディレクトリにいる状態で実行しています。

最後に

cloud9を卒業して、環境構築を行ってみました。
初投稿&初心者のため、内容に誤り・不適切な記述がありましたら、ご指摘いただけると幸いです。

参考サイト

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?