5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Mac】まだ初期のrubyバージョン使用しているの?rbenvを使ってバージョンをコントロールしよう!

Last updated at Posted at 2019-06-22

#想定読者

  • 初期のRubyのバージョンを使用している人
  • rbenvを知らない人
  • 最新のRubyを触りたい人。

#得られるもの

  • 最新のRubyを使うことができる。(今回はRuby2.6.3を使用。)
  • バージョンコントロールの方法がなんとなくわかる。

#準備するもの

  • Homebrew
  • 学ぶ気持ち

#筆者のPCスペック
-macOS Mojave 10.14.5

#rbenvって何?
簡単に言うとRubyのバージョンを簡単に変えることができるツールです。
ただし、バージョンを変えるだけで実際にはRubyをインストールはしません。
Rubyもインストールする際はruby-buildもインストールする必要があります。

#初めに
最初にHomebrewがインストールされているか確認しましょう。
Homebrewの確認は下記のコマンドです。

$ brew -v
Homebrew 2.1.3
Homebrew/homebrew-core (git revision 66a23; last commit 2019-05-22)

※$マークはコピーしなくていいです。
上記が出なかったらこちらからインストールしましょう。

#手順
##rbenvとruby-buildのインストール
下記のコマンドを打ちましょう。

$ brew install rbenv ruby-build

実際に入っているか確認しましょう、

$ brew list
autoconf	openssl		pkg-config	rbenv		ruby-build
$ rbenv -v
rbenv 1.1.2
$ ruby-build --version
ruby-build 20190423

先ほどインストールした以外にもautoconfopensslpkg-config等入っていますが、今は気にしないでください。

実際にインストールはできているので、次はrbenvの実行後を反映させる為の準備を行なっていきます。

##rbenvを初期設定する。
現在rbenvでrubyをインストールしても実行結果は反映されません。
シェルにrubyはrbenvでインストールされたものを使う事を読み込ませる必要があります。

下記のコマンドを打って初期設定を済ませてください。

$ rbenv init
# Load rbenv automatically by appending
# the following to ~/.bash_profile:

eval "$(rbenv init -)"

$ echo eval "$(rbenv init -)" > ~/.bash_profile
$ source ~/.bash_profile

こちらで準備ができました。
次にバージョンの選択を行いましょう。

##バージョンを選択する。
下記のコマンドを打つと、rbenvが持っているRubyのバージョンを全て表示してくれます。

$ rbenv install -l
Available versions:
-----一部抜粋-----
  2.6.2
  2.6.3
  2.7.0-dev
  2.7.0-preview1
-----一部抜粋-----

今回は2.6.3を選択します。(2.7.0はありますが、安定版ではないので今回はインストールを行いません。)
選択すると、ruby-buildが自動的に起動し、インストールを行ってくれます。

$ rbenv install 2.6.3
ruby-build: use openssl from homebrew
Downloading ruby-2.6.3.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.3.tar.bz2
Installing ruby-2.6.3...
Installed ruby-2.6.3 to /Users/username/.rbenv/versions/2.6.3

##選択したバージョンを使用する
下記のコマンドでどのrubyをインストールしたか確認できます。
*が今使っているバージョンです。systemというのは初期のバージョンを使用していることを指します。

$ rbenv versions
* system (set by /Users/username/.rbenv/version)
  2.6.3

今回は新しくインストールした"2.6.3"を使用してみましょう。

$ rbenv global 2.6.3
$ rbenv version
  system
* 2.6.3 (set by /Users/username/.rbenv/version)

実際にバージョンを確認してみましょう。

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]

これで無事にインストールできました!
違うバージョンを使用する際は再度バージョンを選択→インストール→選択する
を繰り返す事でバージョンを自由に変更することができます。

5
7
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
5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?