0
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 3 years have passed since last update.

ruby on railsの開発環境構築

Last updated at Posted at 2020-06-10

仮想環境

Vagrant(仮想環境を簡単に構築・管理するツール)
VirtualBox(PC上で別のOSをインストールするための仮想化ソフト)
CentOS(Linux)

Vagrantをインストール
https://www.vagrantup.com/downloads.html
VirtualBoxをインストール
https://www.virtualbox.org/wiki/Downloads

CentOS インストール用のディレクトリ(Windows では フォルダ)を作成

mkdir -p project/centos7
cd -p project/centos7

仮想環境(VirtualBox)へ、CentOS 7 をインストール

vagrant init centos/7  # CentOS7用のVagrant設定ファイルを作成する

Vagrantfileファイルがカレントディレクトリへ作成される
仮想環境の上 CentOS の IP アドレスを指定するためにコメントを外す

# config.vm.network "private_network", ip: "192.168.33.10"
↓
config.vm.network "private_network", ip: "192.168.33.10"

仮想環境を起動

vagrant up

仮想環境上のCentOS にログイン(ここからは仮想環境上での操作)

vagrant ssh

selinuxを無効化 ※再起動必要

sudo sed -i "s/\(^SELINUX=\).*/\disabled/" /etc/selinux/config

rbenv をインストール(Rubyのバージョンを変更するためのツール)
まずは必要なパッケージをインストール

sudo yum install -y git gcc openssl-devel readline-devel zlib-devel sqlite-devel gcc-c++ libicu-devel cmake vim

rbenv をインストール

git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source ~/.bash_profile

rbenv が使えることを確認

rbenv --version

rbenv へ Ruby をインストールできるようにするプラグインを追加

git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

Ruby をインストール(ver.2.4.2)

rbenv install 2.4.2

CentOS環境全体で使えるrubyのバージョンを指定

rbenv global 2.4.2    

ruby のバージョンや設定を反映

rbenv rehash     

確認

ruby -v

Bundler のインストール(Rubyのライブラリ管理ツール)

gem install bundler

rbenv のバージョンや設定を反映

rbenv rehash 

確認

bundle -v

Ruby on Rails のインストール

mkdir -p /home/vagrant/app/myapp  # インストールディレクトリを作成
cd /home/vagrant/app/myapp

Gemfile ファイルを作成

bundle init

gem “rails” のコメントを外す

sed -i 's/# gem "rails"/gem "rails", "~> 5.1.0"/g' Gemfile

Ruby on Rails と関連する gem を自動でインストールする

bundle install --path vendor/bundler

Ruby on Rails の雛形を作成

bundle exec rails new .

javascript のランタイムの gem のコメントを外す

sed -i "s/# gem 'therubyracer'/gem 'therubyracer'/g" Gemfile

javascript のランタイムをインストールする

bundle install

Ruby on Rails を起動

./bin/rails s -b 0.0.0.0

ブラウザで、以下にアクセスしてrailsが起動しているか確認
http://192.168.33.10:3000

WindowsからCentOS上のファイルを操作できるようにする

CentOs上の設定

ファイル共有用のデーモンであるsambaをインストール

yum install samba

共有用のアカウントを作り,ホームディレクトリのパーミッションを777に変更

adduser vagrant
chown 777 /home/vagrant

sambaへユーザを登録
※パスワード聞かれるからご自由に

pdbedit -a vagrant

samba.confの一番下に設定を追記
※Windowsのエクスプローラのパス欄に「\\share」と入力するとアクセスできるようになる

vim /etc/samba/smb.conf

以下の設定を追記

[share]
    path = /home/vagrant
    writable = yes

デーモンをスタートアップへ登録し起動

systemctl enable smb
systemctl start smb

Windowsでの設定

拡張機能のSMBクライアントを有効化
コントロールパネル⇒
プログラム⇒
Windowsの機能の有効化または無効化⇒
SMB 1.0/CIFSクライアント
にチェックをつけて『OK』

次回から開発環境起動まで

VagrantFileがあるディレクトリまで移動

cd /Users/owner/project/centos7

仮想環境を起動

vagrant up

仮想環境にログイン

vargant ssh

rails起動

./bin/rails s -b 0.0.0.0

windowsのディレクトリ検索窓から

\\192.168.33.10\share にアクセス

user:vagrant
Password:vagrant
0
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
0
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?