LoginSignup
17
17

More than 5 years have passed since last update.

centOS6.5にrails4.2の環境構築

Last updated at Posted at 2015-05-07

環境

centOS 6.5
ruby 2.2.2
rails 4.2.1

yumで必要なものをインストール

最初に今回の構築で必要になるものをyumでまとめてインストールしておきます。

$ sudo yum -y update
$ sudo yum -y install git sqlite sqlite-devel httpd-devel curl-devel apr-devel apr-util-devel libffi-devel openssh openssl openssl-devel readline-devel

mysqlをインストール

railsはsqlite3を初期状態では使用するため、なくてもいいですが本番環境の時のためにインストールしておきます。

$ sudo yum install -y mysql mysql-server mysql-devel
$ sudo chkconfig --level 35 mysqld on
$ sudo service mysqld start

インストールの確認をします。

$ mysql --version
mysql  Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1

rbenvをインストール

rubyを複数バージョンインストールできるrbenvをインストールします。
anyenvがすでに入っている方はanyenv install 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
$ exec $SHELL -l

インストールの確認をします。

$ rbenv -v
rbenv 0.4.0-148-g5b9e4f0

rbenvのプラグインをインストール

rubyのビルドに必要なruby-buildをインストールします

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

必要な人は下記プラグインも入れてください

  • rbenv-gem-rehash

    gemインストール時に必要なrbenv rehashを自動で実行してくれます。

$ git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/ruby-gem-rehash
  • rbenv-update

    rbenv updateコマンド実行時にrbenvのプラグインもアップデートしてくれます。

$ git clone https://github.com/rkh/rbenv-update.git ~/.rbenv/plugins/rbenv-update

rubyをインストール

$ rbenv install 2.2.2
$ rbenv global 2.2.2

インストールの確認をします。

$ ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]

railsをインストール

$ gem install rails --no-ri --no-doc

インストールの確認をします。

$ rails -v
Rails 4.2.1

railsプロジェクトを作成する

rails newコマンドで新規プロジェクトを作成します。

-d mysqlオプションでデフォルトでmysqlを利用する設定でプロジェクトを作成します。sqliteでいい方はこのオプションは必要ありません。

$ rails new tutorial -d mysql

mysqlを使っている場合はrakeコマンドでデータベースを作成します

$ rake db:create

WEBrickでサーバーを起動してみます。

$ cd tutorial
$ rails s
=> Booting WEBrick
=> Rails 4.2.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-05-07 04:32:46] INFO  WEBrick 1.3.1
[2015-05-07 04:32:46] INFO  ruby 2.2.2 (2015-04-13) [x86_64-linux]
[2015-05-07 04:32:46] INFO  WEBrick::HTTPServer#start: pid=3906 port=3000

ブラウザでlocalhost:3000にアクセスしてみましょう。
rails

ExecJS::RuntimeUnavailableエラーが出てしまった場合

ExecJS::RuntimeUnavailable: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.

上記のようなエラーが出た場合は、therubyracerを使うようにGemfileを書き換えます。

$ cd tutorial
$ vi Gemfile

15行目のコメントを外してtherubyracerを有効にします。

gem 'therubyracer', platforms: :ruby

gemをインストールします。

$ bundle install

Sorry, you can't use byebug without Readline.エラーが出てしまった場合

Sorry, you can't use byebug without Readline. To solve this, you need to
rebuild Ruby with Readline support. If using Ubuntu, try `sudo apt-get
install libreadline-dev` and then reinstall your Ruby.

上記のようなエラーが出た場合は、readlineを使うようにGemfileを書き換えます。

$ cd tutorial
$ vi Gemfile

行末にrb-readlineを追加します。

gem 'rb-readline'

gemをインストールします。

$ bundle install

vagrant上でrails serverを起動してページが開けない場合

rails serverコマンド時に-bオプションでipアドレスを指定してあげましょう。

$ rails s -b 0.0.0.0
17
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
17
17