LoginSignup
7
7

More than 5 years have passed since last update.

ruby2.4.1 + rails5.0.2環境構築(CentOS7.3)

Posted at

構築していきます。
今回はrubyとrailsの最新版をインストールします。
その後、アプリを作ってスタート画面をブラウザに表示するまで行きます。

動作環境マシン

$ cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 

ソフトウェアバージョン

$ rbenv -v
rbenv 1.1.0-2-g4f8925a
$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
$ rails -v
Rails 5.0.2

rbenvのインストール

rubyは他のバージョンと最新を共存させたいため、rbenv経由でインストールします。今回はrubyというユーザを事前に作成しておき、そのユーザで利用します。

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

$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ cd ~/.rbenv/plugins/ruby-build
$ sudo ./install.sh

rubyのインストール

インストール可能なバージョンを確認して、最新(2017/04/07時点)を導入します。以下の通り、最新は2.4.1のようです。

$ rbenv install -list
  2.4.0
  2.4.1 <
  2.5.0-dev
  jruby-1.5.6
  jruby-1.6.3

$ rbenv install 2.4.1

インストールが途中で失敗した。ログを見る限り、パッケージが足りてないように見受けられる。

BUILD FAILED (CentOS Linux 7 using ruby-build 20170405-2-g3b15693)

Inspect or clean up the working tree at /tmp/ruby-build.20170407234557.15644
Results logged to /tmp/ruby-build.20170407234557.15644.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.

パッケージを入れて、再度インストールする
毎回このエラー出してしまう気がする。。。
$ sudo yum install -y openssl-devel readline-devel zlib-devel
$ rbenv install 2.4.1
Installed ruby-2.4.1 to /home/ruby/.rbenv/versions/2.4.1

$ ruby -v
rbenv: ruby: command not found
The `ruby' command exists in these Ruby versions:
  2.4.1

$ rbenv global 2.4.1
$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

$ echo 'rbenv global 2.4.1' >> .bash_profile
$ cat /home/ruby/.bash_profile
〜省略〜
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
rbenv global 2.4.1

railsのインストール

rubyがインストールできたので、railsをインストールします。

$ gem update --no-document
$ gem cleanup
$ gem install rails --no-document
$ rails -v
Rails 5.0.2

プロジェクト作成

プロジェクトを作成する。今回のプログラムではアクティブレコードは利用しないため除外。他にも不要なものがあるけど後回し。

$ rails new right --skip-active-recode
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.

sqliteを利用しないが、とりあえず進む為にインストールする
(あとで削除します)

$ gem install sqlite3 -v '1.3.13'
Building native extensions.  This could take a while...
ERROR:  Error installing sqlite3:
    ERROR: Failed to build gem native extension.
〜中略〜
sqlite3.h is missing. Try 'brew install sqlite3',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
掘っても掘ってもキリがない。。。

$ sudo yum install sqlite-devel
$ gem install sqlite3 -v '1.3.13'
$ rails new right --skip-active-recode
Bundle complete! 15 Gemfile dependencies, 62 gems now installed.

インストールが全て終わったので、Pumaを動かして画面から確認します

$ rails server -b 0.0.0.0

動いてくれました。
スクリーンショット 2017-04-08 0.44.53.png

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