LoginSignup
44
39

More than 3 years have passed since last update.

rbenvを使ってUbuntu 18.04にRubyをインストールする

Posted at

はじめに

UbuntuにRubyをインストールしたときの記録です。

各種バージョン

Ubuntu 18.04 LTS
Ruby 2.6.5

インストール方法

rbenvのインストール

rbenvはRubyのバージョン管理ツールです。rbenvを使ってRubyをインストールします。

まずは、関連パッケージをインストール


$ sudo apt update
$ sudo apt install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm5 libgdbm-dev

rbenvをGitHubからcloneする

$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv

~/.rbenv/bin にパスを通す

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc

rbenvをセットアップ

$ ~/.rbenv/bin/rbenv init

セットアップを自動で行うために.bashrcに追記。
メッセージには.bash_profileに追記するように出るが、GUI版Ubuntuの場合は.bashrcに追記。

$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc

シェルを再起動 or 以下のコマンドを実行。

$ source ~/.bashrc

rbenv-doctorを使ってrbenvの設定ができたか確認する。

$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash

ruby-buildがインストールされていない場合は、以下が表示される

Checking `rbenv install' support: not found
Unless you plan to add Ruby versions manually, you should install ruby-build.
Please refer to https://github.com/rbenv/ruby-build#installation

ruby-buildのインストール

GitHubからrbenvのプラグインとしてインストール

$ mkdir -p "$(rbenv root)"/plugins
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build

もう一度rbenv-doctorを実行する

$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash

これで、rbenv installを実行できるようになる

Rubyのインストール

インストールできるバージョンをチェック

$ rbenv install -l

バージョンを指定してインストール

$ rbenv install 2.6.5

Rubyのデフォルトバージョンを指定

$ rbenv global 2.6.5

確認して終了

$ ruby -v
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]

参考サイト

44
39
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
44
39