LoginSignup
13

More than 5 years have passed since last update.

CentOS 7 で手っ取り早く Ruby on Rails環境を作るメモ

Last updated at Posted at 2017-02-22

概要

Ruby on Rails の勉強することになって、CentOS7 の環境に入れようとしたところ、色んな情報があったけど微妙に古くなってたりしてたので、2017年2月22日(にゃんにゃん!)時点での情報をメモ。

最終的な結論に至るまでのすったもんだは後半にまとめとくので、そういうの興味ないですという方は、前半のコマンドだけ、ガッとコピーしてターミナルに貼り付けたら幸せになれるかも。

参考にした情報

諸先輩方ありがとうございますっ!!! m(_ _)m
http://qiita.com/noraworld/items/d92cca9bb449b48a97aa
http://qiita.com/inouet/items/478f4228dbbcd442bfe8
http://qiita.com/tmf16/items/efcb3a85730d78957249
http://qiita.com/k-yamada@github/items/c3b90f14c4fd81da8722

想定する環境

  • CentOS7 (GUIサーバとしてインストール直後の状況を想定)
  • 作業ユーザはsudo出来ることを想定

※sudo出来ないユーザの場合は、rootでvisudoしておく。記述内容は以下のような感じ(作業ユーザがhogehogeさんのとき)

hogehoge ALL=(ALL) ALL

実行するコマンド

とりあえずこれをコピってターミナルに突っ込んだらうまくいくはず。

sudo yum install -y git gcc openssl-devel readline-devel zlib-devel sqlite-devel gcc-c++ libicu-devel cmake

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

rbenv install -l
rbenv install 2.4.0
rbenv global 2.4.0
ruby --version

gem install rails

rbenv install 2.4.0 のとこで結構時間かかるけど、それで正しいので信じて待つ。
サーバのスペックによって時間違うと思うけどざっくり30分もあれば余裕。

ここまでの七転び八起き

作業ユーザをsudo出来るようにする

su -
visudo

最終行に下記を追加 (hogehogeは作業ユーザに置換)

hogehoge  ALL=(ALL)       ALL

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

yum -y install git
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

rbenv install -l
rbenv install 2.4.0

ここで一度コケる。

Installing ruby-2.4.0...

BUILD FAILED (CentOS Linux 7 using ruby-build 20170112-8-g06da00b)

Inspect or clean up the working tree at /tmp/ruby-build.20170201234557.22789
Results logged to /tmp/ruby-build.20170201234557.22789.log

Last 10 log lines:
checking for ruby... false
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/tmp/ruby-build.20170201234557.22789/ruby-2.4.0':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

Cコンパイラがないと怒られている。

sudo yum -y install gcc

再チャレンジ。数多ある先人の例と同じくエラーが出る。

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...

BUILD FAILED (CentOS Linux 7 using ruby-build 20170112-8-g06da00b)

Inspect or clean up the working tree at /tmp/ruby-build.20170201234923.23155
Results logged to /tmp/ruby-build.20170201234923.23155.log

Last 10 log lines:
The Ruby openssl extension was not compiled.
The Ruby readline extension was not compiled.
The Ruby zlib extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Try running `yum install -y openssl-devel readline-devel zlib-devel` to fetch missing dependencies.

Configure options used:
  --prefix=/home/hogemi/.rbenv/versions/2.4.0
  LDFLAGS=-L/home/hogemi/.rbenv/versions/2.4.0/lib
  CPPFLAGS=-I/home/hogemi/.rbenv/versions/2.4.0/include

てことで。

sudo yum install -y openssl-devel readline-devel zlib-devel

あらためて。

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/hogemi/.rbenv/versions/2.4.0

よーし!

rbenv rehash
rbenv versions
  2.4.0
rbenv global 2.4.0
ruby --version

んでは Rails のインストール。

gem install rails
 .
 .
 .
Done installing documentation for i18n, thread_safe, tzinfo, concurrent-ruby, activesupport, rack, rack-test, mini_portile2, nokogiri, loofah, rails-html-sanitizer, rails-dom-testing, builder, erubis, actionview, actionpack, activemodel, arel, activerecord, globalid, activejob, mime-types-data, mime-types, mail, actionmailer, nio4r, websocket-extensions, websocket-driver, actioncable, thor, method_source, railties, bundler, sprockets, sprockets-rails, rails after 106 seconds
36 gems installed

よーしよーし!

ただ、Railsが動くまでにはまだ色々あるみたいなので引き続き。

プロジェクト作ってみる。

rails new testapp
cd testapp
rails s

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.
         run  bundle exec spring binstub --all
bundler: command not found: spring
Install missing gem executables with `bundle install`

むぅ。

sqlite-develを入れてみる。

sudo yum install -y sqlite-devel

rails s

Could not find gem 'sqlite3' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

ふむ。

bundle install
  .
  .
Bundle complete! 15 Gemfile dependencies, 62 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

お。うまくいったぽ。

rails s
  .
  .
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

ふむ。止まっちゃう。

プロジェクトのGemfileを編集して、therubyracer の行のコメントアウトを外す(有効にする)

vi Gemfile

bundle install
  .
  .
make: g++: コマンドが見つかりませんでした
make: *** [accessor.o] エラー 127

make failed, exit code 2

Gem files will remain installed in
/home/hogemi/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/therubyracer-0.12.3
for inspection.
Results logged to
/home/hogemi/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/extensions/x86_64-linux/2.4.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'` succeeds before
bundling.

おおぅ…。道は長い。

sudo yum install -y gcc-c++

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

うっしゃ。

rails s
  .
  .
  .
* Version 3.7.0 (ruby 2.4.0-p0), codename: Snowy Sagebrush
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

よーしOK!

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
13