帰宅後科学者

オタクですが何か?

使用 asdf 統一管理程式語言的版本

當我們要安裝語言工具到電腦上,通常有幾種方法:

  • 編譯原始碼
  • 直接下載 binary
  • 透過 apt 等套件管理工具安裝
  • 透過 nvm 、 rvm 等版本管理工具安裝

但如果我想安裝多個語言,又想輕鬆地切換版本,也想要統一管理所有語言的版本的話,這時候 asdf 就是一個好選擇。

安裝 asdf

asdf 官方提供的文件已經有相當豐富的說明了,以下我做個整理。

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.6.3
echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.bashrc
echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc

如此 asdf 就會安裝到你的家目錄底下的 .asdf 了,試試看 asdfCLI

$ asdf
version: v0.6.0

MANAGE PLUGINS
  asdf plugin-add <name> [<git-url>]   Add a plugin from the plugin repo OR, add a Git repo 
                                       as a plugin by specifying the name and repo url
  asdf plugin-list                     List installed plugins
  asdf plugin-list --urls              List installed plugins with repository URLs
  asdf plugin-list-all                 List plugins registered on asdf-plugins repository with URLs
  asdf plugin-remove <name>            Remove plugin and package versions
  asdf plugin-update <name>            Update plugin
  asdf plugin-update --all             Update all plugins


MANAGE PACKAGES
  asdf install [<name> <version>]      Install a specific version of a package or,
                                       with no arguments, install all the package
                                       versions listed in the .tool-versions file
  asdf uninstall <name> <version>      Remove a specific version of a package
  asdf current                         Display current version set or being used for all packages
  asdf current <name>                  Display current version set or being used for package
  asdf where <name> <version>          Display install path for an installed version
  asdf which <name>                    Display install path for current version
  asdf local <name> <version>          Set the package local version
  asdf global <name> <version>         Set the package global version
  asdf list <name>                     List installed versions of a package
  asdf list-all <name>                 List all versions of a package


UTILS
  asdf reshim <name> <version>         Recreate shims for version of a package
  asdf update                          Update asdf to the latest stable release
  asdf update --head                   Update asdf to the latest on the master branch


"Late but latest"
-- Rajinikanth

看到這個畫面表示安裝成功了,接下來試著安裝 Golang 看看。

安裝 Golang

首先安裝 Golangasdf plugin:

$ asdf plugin-add golang
Cloning into '/home/mike/.asdf/plugins/golang'...
remote: Enumerating objects: 94, done.
remote: Total 94 (delta 0), reused 0 (delta 0), pack-reused 94
Unpacking objects: 100% (94/94), done.

確認一下目前所有 asdf plugin:

$ asdf plugin-list
golang

再來確認目前有哪些 Golang 的版本可以安裝:

$ asdf list-all golang
1.2.2
1.3
...
1.11.5
1.12beta1
1.12beta2
1.12rc1

接下來安裝 1.11.5 版的 Golang

$ asdf install golang 1.11.5
Platform 'linux' supported!
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  133M  100  133M    0     0  1403k      0  0:01:37  0:01:37 --:--:-- 1478k

再來必須指定要使用的 Golang 版本:

$ asdf global golang 1.11.5
$ go version
go version go1.11.5 linux/amd64

這樣就成功囉!接下來試著升級我們的 Golang 版本:

$ asdf install golang 1.12rc1
Platform 'linux' supported!
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  121M  100  121M    0     0  1426k      0  0:01:27  0:01:27 --:--:-- 1396k

然後重新指定版本:

$ asdf global golang 1.12rc1
$ go version
go version go1.12rc1 linux/amd64

過程非常輕鬆,其他語言也適用,目前 asdf 提供的所有語言可以在這邊查詢。 asdf 的 plugin 是由整個開源社群共同維護的,你也可以盡自己的一份力讓 asdf 支援更多語言喔!