読者です 読者をやめる 読者になる 読者になる

goodbyegangsterのブログ

備忘録的な

CnetOS上にRails環境を作成する

はるか昔にphpで作成したwebサイトをRailsで作り直したい。ということで、まずはRails環境を作ります。Railsに関しては、ずっと前に「Ruby on Rails チュートリアル」を一度やっただけです。

環境作成にあたって、以下のサイトにお世話になりました。

LinuxにRuby on Railsをインストールする | tsuchikazu blog

バージョン情報は以下です。

rbenvのインストール

Ruby環境をバージョン毎に管理してくれる有名なやつ。インストール方法はgithubに載ってます。

GitHub - rbenv/rbenv: Groom your app’s Ruby environment

githubからモジュールをクローンしてきます。

$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv

ユーザ環境設定ファイルに、システム環境変数の情報とrbnevのinit処理を記載してあげます。

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

ユーザを入り直せば、rbenv使えるようになります。以下はバージョン確認。

$ rbenv -v
rbenv 1.1.0-2-g4f8925a

ruby-buildの追加

Rubyを簡単にインストールできるようする、rbenvのプラグインらしいです。これもgithubにインストール方法が載ってます。

GitHub - rbenv/ruby-build: Compile and install Ruby

というか、githubからクローンしてくるだけ。

$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ rbenv install
Usage: rbenv install [-f|-s] [-kpv] <version>
       rbenv install [-f|-s] [-kpv] <definition-file>
       rbenv install -l|--list
       rbenv install --version

  -l/--list          List all available versions
  -f/--force         Install even if the version appears to be installed already
  -s/--skip-existing Skip if the version appears to be installed already

  ruby-build options:

  -k/--keep          Keep source tree in $RBENV_BUILD_ROOT after installation
                     (defaults to $RBENV_ROOT/sources)
  -p/--patch         Apply a patch from stdin before building
  -v/--verbose       Verbose mode: print compilation status to stdout
  --version          Show version of ruby-build

For detailed information on installing Ruby versions with
ruby-build, including a list of environment variables for adjusting
compilation, see: https://github.com/rbenv/ruby-build#usage

Rubyのインストール

そうして、Rubyをインストールします。Rails5ではRuby2.2以上が必要のようで、現時点で安定版最新バージョンである2.4.0を入れます。

$ rbenv install 2.4.0
Downloading ruby-2.4.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.0.tar.bz2
Installing ruby-2.4.0...
Installed ruby-2.4.0 to /home/rails/.rbenv/versions/2.4.0
$ rbenv rehash

バージョン確認でインストールされているかどうかを確認。

$ rbenv versions
* system (set by /home/rails/.rbenv/version)
  2.4.0

適当なディレクトリを作成して、インストールしたRubyバージョンの環境を作成します。

$ mkdir test
$ cd test
$ rbenv local 2.4.0
$ rbenv versions
  system
* 2.4.0 (set by /home/rails/test/.ruby-version)

Railsのインストール

最後にRailsを入れます。Railsも最新版である5.0.1を入れます。gemを使ってインストール。

$ gem install rails -v 5.0.1
$ rails -v
Rails 5.0.1

動作確認のため、適当な新規プロジェクトを作成して、rails serverを起動してみます。

$ rails new yukamaki
(略)
$ rails server
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:51: warning: constant ::Fixnum is deprecated
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:52: warning: constant ::Bignum is deprecated
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:94:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireError)
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
Backtrace for gem load error is:
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/execjs-2.7.0/lib/execjs.rb:4:in `<top (required)>'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/uglifier-3.0.4/lib/uglifier.rb:5:in `require'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/uglifier-3.0.4/lib/uglifier.rb:5:in `<top (required)>'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:91:in `require'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:91:in `block (2 levels) in require'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:86:in `each'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:86:in `block in require'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:75:in `each'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:75:in `require'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler.rb:106:in `require'
/home/rails/test/yukamaki/config/application.rb:7:in `<top (required)>'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:88:in `require'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:88:in `block in server'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:85:in `tap'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:85:in `server'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
/home/rails/test/yukamaki/bin/rails:9:in `require'
/home/rails/test/yukamaki/bin/rails:9:in `<top (required)>'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/lib/spring/client/rails.rb:28:in `load'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/lib/spring/client/rails.rb:28:in `call'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/lib/spring/client/command.rb:7:in `call'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/lib/spring/client.rb:30:in `run'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/bin/spring:49:in `<top (required)>'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/lib/spring/binstub.rb:31:in `load'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/lib/spring/binstub.rb:31:in `<top (required)>'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/home/rails/.rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/home/rails/test/yukamaki/bin/spring:14:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Bundler Error Backtrace:
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:90:in `block (2 levels) in require'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:86:in `each'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:86:in `block in require'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:75:in `each'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler/runtime.rb:75:in `require'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/bundler-1.13.7/lib/bundler.rb:106:in `require'
        from /home/rails/test/yukamaki/config/application.rb:7:in `<top (required)>'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:88:in `require'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:88:in `block in server'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:85:in `tap'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:85:in `server'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
        from /home/rails/test/yukamaki/bin/rails:9:in `require'
        from /home/rails/test/yukamaki/bin/rails:9:in `<top (required)>'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/lib/spring/client/rails.rb:28:in `load'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/lib/spring/client/rails.rb:28:in `call'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/lib/spring/client/command.rb:7:in `call'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/lib/spring/client.rb:30:in `run'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/bin/spring:49:in `<top (required)>'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/lib/spring/binstub.rb:31:in `load'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.0/lib/spring/binstub.rb:31:in `<top (required)>'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/rails/.rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/rails/test/yukamaki/bin/spring:14:in `<top (required)>'
        from bin/rails:3:in `load'
        from bin/rails:3:in `<main>'

起動しないですねえ。

「Gem Load Error is: Could not find a JavaScript runtime.」ってメッセージが怪しそうだ、とググってみたら、「Ruby on Rails チュートリアル」に以下の記載が。そんなん覚えてねーし。

第1章 ゼロからデプロイまで | Rails チュートリアル

JavaScriptランタイムがインストールされていないというエラーが表示された場合は、GitHubのexecjsページにあるインストール可能なランタイムの一覧からJavaScriptランタイムを入手してください。 個人的にはNode.jsがおすすめです)。

Node.jsがおすすめとあるので、Node.jsを入れます。入れ方は公式サイトに書いてありました。

Installing Node.js via package manager | Node.js

rootで実行です。

# curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
# yum -y install nodejs

改めてサーバーを起動します。

$ rails server
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:51: warning: constant ::Fixnum is deprecated
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:52: warning: constant ::Bignum is deprecated
=> Booting Puma
=> Rails 5.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/core_ext/numeric/conversions.rb:138: warning: constant ::Fixnum is deprecated
Puma starting in single mode...
* Version 3.6.2 (ruby 2.4.0-p0), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

あれ、リモートから繋がらんぞ。

デフォルトではローカルホストからしか繋がらないので、ipアドレスバインド指定して起動する必要があるそうです。「Ruby on Rails チュートリアル」にも書いてありましたね。うん、全く覚えてない。

再度、IPアドレスバインドし直して起動します。

$ rails server -b 0.0.0.0
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:51: warning: constant ::Fixnum is deprecated
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:52: warning: constant ::Bignum is deprecated
=> Booting Puma
=> Rails 5.0.1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
/home/rails/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/core_ext/numeric/conversions.rb:138: warning: constant ::Fixnum is deprecated
Puma starting in single mode...
* Version 3.6.2 (ruby 2.4.0-p0), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000

繋がったー。

f:id:goodbyegangster:20161229103410j:plain