BioErrorLog Tech Blog

試行錯誤の記録

GitHubレポジトリから直接cargo installする | Rust

RustツールをGitHubレポジトリから直接cargo installする方法の備忘録です。

はじめに

こんにちは、@bioerrorlogです。

Rustで書いたCLIツールを、crates.ioに公開せずにさっとGitHubレポジトリから直接インストールしたいことがあります。

このやり方の備忘録を残します。

GitHubレポジトリから直接cargo installする

やり方

cargo install --git <url>

--gitオプションでレポジトリURLを指定すれば、gitレポジトリから直接インストールできます。

シンプルでわかりやすいですね。

参考:The Cargo Bookより

--git url
    Git URL to install the specified crate from.

具体例

練習で書いたgrrsというRust製ツール(レポジトリ名はrust-grep)をGitHubレポジトリ経由でインストールしてみます。

github.com

cargo install --git https://github.com/bioerrorlog/rust-grep
#     Updating git repository `https://github.com/bioerrorlog/rust-grep`
#   Installing grrs v0.1.0 (https://github.com/bioerrorlog/rust-grep#88d46c52)
#     Updating crates.io index
#   Downloaded proc-macro2 v1.0.47
#   Downloaded anyhow v1.0.66
#   Downloaded syn v1.0.103
#   Downloaded unicode-ident v1.0.5
#   Downloaded libc v0.2.135
#   Downloaded 5 crates (962.3 KB) in 1.39s
#    Compiling proc-macro2 v1.0.47
#    Compiling version_check v0.9.4
#    Compiling quote v1.0.21
#    Compiling unicode-ident v1.0.5
#    Compiling syn v1.0.103
#    Compiling libc v0.2.135
#    Compiling autocfg v1.1.0
#    Compiling os_str_bytes v6.3.0
#    Compiling heck v0.4.0
#    Compiling hashbrown v0.12.3
#    Compiling anyhow v1.0.66
#    Compiling termcolor v1.1.3
#    Compiling once_cell v1.15.0
#    Compiling bitflags v1.3.2
#    Compiling textwrap v0.15.1
#    Compiling strsim v0.10.0
#    Compiling clap_lex v0.2.4
#    Compiling proc-macro-error-attr v1.0.4
#    Compiling proc-macro-error v1.0.4
#    Compiling indexmap v1.9.1
#    Compiling atty v0.2.14
#    Compiling clap_derive v3.2.18
#    Compiling clap v3.2.22
#    Compiling grrs v0.1.0 (/home/bioerrorlog/.cargo/git/checkouts/rust-grep-9a4bb816a8855267/88d46c5)
#     Finished release [optimized] target(s) in 3m 05s
#   Installing /home/bioerrorlog/.cargo/bin/grrs
#    Installed package `grrs v0.1.0 (https://github.com/bioerrorlog/rust-grep#88d46c52)` (executable `grrs`)


インストールされたことを確認します:

grrs --help
# grrs 
#
# USAGE:
#     grrs <PATTERN> <PATH>
#
# ARGS:
#     <PATTERN>    
#     <PATH>       
#
# OPTIONS:
#     -h, --help    Print help information

which grrs
# /home/bioerrorlog/.cargo/bin/grrs

無事インストールされました。

補足:アンインストール方法

アンインストールは、通常通りのcargo uninstallコマンドで行います。

cargo uninstall <package name>

先ほどのgrrsの例ですと、下記コマンドでアンインストールできます:

cargo uninstall grrs
#     Removing /home/bioerrorlog/.cargo/bin/grrs

# アンインストールされたことを確認
grrs --help
# bash: /home/bioerrorlog/.cargo/bin/grrs: No such file or directory

おわりに

以上、RustツールをGitHubレポジトリから直接cargo installする方法でした。

Rustの人気は幅広い領域で高まっているように感じます。
自分も土地勘を得ていきたいところです。

[関連記事]

www.bioerrorlog.work

www.bioerrorlog.work

www.bioerrorlog.work

参考

cargo install - The Cargo Book

cargo uninstall - The Cargo Book

Packaging and distributing a Rust tool - Command Line Applications in Rust