LoginSignup
10
5

初心者の初心者による初心者のためのバージョン管理ツール rtx 入門

Last updated at Posted at 2023-02-16

はじめに

バージョン管理ツールといえば anyenv や asdf が有名ですが、最近になって asdf の Rust クローンである rtx というものが登場しました。私もこちらの記事で書いたように rtx を導入してみたので、いろいろいじりながらその使い方をまとめてみようと思います。

実行環境

  • WSL2 (Ubuntu)
  • fish shell
  • Homebrew 導入済み

ほかの環境での使い方もまとめるつもりですが一応。

rtx とは

以降公式ドキュメントおよび --help を参考にしていきます。

概要

rtx のことを公式では "Polyglot runtime manager (asdf rust clone)" すなわち「多言語対応ランタイムマネージャ(asdf rust クローン)」と説明しています。詳しくは後述しますが、asdf と互換性がありつつもより高速に動作するようなバージョン管理ツールを目指しているようです。

特徴

公式ドキュメントによると rtx には以下のような特徴があるとのことです。

  • asdf 互換:rtx は asdf プラグインと .tool-versions ファイルに互換性があり、そのまま使うことができます。
  • 多言語対応:あらゆる言語と互換性があるため、 nvm, nodenv, pyenv などがそれぞれどう動作するのか考える必要はありません。ただこれ一つを使えばいいのです。
  • 高速:rtx は Rust で書かれており、非常に高速です。asdf より 20 倍から 200 倍は速いです。
  • shim 無し:(asdf で用いられる)shim は問題を引き起こし、ノードを破壊し、オーバーヘッドを増やします。我々は shim を使用しません。
  • より良い UX:asdf は asdf plugin add や asdf install のように多数のおかしな UX における意思決定がなされています。我々は rtx を使いやすくするために配慮しています。
  • ファジーマッチングとエイリアス:asdf のように正確なバージョン番号を指定する必要はありません。
  • ワンコマンドインストール:各プラグインを手動でインストールする必要がなく、rtx install --all を実行するだけで、必要なプラグインを全てインストールしてくれます。

このように rtx は asdf よりも使いやすいツールであるということを標榜しています。

導入方法

ここでは公式ドキュメントにある導入方法をまとめていきたいと思います。

まず、前提として rtx が対応しているのは macOS および linux のみです。今のところ Windows には対応する予定はないようですので、Windows ユーザーは WSL を用いるべきでしょう。

インストール

スタンドアローン

パッケージマネージャを使わない場合はこちらの方法を用います。

sh
$ curl https://rtx.pub/install.sh | sh

シェルスクリプトを使いたくない場合は以下のコマンドを実行します。

sh
$ curl https://rtx.pub/rtx-latest-macos-arm64 > /usr/local/bin/rtx

rtx の置き場所はどこでもいいそうなので、適宜 ~/bin, /usr/local/bin, ~/.local/share/rtx/bin/rtx などと読み替えてください。また、ダウンロード先の URL もアーキテクチャ (x64, arm64) やプラットフォーム (macos, linux) に応じて適宜読み替えてください。

Homebrew

sh
$ brew install jdxcode/tap/rtx

Cargo

Cargo でソースからビルドする場合、こちらを使います。

sh
$ cargo install rtx-cli

cargo-binstall を用いるとより高速になります。

sh
$ cargo install cargo-binstall
$ cargo binstall rtx-cli

npm

sh
$ npm install -g @jdxcode/rtx

フルインストールせずに試したい場合は npx を使います。

sh
$ npx @jdxcode/rtx exec python@3.11 -- python some_script.py

Github Releases

Github から最新版を入手したい場合はこちらを使います。

sh
$ curl https://github.com/jdxcode/rtx/releases/download/v1.9.0/rtx-v1.9.0-linux-x64 | tar -xJv
$ mv rtx/bin/rtx /usr/local/bin

apt

Ubuntu/Debian でインストールする場合はこちらを使います。

sh
$ wget -qO - https://rtx.pub/gpg-key.pub | gpg --dearmor | sudo tee /usr/share/keyrings/rtx-archive-keyring.gpg 1> /dev/null
$ echo "deb [signed-by=/usr/share/keyrings/rtx-archive-keyring.gpg arch=amd64] https://rtx.pub/deb stable main" | sudo tee /etc/apt/sources.list.d/rtx.list # arm64 の場合
$ sudo apt update
$ sudo apt install -y rtx

dnf

Fedora, CentOS, Amazon Linux, RHEL やその他の dnf-based distributions でインストールする場合はこちらを使います。

sh
$ dnf install -y dnf-plugins-core
$ dnf config-manager --add-repo https://rtx.pub/rpm/rtx.repo
$ dnf install -y rtx

yum

sh
$ yum install -y yum-utils
$ yum-config-manager --add-repo https://rtx.pub/rpm/rtx.repo
$ yum install -y rtx

aur

Arch Linux でインストールする場合はこちらを使います。

sh
$ git clone https://aur.archlinux.org/rtx.git
$ cd rtx
$ makepkg -si

初期化スクリプトへの記入

各シェルに合わせて以下のコマンドを実行します。

bash
$ echo 'eval "$(rtx activate bash)"' >> ~/.bashrc
zsh
$ echo 'eval "$(rtx activate zsh)"' >> ~/.zshrc
fish
$ echo 'rtx activate fish | source' >> ~/.config/fish/config.fish

こちらのページによると、fish の場合 config.fish に以下の記述が必要とのことです。

config.fish
rtx activate fish | source
rtx hook-env -s fish | source  # プラグインのパスを追加する
rtx complete -s fish | source  # rtxの補完を追加

ほかのシェルでも同様の記述が必要なのか、検証でき次第追記したいと思います。

基本的な使い方

以下、断りのない限り私の環境での話となります。ほかの環境でも大きな違いはないと思いますが念のため。

バージョンの確認

まず、無事にインストールできたかの検証もかねて試しにバージョンを確認してみます。

sh
$ rtx --version
1.9.0 linux-x64 (built 2023-02-13)

このような表示が出れば成功です。

Python のインストール

ここでは Python をインストールしながら実際の使い方を見ていきます。rtx の使い方とはずれますが、こちらを参考にして事前に依存関係をインストールしておいてください。

プラグイン一覧の表示

インストール可能なプラグインを確認します。

sh
$ rtx plugins ls -a
1password-cli
R
act

...

zola
zoxide
zprint

ちなみに、rtx は asdf のプラグインを使用しているため、こちらからも一覧を確認できます。

Python プラグインのインストール(省略可)

Python プラグインをインストールします。

sh
$ rtx install -p python

インストール出来たらインストール可能な Python ランタイム(バージョン)を確認します。

sh
$ rtx ls-remote python
2.1.3
2.2.3
2.3.7
2.4.0
2.4.1

...

Python ランタイムのインストール

特定の Python ランタイム(バージョン)をインストールします。

sh
$ rtx install python@3.12-dev
⢁ Installing runtime: python@3.12-dev...

✓ Runtime python@3.12-dev installed
rtx: python@anaconda3-2022.05

なお、インストール可能な最新バージョン(執筆時点では Python 3.11.2)をインストールしたい場合は以下のコマンドを実行します。

sh
$ rtx install python@latest

また、事前に Python プラグインをインストールしていなかった場合、この時点で一緒にインストールされるので、Python プラグインを先にインストールしておかなくても大丈夫です。

インストールしたランタイムを確認してみます。

sh
$ rtx ls
   python 3.11.2
   python 3.12-dev

デフォルトランタイムの設定

特定のランタイムをグローバルに適用する場合、以下のコマンドを実行します。

sh
$ rtx global python@3.11.2

設定できたことを確認します。

sh
$ rtx ls
-> python 3.11.2             (set by ~/.tool-versions)
   python 3.12-dev

特定のランタイムを現在のディレクトリに適用する場合、以下のコマンドを実行します。

sh
$ rtx local python@3.11.2
sh
$ rtx ls
-> python 3.11.2             (set by <current_directory>/.tool-versions)
   python 3.12-dev

この際、現在のディレクトリに .tool-versions というファイルが生成されます。

.tool-versions
python 3.11.2

ランタイムをグローバルと同じにしたいときは .tool-versions を削除すれば良いです。

sh
$ rm .tool-versions

ランタイム・プラグインの削除

ランタイムの削除をしたい場合、以下のコマンドを実行します。

sh
$ rtx uninstall python@3.11.2
uninstalling python@3.11.2

プラグインの削除をしたい場合、以下のコマンドを実行します。

sh
$ rtx plugins uninstall python
uninstalling plugin: python

プラグインを削除するとランタイムも同時に削除されます。

コマンド一覧

最後に rtx -h と入力して rtx のコマンドを概観してみようと思います(rtx --help も同様)。

sh
$ rtx -h
Polyglot runtime manager (asdf rust clone)

Usage: rtx [OPTIONS] <COMMAND>

Commands:
  activate    Enables rtx to automatically modify runtimes when changing directory
  alias       Manage aliases [aliases: a]
  complete    generate shell completions
  current     Shows currently active, and installed runtime versions
  deactivate  disable rtx for current shell session
  direnv      Output direnv function to use rtx inside direnv
  doctor      Check rtx installation for possible problems.
  env         exports env vars to activate rtx in a single shell session [aliases: e]
  exec        execute a command with runtime(s) set [aliases: x]
  global      sets global .tool-versions to include a specified runtime [aliases: g]
  install     install a runtime [aliases: i]
  latest      get the latest runtime version of a plugin's runtimes
  local       Sets .tool-versions to include a specific runtime [aliases: l]
  ls          list installed runtime versions [aliases: list]
  ls-remote   list runtime versions available for install [aliases: list-remote]
  plugins     Manage plugins [aliases: p]
  settings    Manage settings
  uninstall   removes runtime versions
  version     Show rtx version
  help        Print this message or the help of the given subcommand(s)

Options:
  -v, --verbose...  Show installation output
  -h, --help        Print help (see more with '--help')
  -V, --version     Print version

Examples:
  rtx install nodejs@20.0.0       Install a specific node version
  rtx install nodejs@20.0         Install a version matching a prefix
  rtx local nodejs@20             Use node-20.x in current project
  rtx global nodejs@20            Use node-20.x as default

  rtx install nodejs              Install the version specified in .tool-versions
  rtx local nodejs                Use latest node in current directory
  rtx global system               Use system node everywhere unless overridden

  rtx x nodejs@20 -- node app.js  Run `node app.js` with PATH pointing to node-20.x

おわりに

ここまで rtx の使い方についてまとめてきましたが、ここで挙げた内容は基本的な部分のみなので、いずれより詳細なコマンドの内容や、config ファイルの書き方などについてもまとめられればと思います。rtx は公開されたばかりでまだほとんど情報が出回っていないので、今後ユーザーが増えることを期待します。

参考文献

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