LoginSignup
3
3

More than 3 years have passed since last update.

rbenvでUbuntu20.04にrubyをインストール

Posted at

環境

  • OS:Ubuntu 20.04
  • Rubyバージョン:2.5.8

前提として、VMware上に作成した仮想環境。
OSインストールしたてのまっさらな状態からrbenvを使ってrubyをインストールする。

UbuntuへのRubyインストール

参考記事はこちら
まずはgitのインストールから。
一応、gitがデフォルトでインストールされていないか確認しておく。

$ git

コマンド 'git' が見つかりません。次の方法でインストールできます:

sudo apt install git

では、インストール。

$ sudo apt install git

一応、インストール時のログはテキストで残しておく。
インストール後、以下の設定を実施。

$ git config --global user.name [ユーザ名]
$ git config --global user.email [メールアドレス]

gitのインストールが完了したので、ここからRubyをインストール。
まず、rbenvをダウンロード。

$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

.bashrcに以下の記述を追加(echo '文字列' >> 'ファイル名'でファイルに文字列を書き込むことができる)。

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc

実行後、エディタで.bashrcの中身を確認してみると、ファイルの末尾にechoした内容が追加されていることが分かる。
captcher01.png

sourceコマンドで.bashrcの内容を読み込む。

$ source ~/.bashrc

インストール可能なRubyのバージョンを確認

$ rbenv install -l
2.5.8
2.6.6
2.7.1
jruby-9.2.12.0
maglev-1.0.0
mruby-2.1.1
rbx-5.0
truffleruby-20.1.0
truffleruby+graalvm-20.1.0

Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all' to show all local versions.

サポート範囲内の2.5~2.7までしかインストールできない模様。
参考書とバージョンは異なるものの、2.5.8にてインストールすることにした。

$ rbenv install 2.5.8

Downloading ruby-2.5.8.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.8.tar.bz2
Installing ruby-2.5.8...

WARNING: ruby-2.5.8 is nearing its end of life.
It only receives critical security updates, no bug fixes.


BUILD FAILED (Ubuntu 20.04 using ruby-build 20200727)

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

エラーとなってしまった。Cコンパイラが入ってないよ!というエラーのようなので、gccをインストールしてみる。

$ sudo apt install gcc

正常終了した。
こちらも、一応インストールログはテキストで記録しておく。

再度rbenvでrubyをインストール

$ rbenv install 2.5.8

Downloading ruby-2.5.8.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.8.tar.bz2
Installing ruby-2.5.8...

WARNING: ruby-2.5.8 is nearing its end of life.
It only receives critical security updates, no bug fixes.


BUILD FAILED (Ubuntu 20.04 using ruby-build 20200727)

Inspect or clean up the working tree at /tmp/ruby-build.20200728013413.16282.OsujhX
Results logged to /tmp/ruby-build.20200728013413.16282.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 27352: make: command not found
no
checking for safe null command for make... configure: error: no candidate for safe null command

またもエラーになってしまった。
今度はmakeがインストールされてないよ!というエラー。
※make:Linuxにおいてソースファイルをコンパイルするときに必須のコマンド。らしい。
ということで、makeもインストールしてみる。

$ sudo apt install make

例によってインストールログはテキストで残しておく。

今度こそ、rbenvでrubyをインストール。

$ rbenv install 2.5.8
The Ruby openssl extension was not compiled.
The Ruby readline extension was not compiled.
The Ruby zlib extension was not compiled.
Try running `apt-get install -y libssl-dev libreadline-dev zlib1g-dev` to fetch missing dependencies.

またもエラー、どうやら『openssl』と『readline』と『zlib』が入っていないのが原因らしい。
とりあえず指示通りに実行してみる。

$ apt-get install -y libssl-dev libreadline-dev zlib1g-dev

これは問題なく完了。再度rbenvでインストールを実行。

$ rbenv install 2.5.8
Downloading ruby-2.5.8.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.8.tar.bz2
Installing ruby-2.5.8...

WARNING: ruby-2.5.8 is nearing its end of life.
It only receives critical security updates, no bug fixes.

Installed ruby-2.5.8 to /home/kei/.rbenv/versions/2.5.8

インストールできた!

最後に、rbenvでバージョンを指定して完了。

$ rbenv global 2.5.8

バージョンも確認しておく。

$ ruby -v
ruby 2.5.8p224 (2020-03-31 revision 67882) [x86_64-linux]
3
3
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
3
3