LoginSignup
3
4

More than 5 years have passed since last update.

Ruby on Rails5をvagrantに構築

Posted at

vagrant boxを登録

今回はcentos7に環境を作るため以下コマンドにてvagrant boxを登録

$ vagrant box add CentOS7 https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box

ポートフォワーディング設定

Railsの組込サーバを使用する場合3000番ポートを使用するため
Vagrantfileを変更

Vagrantfileの25行目付近のconfig.vm.networを修正

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # ...

  # config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network :"forwarded_port", guest: 3000, host: 3000

  # ...
end

Vagrant起動

上記設定が終われば以下コマンドでvagrantを起動
時間は結構かかります

$ vagrant up

firewallを止める

色々と問題が起こるのでデフォルトのfirewallを止める

vagrant sshでvagrantの中に入る

$ vagrant ssh

vagrantのcentos上で以下を実行

$ sudo systemctl stop firewalld
$ sudo systemctl disable firewalld

gitをインストール(vagrant上)

$ sudo yum -y install git

rbenvインストール(vagrant上)

rubyのバージョン管理ツールであるrbenvをインストール

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

rubyのインストール(vagrant上)

$ rbenv install -l

でrubyのインストール可能なバージョンを取得

今回はv2.3.1を使用するので

$ rbenv install 2.3.1

でv2.3.1をインストールし

$ rbenv golobal 2.3.1

でrubyのバージョンを指定

nodeインストール(vagrant上)

後ほど必要になるのでnodeのバージョン管理ツールである
nodebrewからnode.jsをインストール

nodebrewインストール

$ curl -L git.io/nodebrew | perl - setup

環境変数の設定

.bash_profileに以下を追加

$ vi ~/.bash_profile
export PATH=$HOME/.nodebrew/current/bin:$PATH

bashを再起動

$ source ~/.bash_profile

nodebrewインストール確認

以下コマンドを実行しhelpの内容が表示さればnodebrewはインストール完了

$ nodebrew help

Node.jsインストール

install-binaryでnodeの安定板をインストール

$ nodebrew install-binary stable

インストールしたNode.jsのバージョン確認

$ nodebrew list

先ほどインストールした安定版を指定

$ nodebrew use stable

Node.jsのバージョンを確認し表示されれればインストール完了

$ node -v

Railsをインストール

$ rbenv exec gem install rails

以下コマンドでRailsのバージョンが表示されれば無事インストール完了

$ rbenv exec rails -v
3
4
1

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