Ubuntu 18.04 Railsインストールコマンド$ gem install rails のエラーを解決した話

# 目的

- Ubuntu 18.04でコマンド`$ gem install rails`を実行した際に出たエラーを解決した話をまとめる。

# 経緯

1. 下記記事を参考にUbuntu18.04でRailsの環境構築をしていたところ、コマンド`$ gem install rails`を実行した際にエラーが出た。  
[MacOS / Ubuntu で Ruby on Rails の開発環境を構築する](https://qiita.com/yu-yan/items/6e693caf23054c6ba9a1)

# エラー内容

- 下記にエラー内容を抜粋した内容を記載する。

```
$ gem install rails
Building native extensions. This could take a while...
ERROR:  Error installing rails:
        ERROR: Failed to build gem native extension.

    current directory: /home/linuxbrew/.linuxbrew/lib/ruby/gems/2.6.0/gems/nokogiri-1.10.4/ext/nokogiri
/home/linuxbrew/.linuxbrew/opt/ruby/bin/ruby -I /home/linuxbrew/.linuxbrew/Cellar/ruby/2.6.5/lib/ruby/2.6.0 -r ./siteconf20191025-8352-1g8if1f.rb extconf.rb
checking if the C compiler accepts ... yes
Building nokogiri using packaged libraries.
Using mini_portile version 2.4.0
checking for gzdopen() in -lz... no
zlib is missing; necessary for building libxml2
*** extconf.rb failed ***
・
・
・
extconf failed, exit code 1

Gem files will remain installed in /home/linuxbrew/.linuxbrew/lib/ruby/gems/2.6.0/gems/nokogiri-1.10.4 for inspection.
Results logged to /home/linuxbrew/.linuxbrew/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0/nokogiri-1.10.4/gem_make.out
```

# 分析

- エラーの内容を翻訳して見た。
- どうやらzlibなるものがインストールされていないようだ。

```
# 翻訳前
checking for gzdopen() in -lz... no
zlib is missing; necessary for building libxml2
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary

# 翻訳語
-lzでgzdopen（）をチェックしています... no
zlibがありません。 libxml2のビルドに必要
*** extconf.rbが失敗しました***
何らかの理由でMakefileを作成できませんでした。おそらく必要なものがありません。
```

# 解決方法

- zlibをインストールする為のコマンド`$ sudo apt-get install zlib1g-dev`を実行した。
- 実行後再度コマンド`$ gem install rails`を実行したら正常に実行された。
- 下記に解決方法に至った筆者の流れを記載する。
    1. コマンド`$ gem install rails`を実行したらエラー発生した。
    1. コマンド`$ sudo apt-get install zlib1g-dev`を実行した。
    1. コマンド`$ gem install rails`が正常に実行できた。

# 付録

- 下記に筆者がコマンド`$ gem install rails`でエラーを出してから解決するまでのログを記載する。

```
miriwo@miriwoPC:~$ gem install rails
Building native extensions. This could take a while...
ERROR:  Error installing rails:
        ERROR: Failed to build gem native extension.

    current directory: /home/linuxbrew/.linuxbrew/lib/ruby/gems/2.6.0/gems/nokogiri-1.10.4/ext/nokogiri
/home/linuxbrew/.linuxbrew/opt/ruby/bin/ruby -I /home/linuxbrew/.linuxbrew/Cellar/ruby/2.6.5/lib/ruby/2.6.0 -r ./siteconf20191025-8352-1g8if1f.rb extconf.rb
checking if the C compiler accepts ... yes
Building nokogiri using packaged libraries.
Using mini_portile version 2.4.0
checking for gzdopen() in -lz... no
zlib is missing; necessary for building libxml2
*** 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.

Provided configuration options:
        --with-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/home/linuxbrew/.linuxbrew/Cellar/ruby/2.6.5/bin/$(RUBY_BASE_NAME)
        --help
        --clean
        --use-system-libraries
        --enable-static
        --disable-static
        --with-zlib-dir
        --without-zlib-dir
        --with-zlib-include
        --without-zlib-include=${zlib-dir}/include
        --with-zlib-lib
        --without-zlib-lib=${zlib-dir}/lib
        --enable-cross-build
        --disable-cross-build

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /home/linuxbrew/.linuxbrew/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0/nokogiri-1.10.4/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/linuxbrew/.linuxbrew/lib/ruby/gems/2.6.0/gems/nokogiri-1.10.4 for inspection.
Results logged to /home/linuxbrew/.linuxbrew/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0/nokogiri-1.10.4/gem_make.out
miriwo@miriwoPC:~$ sudo apt-get install zlib1g-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libllvm7 linux-headers-4.18.0-17 linux-headers-4.18.0-17-generic linux-image-4.18.0-17-generic linux-modules-4.18.0-17-generic linux-modules-extra-4.18.0-17-generic
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  zlib1g-dev
0 upgraded, 1 newly installed, 0 to remove and 38 not upgraded.
Need to get 176 kB of archives.
After this operation, 457 kB of additional disk space will be used.
Get:1 http://jp.archive.ubuntu.com/ubuntu bionic/main amd64 zlib1g-dev amd64 1:1.2.11.dfsg-0ubuntu2 [176 kB]
Fetched 176 kB in 0s (453 kB/s)    
Selecting previously unselected package zlib1g-dev:amd64.
(Reading database ... 225859 files and directories currently installed.)
Preparing to unpack .../zlib1g-dev_1%3a1.2.11.dfsg-0ubuntu2_amd64.deb ...
Unpacking zlib1g-dev:amd64 (1:1.2.11.dfsg-0ubuntu2) ...
Setting up zlib1g-dev:amd64 (1:1.2.11.dfsg-0ubuntu2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
miriwo@miriwoPC:~$ gem install rails
Building native extensions. This could take a while...
Successfully installed nokogiri-1.10.4
Successfully installed crass-1.0.5
Successfully installed loofah-2.3.1
Successfully installed rails-html-sanitizer-1.3.0
Successfully installed rails-dom-testing-2.0.3
Successfully installed builder-3.2.3
Successfully installed erubi-1.9.0
Successfully installed actionview-6.0.0
Successfully installed actionpack-6.0.0
Successfully installed activemodel-6.0.0
Successfully installed activerecord-6.0.0
Successfully installed globalid-0.4.2
Successfully installed activejob-6.0.0
Successfully installed mini_mime-1.0.2
Successfully installed mail-2.7.1
Successfully installed actionmailer-6.0.0
Building native extensions. This could take a while...
Successfully installed nio4r-2.5.2
Successfully installed websocket-extensions-0.1.4
Building native extensions. This could take a while...
Successfully installed websocket-driver-0.7.1
Successfully installed actioncable-6.0.0
Successfully installed mimemagic-0.3.3
Successfully installed marcel-0.3.3
Successfully installed activestorage-6.0.0
Successfully installed actionmailbox-6.0.0
Successfully installed actiontext-6.0.0
Successfully installed thor-0.20.3
Successfully installed method_source-0.9.2
Successfully installed railties-6.0.0
Successfully installed sprockets-4.0.0
Successfully installed sprockets-rails-3.2.1
Successfully installed rails-6.0.0
Parsing documentation for nokogiri-1.10.4
Installing ri documentation for nokogiri-1.10.4
Parsing documentation for crass-1.0.5
Installing ri documentation for crass-1.0.5
Parsing documentation for loofah-2.3.1
Installing ri documentation for loofah-2.3.1
Parsing documentation for rails-html-sanitizer-1.3.0
Installing ri documentation for rails-html-sanitizer-1.3.0
Parsing documentation for rails-dom-testing-2.0.3
Installing ri documentation for rails-dom-testing-2.0.3
Parsing documentation for builder-3.2.3
Installing ri documentation for builder-3.2.3
Parsing documentation for erubi-1.9.0
Installing ri documentation for erubi-1.9.0
Parsing documentation for actionview-6.0.0
Installing ri documentation for actionview-6.0.0
Parsing documentation for actionpack-6.0.0
Installing ri documentation for actionpack-6.0.0
Parsing documentation for activemodel-6.0.0
Installing ri documentation for activemodel-6.0.0
Parsing documentation for activerecord-6.0.0
Installing ri documentation for activerecord-6.0.0
Parsing documentation for globalid-0.4.2
Installing ri documentation for globalid-0.4.2
Parsing documentation for activejob-6.0.0
Installing ri documentation for activejob-6.0.0
Parsing documentation for mini_mime-1.0.2
Installing ri documentation for mini_mime-1.0.2
Parsing documentation for mail-2.7.1
Installing ri documentation for mail-2.7.1
Parsing documentation for actionmailer-6.0.0
Installing ri documentation for actionmailer-6.0.0
Parsing documentation for nio4r-2.5.2
Installing ri documentation for nio4r-2.5.2
Parsing documentation for websocket-extensions-0.1.4
Installing ri documentation for websocket-extensions-0.1.4
Parsing documentation for websocket-driver-0.7.1
Installing ri documentation for websocket-driver-0.7.1
Parsing documentation for actioncable-6.0.0
Installing ri documentation for actioncable-6.0.0
Parsing documentation for mimemagic-0.3.3
Installing ri documentation for mimemagic-0.3.3
Parsing documentation for marcel-0.3.3
Installing ri documentation for marcel-0.3.3
Parsing documentation for activestorage-6.0.0
Installing ri documentation for activestorage-6.0.0
Parsing documentation for actionmailbox-6.0.0
Installing ri documentation for actionmailbox-6.0.0
Parsing documentation for actiontext-6.0.0
Installing ri documentation for actiontext-6.0.0
Parsing documentation for thor-0.20.3
Installing ri documentation for thor-0.20.3
Parsing documentation for method_source-0.9.2
Installing ri documentation for method_source-0.9.2
Parsing documentation for railties-6.0.0
Installing ri documentation for railties-6.0.0
Parsing documentation for sprockets-4.0.0
Installing ri documentation for sprockets-4.0.0
Parsing documentation for sprockets-rails-3.2.1
Installing ri documentation for sprockets-rails-3.2.1
Parsing documentation for rails-6.0.0
Installing ri documentation for rails-6.0.0
Done installing documentation for nokogiri, crass, loofah, rails-html-sanitizer, rails-dom-testing, builder, erubi, actionview, actionpack, activemodel, activerecord, globalid, activejob, mini_mime, mail, actionmailer, nio4r, websocket-extensions, websocket-driver, actioncable, mimemagic, marcel, activestorage, actionmailbox, actiontext, thor, method_source, railties, sprockets, sprockets-rails, rails after 48 seconds
31 gems installed
```