😇

uv をインストールしたと思ったら [Command 'uv' not found] が出た時

に公開

結論

source ~/.bashrc

解説

今流行りの MCP (Model Context Protocol) を触ってみようと、チュートリアル を開始したところ、1つ目のコマンドでつまずきました。

user01@user01-ubuntu:~$ curl -LsSf https://astral.sh/uv/install.sh | sh
downloading uv 0.6.14 x86_64-unknown-linux-gnu
no checksums to verify
installing to /home/user01/.local/bin
  uv
  uvx
everything's installed!

To add $HOME/.local/bin to your PATH, either restart your shell or run:

    source $HOME/.local/bin/env (sh, bash, zsh)
    source $HOME/.local/bin/env.fish (fish)
user01@user01-ubuntu:~$
user01@user01-ubuntu:~$
user01@user01-ubuntu:~$ uv
Command 'uv' not found, but can be installed with:
sudo snap install astral-uv
user01@user01-ubuntu:~$

uv のインストールコマンドが成功したように見えますが、uv コマンドが見つからないというエラーが出ています。

インストールスクリプト https://astral.sh/uv/install.sh を確認したところ、デフォルトでは $HOME/.local/bin に uv がインストールされるようです。

user01@user01-ubuntu:~$ ls -l $HOME/.local/bin
total 40460
-rw-rw-r-- 1 user01 user01      328 Apr 14 22:22 env
-rw-rw-r-- 1 user01 user01      165 Apr 14 22:22 env.fish
-rwxr-xr-x 1 user01 user01 41044656 Apr  9 21:38 uv
-rwxr-xr-x 1 user01 user01   373296 Apr  9 21:38 uvx
user01@user01-ubuntu:~$

ありました。また、ここにある uv は実行可能なようです。

user01@user01-ubuntu:~$ $HOME/.local/bin/uv
An extremely fast Python package manager.

Usage: uv [OPTIONS] <COMMAND>

Commands:
  run      Run a command or script
  init     Create a new project
  add      Add dependencies to the project
  remove   Remove dependencies from the project
  sync     Update the project's environment
  lock     Update the project's lockfile
<省略>

とすると、PATH の問題である可能性が高いです。インストールスクリプトを確認すると、env ファイルで PATH を追加するように書いてあります。

write_env_script_sh() {
    # write this env script to the given path (this cat/EOF stuff is a "heredoc" string)
    local _install_dir_expr="$1"
    local _env_script_path="$2"
    ensure cat <<EOF > "$_env_script_path"
#!/bin/sh
# add binaries to PATH if they aren't added yet
# affix colons on either side of \$PATH to simplify matching
case ":\${PATH}:" in
    *:"$_install_dir_expr":*)
        ;;
    *)
        # Prepending path in case a system-installed binary needs to be overridden
        export PATH="$_install_dir_expr:\$PATH"
        ;;
esac
EOF
}

インストール直後は、この env ファイルが読み込まれていないので、PATH に $HOME/.local/bin が追加されていません。
したがって、解決方法は、 シェルの再起動 か、 env ファイルを読み込む ことです。

source ~/.bashrc

これで、uv コマンドが使えるようになりました。

user01@user01-ubuntu:~$ uv
An extremely fast Python package manager.

Usage: uv [OPTIONS] <COMMAND>

Commands:
  run      Run a command or script
  init     Create a new project
  add      Add dependencies to the project
  remove   Remove dependencies from the project
  sync     Update the project's environment
<省略>

ここで、改めてインストール時のメッセージを確認してみましょう。

user01@user01-ubuntu:~$ curl -LsSf https://astral.sh/uv/install.sh | sh
downloading uv 0.6.14 x86_64-unknown-linux-gnu
no checksums to verify
installing to /home/user01/.local/bin
  uv
  uvx
everything's installed!

To add $HOME/.local/bin to your PATH, either restart your shell or run:

    source $HOME/.local/bin/env (sh, bash, zsh)
    source $HOME/.local/bin/env.fish (fish)
user01@user01-ubuntu:~$

To add $HOME/.local/bin to your PATH, either restart your shell or run:

最初から答え書いてあるじゃん・・・出力はちゃんと読みましょう(戒め)

補足

uv とは、Rust で書かれた非常に高速な Python パッケージおよびプロジェクトマネージャーだそうです。

Discussion