これは、なにをしたくて書いたもの?
ツールのインストールにcargo installが載っているパターンを見かける機会が増えてきたので、バイナリークレートが
どのような場所にインストールされるのか確認しておこうかなということで。
環境
今回の環境はこちら。
$ rustup --version rustup 1.29.0 (28d1352db 2026-03-05) info: This is the version for the rustup toolchain manager, not the rustc compiler. info: the currently active `rustc` version is `rustc 1.95.0 (59807616e 2026-04-14)` $ cargo --version cargo 1.95.0 (f2d3ce0bd 2026-03-21)
cargo install
cargo installのドキュメントはこちら。
cargo install - The Cargo Book
cargo installは、Rustバイナリーをビルド、インストールするコマンドです。
ローカルにインストールされているバイナリークレートを管理するコマンド、ともされています。
インストール先も書かれていますね。
--rootオプションCARGO_INSTALL_ROOT環境変数- Cargoの設定値
install.root CARGO_HOME環境変数$HOME/.cargoディレクトリー
優先順位は上から順に高い、でしょうね。
cargo installを実行した時に、対象のパッケージがすでにインストールされており、かつそれが最新ではなかった場合は
Cargoはパッケージを再インストールするようです。
If the package is already installed, Cargo will reinstall it if the installed version does not appear to be up-to-date.
また以下のいずれかの値が変更された場合も、再インストールするようです。
- パッケージのバージョンおよびソース
- インストールされているバイナリー名のセット
- 選択しているフィーチャー
--profileオプション--targetオプション
このコマンドは、プロジェクトレベルではなくシステムまたはユーザーレベルで動作します。つまり、ローカルの設定は
無視され、設定は$CARGO_HOME/config.tomlを参照することになります。
This command operates on system or user level, not project level. This means that the local configuration discovery is ignored. Instead, the configuration discovery begins at $CARGO_HOME/config.toml.
使ってみる
それでは、cargo installを使ってみましょう。
ast-grepをお題にします。まずはインストール。
$ cargo install ast-grep
すると、依存するクレートがダウンロードされてきて、ビルドが始まります…。ビルド済みのクレートをダウンロードして
インストール…ではなく、ビルドはローカルで行うところがポイントですね。
インストール後は、パスも通ります。
$ ast-grep --version ast-grep 0.42.1
どこにインストールされたかというと、$HOME/.cargoディレクトリーです。
$ which ast-grep
$HOME/.cargo/bin/ast-grep
どうしてこのディレクトリーにパスが通っているかというと、Rustのインストール時に.bashrcに以下が追加されているから
ですね。
$ grep -F .cargo ~/.bashrc . "$HOME/.cargo/env"
中身はこんな内容です。
$HOME/.cargo/env
#!/bin/sh # rustup shell setup # affix colons on either side of $PATH to simplify matching case ":${PATH}:" in *:"$HOME/.cargo/bin":*) ;; *) # Prepending path in case a system-installed rustc needs to be overridden export PATH="$HOME/.cargo/bin:$PATH" ;; esac
ドキュメントの例に従って、ripgrepもインストールしてみます。
$ cargo install ripgrep
インストールされたバイナリークレートの一覧は、cargo install --listで確認できます。
$ cargo install --list ast-grep v0.42.1: ast-grep sg ripgrep v15.1.0: rg
ちなみに、$HOME/.cargo/binディレクトリーにはバイナリークレートのビルド結果が配置されています。
$ ll $HOME/.cargo/bin 合計 100808 drwxrwxr-x 2 xxxxx xxxxx 4096 5月 10 21:33 ./ drwxrwxr-x 4 xxxxx xxxxx 4096 5月 10 21:32 ../ -rwxrwxr-x 1 xxxxx xxxxx 54318448 5月 10 21:28 ast-grep* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 cargo -> rustup* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 cargo-clippy -> rustup* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 cargo-fmt -> rustup* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 cargo-miri -> rustup* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 clippy-driver -> rustup* -rwxrwxr-x 1 xxxxx xxxxx 27567112 5月 10 21:33 rg* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 rls -> rustup* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 rust-analyzer -> rustup* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 rust-gdb -> rustup* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 rust-gdbgui -> rustup* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 rust-lldb -> rustup* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 rustc -> rustup* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 rustdoc -> rustup* lrwxrwxrwx 1 xxxxx xxxxx 6 5月 10 21:16 rustfmt -> rustup* -rwxr-xr-x 1 xxxxx xxxxx 20838840 5月 10 21:16 rustup* -rwxrwxr-x 1 xxxxx xxxxx 503936 5月 10 21:28 sg*
インストール時にダウンロードされたクレートのソースコードは、$HOME/.cargo/registry/src/index.crates.io-[xxxx]という
ディレクトリーにあるみたいです。
インストールしたバイナリークレートをアンインストールする場合は、cargo uninstallです。
$ cargo uninstall ripgrep
Removing $HOME/.cargo/bin/rg
$ cargo uninstall ast-grep
Removing $HOME/.cargo/bin/ast-grep
Removing $HOME/.cargo/bin/sg
さて、インストール先を変えてみましょう。cargo installのヘルプを確認。
$ cargo install --help Install a Rust binary Usage: cargo install [OPTIONS] [CRATE[@<VER>]]... Arguments: [CRATE[@<VER>]]... Select the package from the given source Options: --version <VERSION> Specify a version to install --index <INDEX> Registry index to install from --registry <REGISTRY> Registry to use --git <URL> Git URL to install the specified crate from --branch <BRANCH> Branch to use when installing from git --tag <TAG> Tag to use when installing from git --rev <SHA> Specific commit to use when installing from git --path <PATH> Filesystem path to local crate to install from --root <DIR> Directory to install packages into -f, --force Force overwriting existing crates or binaries -n, --dry-run Perform all checks without installing (unstable) --no-track Do not save tracking information --list List all installed packages and their versions --message-format <FMT> Error format [possible values: human, short, json, json-diagnostic-short, json-diagnostic-rendered-ansi, json-render-diagnostics] --debug Build in debug mode (with the 'dev' profile) instead of release mode -v, --verbose... Use verbose output (-vv very verbose/build.rs output) -q, --quiet Do not print cargo log messages --color <WHEN> Coloring [possible values: auto, always, never] --config <KEY=VALUE|PATH> Override a configuration value -Z <FLAG> Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details -h, --help Print help Manifest Options: --ignore-rust-version Ignore `rust-version` specification in packages --locked Assert that `Cargo.lock` will remain unchanged --offline Run without accessing the network --frozen Equivalent to specifying both --locked and --offline Target Selection: --bin [<NAME>] Install only the specified binary --bins Install all binaries --example [<NAME>] Install only the specified example --examples Install all examples Feature Selection: -F, --features <FEATURES> Space or comma separated list of features to activate --all-features Activate all available features --no-default-features Do not activate the `default` feature Compilation Options: -j, --jobs <N> Number of parallel jobs, defaults to # of CPUs. --keep-going Do not abort the build as soon as there is an error --profile <PROFILE-NAME> Install artifacts with the specified profile --target [<TRIPLE>] Build for the target triple --target-dir <DIRECTORY> Directory for all generated artifacts --timings Output a build timing report at the end of the build Run `cargo help install` for more detailed information.
--rootオプションでインストール先が変えられるということでした。というわけで、ディレクトリーを指定してインストール。
$ cargo install --root ~/cargo-test ast-grep ripgrep
指定したディレクトリーが存在しない場合は、Cargoが作成します。
インストールが終了すると、最後に「PATHに追加すること」と言われます。
Summary Successfully installed ast-grep, ripgrep! warning: be sure to add `$HOME/cargo-test/bin` to your PATH to be able to run the installed binaries
今回は絶対パスを指定して実行してみましょう。
$ ~/cargo-test/bin/ast-grep --version ast-grep 0.42.1
$ tree ~/cargo-test $HOME/cargo-test └── bin ├── ast-grep ├── rg └── sg 2 directories, 3 files
指定したディレクトリー配下のファイルはこれだけです。
$ tree $HOME/cargo-test -a $HOME/cargo-test ├── .crates.toml ├── .crates2.json └── bin ├── ast-grep ├── rg └── sg 2 directories, 5 files
$HOME/cargo-test/.crates.tomlの内容は、こんな感じでした。
$HOME/cargo-test/.crates.toml
[v1] "ast-grep 0.42.1 (registry+https://github.com/rust-lang/crates.io-index)" = [ "ast-grep", "sg", ] "ripgrep 15.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = ["rg"]
指定のパスにインストールされたバイナリークレートを確認。
$ cargo install --list --root ~/cargo-test ast-grep v0.42.1: ast-grep sg ripgrep v15.1.0: rg
環境変数CARGO_INSTALL_ROOTで指定する場合は、こうでしょうか。
$ CARGO_INSTALL_ROOT=~/cargo-test cargo install ast-grep ripgrep
ちなみに、--rootオプションやCARGO_INSTALL_ROOT環境変数でインストール先を切り替えても、クレートの
ソースコードをダウンロードする先は$HOME/.cargo/registry/src/index.crates.io-[xxxx]のままから変わりませんでした。
ここを変えたい場合はCARGO_HOME環境変数を設定したりするのかなと思いますが、そこまではやらない気もします…。
というわけで、確認はこれくらいにしましょう。