LoginSignup
19
20

More than 5 years have passed since last update.

Debian 9 Stretch に rbenv をインストールして Ruby の最新バージョンを使う

Posted at

Debian 9 Stretch 環境を用意

今回は macOS 上で Docker for Mac を使用して Debian 9 Stretch 環境を用意する。

Docker For Mac | Docker から Docker For Mac をインストールして、以下のコマンドで Docker コンテナを起動してログインする。

$ docker run --interactive --tty --detach --name foo debian:stretch
$ docker exec --interactive --tty foo bash --login

Debian パッケージをインストールするので apt update コマンドでパッケージ情報を更新しておく。

# apt update

参考: library/debian - Docker Hub

rbenv のインストール

Debian の rbenv パッケージをインストールする。

# apt install rbenv

参考: GitHub - rbenv/rbenv: Groom your app’s Ruby environment, Debian -- stretch の rbenv パッケージに関する詳細

rbenv をインストールしただけでは Ruby をインストールできない

試しに実行してみると rbenv install が見つからない。

# rbenv install
rbenv: no such command `install'

ヘルプコマンドを打っても rbenv install が載っていない。

# rbenv -h
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List all Ruby versions available to rbenv
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme

rbenv だけでは Ruby をインストールすることはできない。 Ruby のソースコードをダウンロードしてコンパイルするプログラムである ruby-build が必要となる。

ruby-build のインストール

Debian にも ruby-build パッケージはあるが、それでは Ruby の最新バージョンをインストールできない。そのため GitHub リポジトリにある最新の ruby-build を持ってくる。

Debian の git パッケージをインストールする。

# apt install git

GitHub リポジトリにある最新の ruby-build をダウンロードして rbenv の plugins/ruby-build ディレクトリに置く。

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

参考: GitHub - rbenv/ruby-build: Compile and install Ruby

Ruby をインストールするために必要なパッケージのインストール

Ruby のソースコードをダウンロードするために必要な Debian パッケージをインストールする (curl または wget)。

# apt install wget

参考: ruby-build/ruby-build at master · rbenv/ruby-build · GitHub

Ruby のソースコードをコンパイルするために必要な Debian パッケージをインストールする。
ruby-build は、コンパイルの前にシステムの依存関係をチェックしないとのこと。必要なライブラリを先にインストールしておく必要がある。

# apt install gcc-6 autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev

参考: Home · rbenv/ruby-build Wiki · GitHub

Ruby 最新バージョンのインストール

インストール可能なバージョン一覧を確認する。

# rbenv install --list

バージョンを指定してインストールする。今回は現時点で最新の 2.4.2 をインストールする (2017-09-15現在)。

# rbenv install 2.4.2
Downloading ruby-2.4.2.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.2.tar.bz2
Installing ruby-2.4.2...
Installed ruby-2.4.2 to /root/.rbenv/versions/2.4.2

インストールできているか確認する。

# rbenv versions
* system (set by /root/.rbenv/version)
  2.4.2

使用する Ruby のバージョンを指定する。

# rbenv global 2.4.2

# rbenv versions
  system
* 2.4.2 (set by /root/.rbenv/version)

PATH を設定する

rbenv で指定したバージョンの Ruby を実行するためには $HOME/.rbenv/shims の実行パス設定が必要。

# export PATH="$(rbenv root)/shims:$PATH"

# ruby --version
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]

ログインするたびに手動で設定するのは不便なので、ログイン時に設定されるように書いておくと便利。

# echo 'export PATH="$(rbenv root)/shims:$PATH"' >> ~/.bash_profile
# echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

(rbenv init は今回の環境では無くても動作した。Debian の rbenv パッケージを使っているからだろうか)

ruby-build のアップデート

Ruby の新しいバージョンがリリースされたときは、 ruby-build を更新する。

$ cd "$(rbenv root)"/plugins/ruby-build
$ git pull

これによって、また新しいバージョンをインストールすることができる。

# rbenv install x.x.x
19
20
4

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
19
20