rbenvをインストールするには

Rubyの複数バージョンをインストールしたり、切り替えたりするツールです。

何かをインストールするまでいろいろ大変ね!

Mac OS X

Mac OS X 10.15.4

brewでインストールできます。rbenvとruby-buildがインストールされます。

$ brew install rbenv

環境変数を追加します。筆者は .bash_profile 、.bashrcのファイル構成なので、.bashrc に追加しました。

6行全てをコピペして、ターミナルで実行してください。

cat <<'EOT'  >> ~/.bashrc

# rbenv
export PATH=$HOME/.rbenv/bin:$PATH
eval "$(rbenv init -)"
EOTCode language: Bash (bash)

いったんターミナルを閉じて、もう一度ターミナルを開きます。

$ rbenv --version
rbenv 1.1.2Code language: Bash (bash)

使い方

Ubuntu 18.04

rbenvとruby-buildの2つをgit cloneします。

$ git clone https://github.com/rbenv/rbenv.git      ~/.rbenv
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/pluginsCode language: PHP (php)

環境変数を.bashrc に追加します。6行全てをコピペして、ターミナルで実行してください。

cat <<'EOT'  >> ~/.bashrc

# rbenv
export PATH=$HOME/.rbenv/bin:$PATH
eval "$(rbenv init -)"
EOTCode language: Bash (bash)

いったんターミナルを閉じて、もう一度ターミナルを開きます。

$ rbenv --version
rbenv 1.1.2-28-gc2cfbd1Code language: Bash (bash)

rbenvのサブコマンドに install が表示されない場合は、ruby-buildが認識されていません。~/.rbenv/plugins/ruby-buildにgit cloneしているか確認してください。

$ rbenv
rbenv 1.1.2-28-gc2cfbd1
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
   install     Install a Ruby version using ruby-build
   uninstall   Uninstall a specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List installed Ruby versions
   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

Code language: Bash (bash)

使い方

使い方

インストール可能なバージョン一覧を表示します。

$ rbenv install --listCode language: Bash (bash)

特定のバージョンのrubyをインストールします。gemも同時にインストールされます。ここでは、2.7.1をインストールします。

$ rbenv install 2.7.1Code language: Bash (bash)

インストール済みのバージョンを表示します。systemは、rbenvインストール前からインストールされていた /usr/bin/ruby です。Max OS Xでは 2.6.3、Ubuntu 18.04では2.5.1です。

* system (set by /Users/aoki.makoto/.rbenv/version)
  2.7.1
Code language: Bash (bash)

global設定を2.7.1に切り替えます。

$ rbenv global 2.7.1

$ rbenv version
2.7.1 (set by /Users/aoki.makoto/.rbenv/version)

$ ruby --version
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]

$ gem --version
3.1.2

$ cat /Users/aoki.makoto/.rbenv/version
2.7.1Code language: Bash (bash)

別のディレクトリ、例えば ~/Documentsに移動して、local設定をsystemに切り替えます。

$ cd ~/Documents

$ rbenv local system

$ rbenv version
* system (set by /Users/aoki.makoto/Documents/.ruby-version)
  2.7.1

$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

$ gem --version
3.0.3

$ cat .ruby-version 
systemCode language: Bash (bash)

シバンを記述するときは、#!/usr/bin/env ruby とします。rbenv local、rbenv globalの設定にしたがって、rubyが起動します。

#!/usr/bin/env ruby

puts RUBY_VERSIONCode language: Ruby (ruby)
$ ./version.rb
2.7.1

シバンを#!/usr/bin/rubyとすると、rbenvインストール以前の/usr/bin/rubyが起動します。

#!/usr/bin/ruby

puts RUBY_VERSIONCode language: Ruby (ruby)
$ ./version.rb
2.6.3Code language: Bash (bash)
タイトルとURLをコピーしました