Rails環境設定を次回の為に記録しておく
- rbenvをインストールする (rubyやrailsを使用する上で複数のバージョンを管理する為のツール)
- rubyをインストールする
- railsをインストールする
大きく分けるとたった3つで簡単に見えるがその為に必要なパッケージが多々あったので、結構時間がかかった。
# rbenvインストールの為に、gitをインストールする
yum install git
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.nus.edu.sg
(略)
Complete!
# rbenvをインストール
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
Cloning into '/root/.rbenv'...
remote: Enumerating objects: 5, done.
(略)
Resolving deltas: 100% (1781/1781), done.
# パスを通す
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
# rbenvの設定を自動で追加する
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
# 設定を反映する
source ~/.bash_profile
bash: evalexport PATH="/root/.rbenv/shims:${PATH}"
export RBENV_SHELL=bash
source '/root/.rbenv/libexec/../completions/rbenv.bash'
command rbenv rehash 2>/dev/null
(略)
# rbenvのプラグインruby-buildをインストール
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/
plugins/ruby-build
Cloning into '/root/.rbenv/plugins/ruby-build'...
remote: Enumerating objects: 1, done.
(略)
Resolving deltas: 100% (7258/7258), done.
# カレントディレクトリを移動
cd ~/.rbenv/plugins/ruby-build
# インストール
./install.sh
# railsに必要なパッケージをインストール
yum install -y openssl-devel readline-devel zlib-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.riken.jp
(略)
Complete!
# ruby2.6.6をインストール
rbenv install 2.6.6
Downloading ruby-2.6.6.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.6.tar.bz2
(略)
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/tmp/ruby-build.20200716211822.1602.epihJD/ruby-2.6.6:
configure: error: no acceptable C compiler found in $PATH
# Cコンパイラが無い という事なのでインストール
yum install gcc.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
(略)
Complete!
# ruby2.6.6をインストール
rbenv install 2.6.6
Downloading ruby-2.6.6.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.6.tar.bz2
Installing ruby-2.6.6...
(略)
Installed ruby-2.6.6 to /root/.rbenv/versions/2.6.6
# 使用するrubyのバージョンを設定
rbenv global 2.6.6
# パスが通っていない
ruby -v
bash: ruby: command not found
# 無理矢理パスを通したが前回はしていない
# rbenv rehashしたかも? もしくは.bash_profileにecho追記した時にスペルミスしていた可能性あり
vi /root/.bash_profile
# 1行追記する
export PATH="/root/.rbenv/shims:${PATH}"
which ruby
/root/.rbenv/shims/ruby
ruby -v
ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]
# railsインストール
gem install rails -v "5.2.3"
Fetching concurrent-ruby-1.1.6.gem
Fetching nokogiri-1.10.10.gem
(略)
38 gems installed
rails -v
Rails 5.2.3
# railsプロジェクトを作成する
rails new sample_rails
(略)
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).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
# sqliteが無いのでインストールしろとの事
yum -y install sqlite-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.riken.jp
(略)
Complete!
# 前回execjsが必要だとか言われた記憶が・・・
gem install execjs
Successfully installed execjs-2.7.0
Parsing documentation for execjs-2.7.0
Installing ri documentation for execjs-2.7.0
Done installing documentation for execjs after 0 seconds
1 gem installed
# javascriptランタイムをインストール
gem install mini_racer
Fetching libv8-7.3.492.27.1-x86_64-linux.gem
(略)
compiling mini_racer_extension.cc
make: g++: Command not found
make: *** [mini_racer_extension.o] Error 127
make failed, exit code 2
# c++コンパイラが必要との事でインストール
yum -y install gcc-c++
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.riken.jp
(略)
Complete!
# mini_racerをインストール
gem install mini_racer
Building native extensions. This could take a while...
Successfully installed mini_racer-0.2.15
Parsing documentation for mini_racer-0.2.15
Installing ri documentation for mini_racer-0.2.15
Done installing documentation for mini_racer after 0 seconds
1 gem installed
Gemfile内のmini_racerの行をコメントアウトする
Gemfile
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'mini_racer', platforms: :ruby
# Gemfileの設定を反映する
bundle install
(略)
Bundle complete! 19 Gemfile dependencies, 80 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
# railsサーバを立ち上げる
rails server -b 0.0.0.0
=> Booting Puma
=> Rails 5.2.4.3 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.6 (ruby 2.6.6-p146), 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
# ブラウザで確認するもエラー
# ファイアウォール設定でポートを解放
firewall-cmd --add-port=3000/tcp --zone=public --per
manent
success
firewall-cmd --reload
success
# railsサーバを立ち上げる
rails server -b 0.0.0.0
=> Booting Puma
=> Rails 5.2.4.3 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.6 (ruby 2.6.6-p146), 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
Started GET "/" for 172.16.0.10 at 2020-07-16 22:05:18 +0900
Cannot render console from 172.16.0.10! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Rails::WelcomeController#index as HTML
Rendering /root/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-5.2.4.3/lib/rails/templates/rails/welcome/index.html.erb
Rendered /root/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-5.2.4.3/lib/rails/templates/rails/welcome/index.html.erb (2.8ms)
Completed 200 OK in 18ms (Views: 7.1ms | ActiveRecord: 0.0ms)
表示された!!