Ruby oci-8をインストールする

### TLDR
はまったところを中心にメモ書きです。
ruby-oci8をインストールするには、


- [公式](https://www.oracle.com/jp/database/technologies/instant-client/downloads.html)  からInstantClientをコピーする
- 環境変数を設定する
- [公式](https://www.oracle.com/jp/database/technologies/instant-client/downloads.html)  からSDKをダウンロード、InstantClientをコピーしたディレクトリ先に移す






### 過程

```
sudo gem install ruby-oci8
```

以下のエラーが出る。

```
$ sudo -E gem install ruby-oci8
Password:
Building native extensions. This could take a while...
ERROR:  Error installing ruby-oci8:
	ERROR: Failed to build gem native extension.

    current directory: /Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r ./siteconf20201214-8821-yb3nw.rb extconf.rb
attempting to locate oracle-instantclient...
checking load library path... 
*** 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
	--without-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=/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/$(RUBY_BASE_NAME)
	--with-instant-client
	--without-instant-client
	--with-instant-client-dir
	--without-instant-client-dir
	--with-instant-client-include
	--without-instant-client-include=${instant-client-dir}/include
	--with-instant-client-lib
	--without-instant-client-lib=${instant-client-dir}/lib
/Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8/oraconf.rb:568:in `guess_ic_dir': RuntimeError (RuntimeError)
	from /Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8/oraconf.rb:268:in `get'
	from extconf.rb:22:in `<main>'
/Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8/oraconf.rb:568:in `guess_ic_dir': Set the environment variable DYLD_LIBRARY_PATH, DYLD_FALLBACK_LIBRARY_PATH or (RuntimeError)
OCI_DIR to point to the Instant client directory.

If DYLD_LIBRARY_PATH or DYLD_FALLBACK_LIBRARY_PATH is set, the environment
variable must be set at runtime also.

If OCI_DIR is set, dependent shared library paths are checked. If the checking
is passed, ruby-oci8 works without DYLD_LIBRARY_PATH or DYLD_FALLBACK_LIBRARY_PATH.

Note: OCI_DIR should be absolute path.
Note: DYLD_* environment variables are unavailable for security reasons on OS X 10.11 El Capitan.
	from /Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8/oraconf.rb:268:in `get'
	from extconf.rb:22:in `<main>'
  DYLD_LIBRARY_PATH is not set.
  DYLD_FALLBACK_LIBRARY_PATH is not set.
  checking OCI_DIR...
  checking the default value of DYLD_FALLBACK_LIBRARY_PATH...
    checking /Users/suzukiko/lib... no
    checking /usr/local/lib... no
    checking /lib... no
    checking /usr/lib... no
---------------------------------------------------
Error Message:
  Set the environment variable DYLD_LIBRARY_PATH, DYLD_FALLBACK_LIBRARY_PATH or
  OCI_DIR to point to the Instant client directory.
  
  If DYLD_LIBRARY_PATH or DYLD_FALLBACK_LIBRARY_PATH is set, the environment
  variable must be set at runtime also.
  
  If OCI_DIR is set, dependent shared library paths are checked. If the checking
  is passed, ruby-oci8 works without DYLD_LIBRARY_PATH or DYLD_FALLBACK_LIBRARY_PATH.
  
  Note: OCI_DIR should be absolute path.
  Note: DYLD_* environment variables are unavailable for security reasons on OS X 10.11 El Capitan.
  
Backtrace:
  /Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8/oraconf.rb:568:in `guess_ic_dir'
  /Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8/oraconf.rb:268:in `get'
  extconf.rb:22:in `<main>'
---------------------------------------------------
See:
 * http://www.rubydoc.info/github/kubo/ruby-oci8/file/docs/install-full-client.md for Oracle full client
 * http://www.rubydoc.info/github/kubo/ruby-oci8/file/docs/install-instant-client.md for Oracle instant client
 * http://www.rubydoc.info/github/kubo/ruby-oci8/file/docs/install-on-osx.md for OS X
 * http://www.rubydoc.info/github/kubo/ruby-oci8/file/docs/report-installation-issue.md to report an issue.


extconf failed, exit code 1

Gem files will remain installed in /Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8 for inspection.
Results logged to /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-19/2.6.0/ruby-oci8-2.2.8/gem_make.out

```

↓の2.2.1のようにInstant Clientのzipファイルを/opt/oralce/で解凍します。
https://docs.oracle.com/cd/F19136_01/lnoci/instant-client.html#GUID-D0042396-7C99-4450-962C-6E3DBF6EFD41

終わったら環境変数を設定します。※ディレクトリの終わりのスラッシュは不要であるのが注意点です。

```console
OCI_DIR=/opt/oracle/instantclient_19_8 export OCI_DIR
DYLD_LIBRARY_PATH=/opt/oracle/instantclient_19_8 export DYLD_LIBRARY_PATH
```

環境変数を引き継ぐために-Eオプションをつけます。

```
sudo -E gem install ruby-oci8
```


SDKを入れろとエラーがでます。

```
Building native extensions. This could take a while...
ERROR:  Error installing ruby-oci8:
	ERROR: Failed to build gem native extension.

    current directory: /Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r ./siteconf20201214-8835-1h4v2a0.rb extconf.rb
attempting to locate oracle-instantclient...
checking load library path... 
  DYLD_LIBRARY_PATH is not set.
  DYLD_FALLBACK_LIBRARY_PATH is not set.
  checking OCI_DIR...
    checking /opt/oracle/instantclient_19_8... yes
  checking dependent shared libraries in /opt/oracle/instantclient_19_8/libclntsh.dylib.19.1...
  /opt/oracle/instantclient_19_8/libclntsh.dylib.19.1 looks like an instant client.
checking for cc... ok
checking for gcc... yes
checking for LP64... yes
checking for sys/types.h... yes
checking for ruby header... ok
*** 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
	--without-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=/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/$(RUBY_BASE_NAME)
	--with-instant-client
	--without-instant-client
	--with-instant-client-dir
	--without-instant-client-dir
	--with-instant-client-include
	--without-instant-client-include=${instant-client-dir}/include
	--with-instant-client-lib
	--without-instant-client-lib=${instant-client-dir}/lib
/Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8/oraconf.rb:1054:in `initialize': RuntimeError (RuntimeError)
	from /Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8/oraconf.rb:271:in `new'
	from /Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8/oraconf.rb:271:in `get'
	from extconf.rb:22:in `<main>'
/Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8/oraconf.rb:1054:in `initialize': '/opt/oracle/instantclient_19_8/sdk/include/oci.h' does not exist. (RuntimeError)
Install 'Instant Client SDK'.
	from /Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8/oraconf.rb:271:in `new'
	from /Library/Ruby/Gems/2.6.0/gems/ruby-oci8-2.2.8/ext/oci8/oraconf.rb:271:in `get'
	from extconf.rb:22:in `<main>'
---------------------------------------------------
Error Message:
  '/opt/oracle/instantclient_19_8/sdk/include/oci.h' does not exist.
  Install 'Instant Client SDK'.
```


https://www.oracle.com/jp/database/technologies/instant-client/downloads.html
環境にあったSDKをインストールし、エラーメッセージに指定のあるディレクトリにおきます。


再度gem installを実行すると、

```
Error Message:
  Could not compile with Oracle instant client.
  You may need to set a environment variable:
      DYLD_LIBRARY_PATH=/opt/oracle/instantclient_19_8
      export DYLD_LIBRARY_PATH

（略）

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

  /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-19/2.6.0/ruby-oci8-2.2.8/mkmf.log

extconf failed, exit code 1

```

とあったので、mkmf.logを見てみると、
/opt/oracle/instantclient_19_8/sdk/include 
以下のファイルがないようなメッセージがあったため、（具体的なものは見失ってしまいまいた。）
ディレクトリをつくります。

```
 mkdir /opt/oracle/instantclient_19_8/sdk/include
```

作ったディレクトリへ


```
/SDKをインストールしたパス/instantclient10_1/sdk/include/
```
の中身を全てコピーします。


```
$ sudo -E gem install ruby-oci8
Building native extensions. This could take a while...
Successfully installed ruby-oci8-2.2.8
Parsing documentation for ruby-oci8-2.2.8
Installing ri documentation for ruby-oci8-2.2.8
Done installing documentation for ruby-oci8 after 1 seconds
1 gem installed
```

インストールできました。



### 参考
https://www.rubydoc.info/github/kubo/ruby-oci8/file/docs/install-instant-client.md
https://stackoverflow.com/questions/11894194/how-to-install-ruby-oci8
https://reboooot.net/post/su-and-sudo-spec-related-to-env/
https://docs.oracle.com/cd/F19136_01/lnoci/instant-client.html#GUID-AAB0378F-2C7B-41EB-ACAC-18DD5D052B01