Windows に Rust をインストールしようとしてネックになっていたのが、前提条件として Microsoft C++ Build Tools のインストールが必要なこと。ストレージの空き容量が 10GB 必要になる。ノート PC であんまり余裕がないので、これを避けたい。
そこで、もしかしたら節約できるのではと思って、WSL で構築済みの Ubuntu にインストールする方法を試してみることにした。
インストール
といっても、公式サイトのインストール手順に書かれているコマンドを実行するだけ。
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh info: downloading installer Welcome to Rust! This will download and install the official compiler for the Rust programming language, and its package manager, Cargo. Rustup metadata and toolchains will be installed into the Rustup home directory, located at: /home/<ユーザー名>/.rustup This can be modified with the RUSTUP_HOME environment variable. The Cargo home directory is located at: /home/<ユーザー名>/.cargo This can be modified with the CARGO_HOME environment variable. The cargo, rustc, rustup and other commands will be added to Cargo's bin directory, located at: /home/<ユーザー名>/.cargo/bin This path will then be added to your PATH environment variable by modifying the profile files located at: /home/<ユーザー名>/.profile /home/<ユーザー名>/.bashrc You can uninstall at any time with rustup self uninstall and these changes will be reverted. Current installation options: default host triple: x86_64-unknown-linux-gnu default toolchain: stable (default) profile: default modify PATH variable: yes 1) Proceed with installation (default) 2) Customize installation 3) Cancel installation >
ここで 1
を入力して Enter。
>1 info: profile set to 'default' info: default host triple is x86_64-unknown-linux-gnu info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' info: latest update on 2023-08-24, rust version 1.72.0 (5680fa18f 2023-08-23) info: downloading component 'cargo' info: downloading component 'clippy' info: downloading component 'rust-docs' 13.7 MiB / 13.7 MiB (100 %) 8.3 MiB/s in 4s ETA: 0s info: downloading component 'rust-std' 26.8 MiB / 26.8 MiB (100 %) 4.4 MiB/s in 11s ETA: 0s info: downloading component 'rustc' 63.7 MiB / 63.7 MiB (100 %) 3.0 MiB/s in 20s ETA: 0s info: downloading component 'rustfmt' info: installing component 'cargo' info: installing component 'clippy' info: installing component 'rust-docs' 13.7 MiB / 13.7 MiB (100 %) 326.4 KiB/s in 43s ETA: 0s info: installing component 'rust-std' 26.8 MiB / 26.8 MiB (100 %) 7.8 MiB/s in 11s ETA: 0s info: installing component 'rustc' 63.7 MiB / 63.7 MiB (100 %) 7.8 MiB/s in 7s ETA: 0s 9 IO-ops / 9 IO-ops (100 %) 0 IOPS in 34s ETA: Unknown info: installing component 'rustfmt' info: default toolchain set to 'stable-x86_64-unknown-linux-gnu' stable-x86_64-unknown-linux-gnu installed - rustc 1.72.0 (5680fa18f 2023-08-23) Rust is installed now. Great! To get started you may need to restart your current shell. This would reload your PATH environment variable to include Cargo's bin directory ($HOME/.cargo/bin). To configure your current shell, run: source "$HOME/.cargo/env"
メッセージの通り、シェルを開きなおせばパスが通る。
$ rustc --version rustc 1.72.0 (5680fa18f 2023-08-23)
ディスク使用量を確認する
インストール中のメッセージによれば $HOME/.cargo
と $HOME/.rustup
にファイルがインストールされるらしい。
$ du -m -d 1 1 ./.cache 14 ./.cargo 1219 ./.rustup 1233 .
合計で 1233MB。つまり 1.2GB。10GB をインストールしなくてよくなった。やったか!? (やってない。後述)
VS Code で編集する
WSL 上のディレクトリを、Windows 側にインストールした VS Code で編集できる。
VS Code には拡張機能の "WSL" をインストールしておく。
WSL のシェル上で code .
を実行すれば、そのディレクトリを VS Code で開くことができる。初回実行時に、VS Code と通信するためのサーバーが自動的にインストールされるようだ。
$ code . Installing VS Code Server for x64 (abd2f3db4bdb28f9e95536dfa84d8479f1eb312d) Downloading: 100% Unpacking: 100% Unpacked 1794 files and folders to /home/sardine/.vscode-server/bin/abd2f3db4bdb28f9e95536dfa84d8479f1eb312d.
Hello World をコンパイルしてみる
簡単なファイルを作って、
fn main() { println!("Hello World!"); }
コンパイルを試したところ、エラーに。
$ rustc hello.rs error: linker `cc` not found | = note: No such file or directory (os error 2) error: aborting due to previous error
Rust はプラットフォームのリンカーを利用するらしいけど、作ったばかりの Ubuntu 環境にはその辺が何も入っていなかった。
$ sudo apt install build-essential
インストール中の表示によれば
After this operation, 208 MB of additional disk space will be used.
ということなので、Rust 自体と合わせて 1.4GB くらいか。
再度コンパイルを試して、今度こそ成功。
$ rustc hello.rs $ ./hello Hello World!
ちなみに、出力された実行可能ファイルは 4,589,640 バイトだった。