rails serverが立ち上がらず、therubyracerをインストールするも失敗するエラーの解決方法

Railsのサーバー起動周りでエラーが出たので、解決策をメモ。

* CentOS7
* Rails 5.2.2

## エラー1 : rails serverが立ち上がらない

```bash
[root@c7836a29680b railsproject]# rails server
Traceback (most recent call last):
	61: from bin/rails:3:in `<main>'
	60: from bin/rails:3:in `load'
	59: from /railsproject/bin/spring:15:in `<top (required)>'
	58: from /root/.rbenv/versions/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:70:in `require'
	57: from /root/.rbenv/versions/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:70:in `require'
	56: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
	55: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
	54: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
	53: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
	52: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
	51: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
	50: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
	49: from /railsproject/bin/rails:9:in `<top (required)>'
	48: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
	47: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
	46: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
	45: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
	44: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
	43: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:83:in `register'
	42: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
	41: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
	40: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/commands.rb:18:in `<main>'
	39: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/command.rb:46:in `invoke'
	38: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/command/base.rb:65:in `perform'
	37: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
	36: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
	35: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
	34: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/commands/server/server_command.rb:142:in `perform'
	33: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/commands/server/server_command.rb:142:in `tap'
	32: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.2/lib/rails/commands/server/server_command.rb:145:in `block in perform'
	31: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
	30: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
	29: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
	28: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
	27: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
	26: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:83:in `register'
	25: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
	24: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
	23: from /railsproject/config/application.rb:7:in `<main>'
	22: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-2.0.1/lib/bundler.rb:114:in `require'
	21: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-2.0.1/lib/bundler/runtime.rb:65:in `require'
	20: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-2.0.1/lib/bundler/runtime.rb:65:in `each'
	19: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-2.0.1/lib/bundler/runtime.rb:76:in `block in require'
	18: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-2.0.1/lib/bundler/runtime.rb:76:in `each'
	17: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-2.0.1/lib/bundler/runtime.rb:81:in `block (2 levels) in require'
	16: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
	15: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
	14: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:83:in `register'
	13: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
	12: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
	11: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/uglifier-4.1.20/lib/uglifier.rb:5:in `<main>'
	10: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
	 9: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
	 8: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
	 7: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
	 6: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
	 5: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:83:in `register'
	 4: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
	 3: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.4.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
	 2: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/execjs-2.7.0/lib/execjs.rb:4:in `<main>'
	 1: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'
/root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect': Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
```

ググってみたが、nodejs入れるといいと言われる。確認してみたが入っている。

```bash
[root@c7836a29680b railsproject]# yum install nodejs
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: ftp-srv2.kddilabs.jp
 * extras: ftp-srv2.kddilabs.jp
 * updates: ftp-srv2.kddilabs.jp
No package nodejs available.
```

### 解決方法:therubyracerをGemfileに入れる

Gemfileに追記して、

```bash
# echo "gem 'therubyracer'" >> Gemfile
```

bundle installを実行する。

## エラー2:therubyracerのインストールに失敗する

```bash
Fetching therubyracer 0.12.3
Installing therubyracer 0.12.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/therubyracer-0.12.3/ext/v8
/root/.rbenv/versions/2.5.0/bin/ruby -r ./siteconf20190219-1132-hg4o9e.rb
extconf.rb
checking for -lpthread... yes
creating Makefile

current directory:
/root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/therubyracer-0.12.3/ext/v8
make "DESTDIR=" clean

current directory:
/root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/therubyracer-0.12.3/ext/v8
make "DESTDIR="
compiling accessor.cc
make: g++: Command not found
make: *** [accessor.o] Error 127

make failed, exit code 2

Gem files will remain installed in
/root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/therubyracer-0.12.3 for
inspection.
Results logged to
/root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/therubyracer-0.12.3/gem_make.out

An error occurred while installing therubyracer (0.12.3), and Bundler
cannot continue.
Make sure that `gem install therubyracer -v '0.12.3' --source
'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  therubyracer
```

### 解決方法:gcc-c++をインストールする

```bash
# yum install gcc-c++
```

そしてbundle update

```bash
# bundle update
```

## 解決！

```bash

[root@c7836a29680b railsproject]# rails server
=> Booting Puma
=> Rails 5.2.2 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.0-p0), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
^C- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2019-02-19 03:34:47 +0000 ===
- Goodbye!
Exiting
```