LoginSignup
0
0

More than 1 year has passed since last update.

WindowsのWSL2環境にRailsをローカルインストールする

Last updated at Posted at 2021-09-20

概要

WSL2に構築したCentOS環境上に、Railsをインストールする。
環境を汚したくないので、グローバルのgemではなくローカルにインストールする。

前提

  • WSL2にLinux環境を構築している
  • rbenvでrubyを管理している

環境

  • OSはCentOS 7.9
  • ruby versionは2.7.4
  • bundler versionは2.1.4(rubyに内包のバージョン)

やりたいこと

  • Railsをローカルにインストールしてアプリケーションのベースを作る

手順

事前準備

root権限(またはsudo)で g++MySQL をインストールしておく。

# yum install gcc-c++
# yum install mysql-devel

ディレクトリ作成

ディレクトリを作成し、その中で作業する。

$ mkdir rails_application
$ cd rails_application

Gemfileの作成

下記コマンドを実行してGemfileを作成する。

$ bundle init

出来たGemfileを編集する。(gem "rails" のコメントを外す)

# frozen_string_literal: true

source "https://rubygems.org"

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

gem "rails"

Railsのインストール

.bundle/configを作成し以下のように編集する。

---
BUNDLE_PATH: "vendor/bundle"

Railsをインストールする。

$ bundle install
-- 省略 --
Using rails 6.1.4.1
Bundle complete! 1 Gemfile dependency, 42 gems now installed.
Bundled gems are installed into `./vendor/bundle`

無事、vendor/bundle配下にインストールできた。

Railsアプリケーションの作成

rails new コマンドでアプリケーションを作成。
この際、下記2点を考慮し、オプションを設定

  • DBにはMySQLを使いたいので、 -d オプションを指定
  • テストにはRspecを使いたいので、 -T オプションを指定
$ bundle exec rails new . -d mysql -T

Gemfile を上書きするか?と聞かれるので了承する。

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

無事インストールが完了した。

作成されたファイルの確認

$ ls -1
app
bin
config
config.ru
db
Gemfile
Gemfile.lock
lib
log
package.json
public
Rakefile
README.md
storage
tmp
vendor

まずはここまで。

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