Ruby with CentOS on Docker


## 目的

- CentOS(v7.3) を Docker で起動して Ruby(v2.4.1) を導入する。


## Docker  起動

```
Inohana:~ inoko$ docker run --name centos -v `pwd`:/centos -itd --restart=always centos tail -f /dev/null
b34358e67eaa4d3140dff0a10e2d7271e8e358e8d945377256916493c7acb71e
Inohana:~ inoko$ docker exec -it centos /bin/bash
[root@b34358e67eaa /]#
```
## Ruby を導入

### rbenvとruby-buildの取得

```
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
```
### Ruby Istall

```
[root@b34358e67eaa ruby-build]# rbenv install 2.4.1
sha256sum: the --quiet option is meaningful only when verifying checksums
Try 'sha256sum --help' for more information.
Downloading ruby-2.4.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.bz2
/root/.rbenv/plugins/ruby-build/bin/ruby-build: line 209: pushd: ruby-2.4.1: No such file or directory

BUILD FAILED (CentOS Linux 7 using ruby-build 20170726)

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

Last 10 log lines:
/tmp/ruby-build.20170807032737.598 ~/.rbenv/plugins/ruby-build
warning: bzip2 not found; consider installing `bzip2` package
tar (child): bzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
```

- 怒られたので bzip2 Install する。

```
[root@b34358e67eaa ruby-build]# yum install bzip2
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: ftp.nara.wide.ad.jp
 * epel: ftp.jaist.ac.jp
 * extras: ftp.yz.yamagata-u.ac.jp
 * updates: ftp.yz.yamagata-u.ac.jp
Resolving Dependencies
--> Running transaction check
---> Package bzip2.x86_64 0:1.0.6-13.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================================
 Package                        Arch                            Version                               Repository                     Size
==========================================================================================================================================
Installing:
 bzip2                          x86_64                          1.0.6-13.el7                          base                           52 k

Transaction Summary
==========================================================================================================================================
Install  1 Package

Total download size: 52 k
Installed size: 82 k
Is this ok [y/d/N]: y
Downloading packages:
bzip2-1.0.6-13.el7.x86_64.rpm                                                                                      |  52 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : bzip2-1.0.6-13.el7.x86_64                                                                                              1/1
  Verifying  : bzip2-1.0.6-13.el7.x86_64                                                                                              1/1

Installed:
  bzip2.x86_64 0:1.0.6-13.el7

Complete!
```

- また怒られた。

```
[root@b34358e67eaa ruby-build]# rbenv install 2.4.1
sha256sum: the --quiet option is meaningful only when verifying checksums
Try 'sha256sum --help' for more information.
Downloading ruby-2.4.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.bz2
Installing ruby-2.4.1...

BUILD FAILED (CentOS Linux 7 using ruby-build 20170726)

Inspect or clean up the working tree at /tmp/ruby-build.20170807032939.685
Results logged to /tmp/ruby-build.20170807032939.685.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.20170807032939.685/ruby-2.4.1':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
```
- gcc Install する。

```
[root@b34358e67eaa ruby-build]# yum install gcc
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: ftp.nara.wide.ad.jp
 * epel: ftp.jaist.ac.jp
 * extras: ftp.yz.yamagata-u.ac.jp
 * updates: ftp.yz.yamagata-u.ac.jp
Resolving Dependencies
--> Running transaction check
---> Package gcc.x86_64 0:4.8.5-11.el7 will be installed
--> Processing Dependency: libgomp = 4.8.5-11.el7 for package: gcc-4.8.5-11.el7.x86_64
--> Processing Dependency: cpp = 4.8.5-11.el7 for package: gcc-4.8.5-11.el7.x86_64
--> Processing Dependency: glibc-devel >= 2.2.90-12 for package: gcc-4.8.5-11.el7.x86_64
--> Processing Dependency: libmpfr.so.4()(64bit) for package: gcc-4.8.5-11.el7.x86_64
--> Processing Dependency: libmpc.so.3()(64bit) for package: gcc-4.8.5-11.el7.x86_64
--> Processing Dependency: libgomp.so.1()(64bit) for package: gcc-4.8.5-11.el7.x86_64
--> Running transaction check
---> Package cpp.x86_64 0:4.8.5-11.el7 will be installed
---> Package glibc-devel.x86_64 0:2.17-157.el7_3.5 will be installed
--> Processing Dependency: glibc-headers = 2.17-157.el7_3.5 for package: glibc-devel-2.17-157.el7_3.5.x86_64
--> Processing Dependency: glibc-headers for package: glibc-devel-2.17-157.el7_3.5.x86_64
---> Package libgomp.x86_64 0:4.8.5-11.el7 will be installed
---> Package libmpc.x86_64 0:1.0.1-3.el7 will be installed
---> Package mpfr.x86_64 0:3.1.1-4.el7 will be installed
--> Running transaction check
---> Package glibc-headers.x86_64 0:2.17-157.el7_3.5 will be installed
--> Processing Dependency: kernel-headers >= 2.2.1 for package: glibc-headers-2.17-157.el7_3.5.x86_64
--> Processing Dependency: kernel-headers for package: glibc-headers-2.17-157.el7_3.5.x86_64
--> Running transaction check
---> Package kernel-headers.x86_64 0:3.10.0-514.26.2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================================
 Package                            Arch                       Version                                  Repository                   Size
==========================================================================================================================================
Installing:
 gcc                                x86_64                     4.8.5-11.el7                             base                         16 M
Installing for dependencies:
 cpp                                x86_64                     4.8.5-11.el7                             base                        5.9 M
 glibc-devel                        x86_64                     2.17-157.el7_3.5                         updates                     1.1 M
 glibc-headers                      x86_64                     2.17-157.el7_3.5                         updates                     669 k
 kernel-headers                     x86_64                     3.10.0-514.26.2.el7                      updates                     4.8 M
 libgomp                            x86_64                     4.8.5-11.el7                             base                        152 k
 libmpc                             x86_64                     1.0.1-3.el7                              base                         51 k
 mpfr                               x86_64                     3.1.1-4.el7                              base                        203 k

Transaction Summary
==========================================================================================================================================
Install  1 Package (+7 Dependent packages)

Total download size: 29 M
Installed size: 60 M
Is this ok [y/d/N]: y
Downloading packages:
(1/8): glibc-headers-2.17-157.el7_3.5.x86_64.rpm                                                                   | 669 kB  00:00:02
(2/8): glibc-devel-2.17-157.el7_3.5.x86_64.rpm                                                                     | 1.1 MB  00:00:03
(3/8): libgomp-4.8.5-11.el7.x86_64.rpm                                                                             | 152 kB  00:00:00
(4/8): cpp-4.8.5-11.el7.x86_64.rpm                                                                                 | 5.9 MB  00:00:03
(5/8): mpfr-3.1.1-4.el7.x86_64.rpm                                                                                 | 203 kB  00:00:00
(6/8): libmpc-1.0.1-3.el7.x86_64.rpm                                                                               |  51 kB  00:00:00
(7/8): gcc-4.8.5-11.el7.x86_64.rpm                                                                                 |  16 MB  00:00:10
kernel-headers-3.10.0-514.26.2 FAILED
http://mirror.nus.edu.sg/centos/7.3.1611/updates/x86_64/Packages/kernel-headers-3.10.0-514.26.2.el7.x86_64.rpm: [Errno 12] Timeout on http://mirror.nus.edu.sg/centos/7.3.1611/updates/x86_64/Packages/kernel-headers-3.10.0-514.26.2.el7.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
Trying other mirror.
(8/8): kernel-headers-3.10.0-514.26.2.el7.x86_64.rpm                                                               | 4.8 MB  00:00:02
------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                     798 kB/s |  29 MB  00:00:37
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mpfr-3.1.1-4.el7.x86_64                                                                                                1/8
  Installing : libmpc-1.0.1-3.el7.x86_64                                                                                              2/8
  Installing : cpp-4.8.5-11.el7.x86_64                                                                                                3/8
  Installing : libgomp-4.8.5-11.el7.x86_64                                                                                            4/8
  Installing : kernel-headers-3.10.0-514.26.2.el7.x86_64                                                                              5/8
  Installing : glibc-headers-2.17-157.el7_3.5.x86_64                                                                                  6/8
  Installing : glibc-devel-2.17-157.el7_3.5.x86_64                                                                                    7/8
  Installing : gcc-4.8.5-11.el7.x86_64                                                                                                8/8
  Verifying  : glibc-devel-2.17-157.el7_3.5.x86_64                                                                                    1/8
  Verifying  : kernel-headers-3.10.0-514.26.2.el7.x86_64                                                                              2/8
  Verifying  : mpfr-3.1.1-4.el7.x86_64                                                                                                3/8
  Verifying  : libmpc-1.0.1-3.el7.x86_64                                                                                              4/8
  Verifying  : libgomp-4.8.5-11.el7.x86_64                                                                                            5/8
  Verifying  : gcc-4.8.5-11.el7.x86_64                                                                                                6/8
  Verifying  : glibc-headers-2.17-157.el7_3.5.x86_64                                                                                  7/8
  Verifying  : cpp-4.8.5-11.el7.x86_64                                                                                                8/8

Installed:
  gcc.x86_64 0:4.8.5-11.el7

Dependency Installed:
  cpp.x86_64 0:4.8.5-11.el7                        glibc-devel.x86_64 0:2.17-157.el7_3.5      glibc-headers.x86_64 0:2.17-157.el7_3.5
  kernel-headers.x86_64 0:3.10.0-514.26.2.el7      libgomp.x86_64 0:4.8.5-11.el7              libmpc.x86_64 0:1.0.1-3.el7
  mpfr.x86_64 0:3.1.1-4.el7

Complete!
```

- また怒られた。

```
[root@b34358e67eaa ruby-build]# rbenv install 2.4.1
sha256sum: the --quiet option is meaningful only when verifying checksums
Try 'sha256sum --help' for more information.
Downloading ruby-2.4.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.bz2
Installing ruby-2.4.1...

BUILD FAILED (CentOS Linux 7 using ruby-build 20170726)

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

Last 10 log lines:
checking for _setjmp as a macro or function... yes
checking for sigsetjmp as a macro or function... no
checking for setjmp type... __builtin_setjmp
checking for prefix of external symbols... NONE
checking pthread.h usability... yes
checking pthread.h presence... yes
checking for pthread.h... yes
checking if make is GNU make... ./configure: line 26601: make: command not found
no
checking for safe null command for make... configure: error: no candidate for safe null command
[root@b34358e67eaa ruby-build]#
```

- make Install する。

```
[root@b34358e67eaa ruby-build]# yum install make
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: ftp.nara.wide.ad.jp
 * epel: ftp.jaist.ac.jp
 * extras: ftp.yz.yamagata-u.ac.jp
 * updates: ftp.yz.yamagata-u.ac.jp
Resolving Dependencies
--> Running transaction check
---> Package make.x86_64 1:3.82-23.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================================
 Package                       Arch                            Version                                Repository                     Size
==========================================================================================================================================
Installing:
 make                          x86_64                          1:3.82-23.el7                          base                          420 k

Transaction Summary
==========================================================================================================================================
Install  1 Package

Total download size: 420 k
Installed size: 1.1 M
Is this ok [y/d/N]: y
Downloading packages:
make-3.82-23.el7.x86_64.rpm                                                                                        | 420 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:make-3.82-23.el7.x86_64                                                                                              1/1
  Verifying  : 1:make-3.82-23.el7.x86_64                                                                                              1/1

Installed:
  make.x86_64 1:3.82-23.el7

Complete!
```

- うまくいった。

```
[root@b34358e67eaa ruby-build]# rbenv install 2.4.1
sha256sum: the --quiet option is meaningful only when verifying checksums
Try 'sha256sum --help' for more information.
Downloading ruby-2.4.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.bz2
Installing ruby-2.4.1...
Installed ruby-2.4.1 to /root/.rbenv/versions/2.4.1

```

## 付随する作業

- which Install する。

```
[root@b34358e67eaa ruby-build]# yum install witch
```
- gem を使いたい。
- 無いと怒られるが which コマンドで見つかる。

```
[root@b34358e67eaa ruby-build]# gem
rbenv: gem: command not found

The `gem' command exists in these Ruby versions:
  2.4.1

[root@b34358e67eaa ruby-build]# which gem
/root/.rbenv/shims/gem
```
- はじめ`ruby -v` コマンドが使えなかったが、 `rbenv global --h` 後、使えるようになった。

```
[root@b34358e67eaa ruby-build]#  rbenv versions
  2.4.1
[root@b34358e67eaa ruby-build]# ruby -v
rbenv: ruby: command not found

The `ruby' command exists in these Ruby versions:
  2.4.1

[root@b34358e67eaa ruby-build]# rbenv global --h
rbenv: version `--h' not installed
[root@b34358e67eaa ruby-build]# rbenv global 2.4.1
[root@b34358e67eaa ruby-build]# ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
```
- すると、`gem`コマンドも使えるようになった。

```
[root@b34358e67eaa ruby-build]# gem install rubyXL
Fetching: mini_portile2-2.2.0.gem (100%)
Successfully installed mini_portile2-2.2.0
Fetching: nokogiri-1.8.0.gem (100%)
Building native extensions.  This could take a while...
Successfully installed nokogiri-1.8.0
Fetching: rubyzip-1.2.1.gem (100%)
Successfully installed rubyzip-1.2.1
Fetching: rubyXL-3.3.26.gem (100%)
Successfully installed rubyXL-3.3.26
Parsing documentation for mini_portile2-2.2.0
Installing ri documentation for mini_portile2-2.2.0
Parsing documentation for nokogiri-1.8.0
Installing ri documentation for nokogiri-1.8.0
Parsing documentation for rubyzip-1.2.1
Installing ri documentation for rubyzip-1.2.1
Parsing documentation for rubyXL-3.3.26
Installing ri documentation for rubyXL-3.3.26
Done installing documentation for mini_portile2, nokogiri, rubyzip, rubyXL after 58 seconds
4 gems installed
```





[参考]
http://qiita.com/rocky-3/items/e590642f0ee1577cfbf0
http://www.task-notes.com/entry/20150624/1435114800

- export PATH は必須、export PATH=\$HOME/.rbenv/bin:$PATH" と1行にまとめても良い、ということ。

[参考]
パスを通すということについて
http://qiita.com/Naggi-Goishi/items/2c49ea50602ea80bf015
http://qiita.com/nbkn/items/01a11392921119fa0153
http://pocketstudio.jp/linux/?%A5%D1%A5%B9(PATH)%A4%CE%B3%CE%C7%A7%A4%C8%C0%DF%C4%EA%CA%FD%CB%A1%A4%CF%A1%A9