用 asdf 來管理 Ruby 版本

我原本是用 rvm 來做版控,但後來想說用看看 asdf 好了

安裝 asdf 的步驟就先跳過了

首先要先做

1
asdf plugin-add ruby

這時可以需入

1
2
3
asdf plugin-list
#可以看到
ruby

之後就可以安裝你要的 ruby 版本了

1
asdf install ruby 2.6.3

安裝完畢後,可以輸入

1
2
3
4
asdf list
#可以看到已安裝的版本
ruby
2.6.3

由於已經把rvm刪除了,所以這時候在專案底下應該要跑

1
2
gem install bundler
bundle install

當然也可設定各環境的版本

1
2
3
asdf shell <name> <version> Set the package version in the current shell
asdf local <name> <version> Set the package local version
asdf global <name> <version> Set the package global version

但每個專案下使用的版本都不一樣
所以也必須告訴 asdf要使用哪個版本
那就必須在專案目錄底下新增 .tool-versions
在裡面設定版本
設定好後可以輸入

1
2
3
asdf current
#可以看到
ruby 2.5.7 (set by /Users/willychang/Documents/oakda/kakao-spree/.tool-versions)

或是在專案目錄底下新增

1
2
3
4
touch .asdfrc

#輸入
legacy_version_file = yes
1
2
3
asdf current
#這時候他會去找 .ruby-versions 裡面的版本
ruby 2.5.7 (set by /Users/willychang/Documents/oakda/kakao-spree/.ruby-versions)

但有時候在每個專案下已經指定版本了,也有以後的兩個檔案
但還是會出現

1
2
3
4
5
asdf: No version set for command rails
you might want to add one of the following in your .tool-versions file:

ruby 2.x.x
ruby 2.x.x

因為每個版本的套件都不同,這時候只要下,重新安裝

1
asdf reshim

詳細介紹可參考下列文章
asdf 官網
asdf githut
Introducing Asdf: The Extendable Version Manager - Stratus3D