dockerのUbuntuコンテナにrbenvをインストールする手順
dockerのUbuntuコンテナでRuby関連の実験をしたく、rbenvを導入したので手順をまとめます。docker hubの公式Rubyコンテナは敢えて使わない方針です。
rbenvのインストール
apt-get update
して、gitをインストール後、git clone
します。
apt-get update
apt-get 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
ruby-buildが必要なので、https://github.com/rbenv/ruby-build#installationを参考にgit cloneします。ruby-buildはコマンドラインでRubyの様々なバージョンを簡単にインストールするためのプラグインで、インストールするとrbenv install
コマンドが使えるようになります。
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
最後にrbenvが正常にインストールされたか、下記コマンドでチェックします。
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
rbenv -v
rubyのインストール
次のようなコマンドで好きなバージョンのrubyを指定してインストールできます。
rbenv install 2.5.1
インストールできるリストは次のコマンドでチェックできます。
rbenv install -l
インストール後、下記コマンドでインストールしたrubyをセットします。
rbenv global 2.5.1
トラブルシューティング
dockerのUbuntuコンテナにインストールする際、ハマりどころが色々ありましたのでトラブルシューティングとしてメモしました。これで次回以降のインストールも余裕ですね!やったね!
rbenv shimsのPATHが無くて怒られる
Checking for rbenv shims in PATH: not found
The directory `/root/.rbenv/shims’ must be present in PATH for rbenv to work.
Please run `rbenv init’ and follow the instructions.
rbenvは次のような感じになるよう、shimsのPATHを優先的に読ませる必要があります。
~/.rbenv/shims:/usr/local/bin:/usr/bin:/bin
~/.bash_profileに記述するか、rbenv init
コマンドをSSHの都度実行する必要があります。rbenv init
を毎回実行するのは面倒なので、次のように~/.bash_profileに記述して、SSHの際に自動でrbenv init
が実行されるよう設定すると楽チンです。
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
ruby-bulidが無くて怒られる
Checking `rbenv install’ support: not found
Unless to plan to add Ruby versions manually, you should install ruby-build.
Please refer to https://github.com/rbenv/ruby-build#installation
ruby-buildはコマンドラインでRubyの様々なバージョンを簡単にインストールするためのプラグインです。ruby-buildを使うと、rbenv install
コマンドが使えるようになります。
ruby-buildはrbenvのディレクトリにgit clone
してインストールします。
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
Cコンパイラが無くてrubyのインストールに失敗する
dockerのUbuntuのコンテナなど、Cコンパイラがインストールされていないマシンではインストールに失敗します。RubyはもともとC言語で作られている言語だからです。
BUILD FAILED (Ubuntu 18.04 using ruby-build 20180822-8-g336584c)
Inspect or clean up the working tree at /tmp/ruby-build.20180904233402.4696
Results logged to /tmp/ruby-build.20180904233402.4696.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.20180904233402.4696/ruby-2.5.1′:
configure: error: no acceptable C compiler found in $PATH
See `config.log’ for more details
Cコンパイラは次のコマンドでインストールできます。
apt-get install build-essential
依存関係のライブラリがないっつって怒られる
例えば次のような感じのエラー。指示通りapt-get install -y libreadline-dev zlib1g-dev
でインスールします。
BUILD FAILED (Ubuntu 18.04 using ruby-build 20180822-8-g336584c)
Inspect or clean up the working tree at /tmp/ruby-build.20180904234436.5583
Results logged to /tmp/ruby-build.20180904234436.5583.logLast 10 log lines:
installing capi-docs: /root/.rbenv/versions/2.5.1/share/doc/ruby
The Ruby readline extension was not compiled.
The Ruby zlib extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Try running `apt-get install -y libreadline-dev zlib1g-dev` to fetch missing dependencies.Configure options used:
–prefix=/root/.rbenv/versions/2.5.1
LDFLAGS=-L/root/.rbenv/versions/2.5.1/lib
CPPFLAGS=-I/root/.rbenv/versions/2.5.1/include
Rubyのインストールに成功したっぽいけど、rubyコマンドがないとか言われる
例えば下記の文字。ruby -v
などでバージョンを確認した時に表示されるエラーです。
rbenv: ruby: command not found
The `ruby’ command exists in these Ruby versions:
2.5.1
rbenvはインストールしたrubyのバージョンを指定する必要があるので、下記コマンドでセットします。
rbenv global 2.5.1