LoginSignup
5
7

More than 5 years have passed since last update.

CentOSにRailsをインストールする

Last updated at Posted at 2017-07-25

はじめに

久しぶりにCentOSへRailsをインストールする機会があったので、Railsのインストールからアクセス確認までの手順をメモしました。

もくじ
1. Rails & Bundler
2. サンプルアプリ作成
3. アクセス確認

環境
CentOS 7.3.1611
Rails 5.1.2

準備

事前にRubyをインストールしておく
参考:CentOSにrbenv, Rubyをインストールする - Qiita

1. Rails & Bundler

# インストール
$ gem install rails
$ gem install bundler

2. サンプルアプリ作成

# hello-appアプリを作成
$ rails new hello-app

補足. sqlite-devel

rails newの際に実行されるbundle installで下記のエラーが発生した場合は、sqlite-develをインストールする

エラーログ


Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

...

checking for sqlite3.h... no
sqlite3.h is missing. Try 'brew install sqlite3',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path (the
location where your sqlite3 shared library is located).

...

An error occurred while installing sqlite3 (1.3.13), and Bundler cannot continue.
Make sure that `gem install sqlite3 -v '1.3.13'` succeeds before bundling.
In Gemfile:
  sqlite3
         run  bundle exec spring binstub --all
bundler: command not found: spring
Install missing gem executables with `bundle install`

解決方法

# インストール
$ sudo yum -y install sqlite-devel

# バージョン確認
$ yum list installed | grep sqlite-devel

# 再度bundle installを実行して、
# 成功することを確認
$ cd hello-app
$ bundle install

...
Bundle complete! 16 Gemfile dependencies, 70 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

アクセス確認

# アプリのディレクトリへ移動
$ cd hello-app

# 80ポートでアクセス確認する場合
$ sudo env PATH=$PATH bin/rails s -b 0.0.0.0 -p 80

ブラウザでサーバのアドレスにアクセスする
http://xxx.xxx.xxx.xxx

Ruby_on_Rails_tiny.png

アクセス出来ない場合はFWの設定などを見直す

# サーバ停止
Ctrl + C 

補足. sudo

sudoからのrailsコマンド実行に失敗する場合

rbenvでRubyをインストールした環境ではsudo実行時にPATHが通ってない状態になるため、railsコマンドが実行できない

# サーバ起動
$ sudo bin/rails s -b 0.0.0.0 -p 80

# 結果
/usr/bin/env: ruby: No such file or directory

解決方法

sudoenv PATH=$PATHを付与して実行する

$ sudo env PATH=$PATH bin/rails s -b 0.0.0.0 -p 80

補足. therubyracer, gcc-c++

therubyracerが有効になってない場合

サーバ起動時に以下のエラーが発生する場合はtherubyracerを有効にする

エラーログ

/home/xxx/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:85:in `rescu
e in block (2 levels) in require': There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequire
Error)
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available
 runtimes.
...

解決方法

# Gemfileで`therubyracer`のコメントを外す
$ vi Gemfile
gem 'therubyracer', platforms: :ruby

# インストール
$ bundle install

# 成功することを確認
# 失敗した場合は、次項を参考に「gcc-c++」のインストールを行う
Bundle complete! 17 Gemfile dependencies, 73 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

gcc-c++がインストールされてない場合

therubyracerのbundle install時に以下のエラーが発生する場合はgcc-c++をインストールする

エラーログ

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
...
make: g++: Command not found
make: *** [accessor.o] Error 127
...
An error occurred while installing therubyracer (0.12.3), and Bundler cannot continue.
Make sure that `gem install therubyracer -v '0.12.3'` succeeds before bundling.
In Gemfile:
  therubyracer

解決方法

# インストール
$ sudo yum install -y gcc-c++

# バージョン確認
$ yum list installed  | grep gcc-c++
5
7
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
5
7