Windows 11 + WSL + VS Code + Rust Environment Setup

Introduction

You can set up a Rust environment on either Windows or Linux. After searching online, I found that setting up a Rust environment on Linux is much quicker than on Windows, but I still use the Windows system on a daily basis.

Therefore, I ultimately chose to set up the Rust environment in the WSL2 of Windows 11, and then use VS Code to connect to WSL.

Overall Steps

  1. Install WSL on Windows 11
  2. Change the source on WSL in Windows 11
  3. Install gcc on WSL in Windows 11
  4. Install Rust on WSL in Windows 11
  5. Install Remote-WSL on VS Code in Windows 11
  6. Install rust-analyzer on VS Code in Windows 11
  7. Simple verification
  8. Run and debug

Install WSL on Windows 11

Reference: https://learn.microsoft.com/en-us/windows/wsl/about

https://learn.microsoft.com/en-us/windows/wsl/install

Prerequisites:

image-20231126161500760

Open PowerShell or Windows Command Prompt as an administrator, and enter the wsl --install command to start installing WSL. By default, this will install the latest stable version of Ubuntu. You can manually choose other versions; see the Microsoft tutorial for specific instructions.

After the installation is complete, you need to restart your computer for the configuration to take effect.

PS: It might be because I had a VPN running all the time, but during the WSL installation, it kept prompting "wsl: Localhost proxy configuration detected, but not mirrored to WSL. Localhost proxy is not supported in NAT mode for WSL."

I'm not sure if this would affect the network of WSL, but to be safe, I followed the instructions to make some configurations.

In WSL, there is a configuration file used to configure advanced setting options, which is .wslconfig. This file doesn't exist by default; if you want to configure it, you need to create it manually.

This file is located at C:\Users\<UserName>\.wslconfig.

After creating the file, enter the following content and save it:

1
2
3
4
5
6
[experimental]
autoMemoryReclaim=gradual # gradual | dropcache | disabled
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

Then, open PowerShell and enter wsl --shutdown, and reopen WSL. The previous prompt should be gone.

Change the Source on WSL in Windows 11

Just like a normal Ubuntu system, WSL uses foreign sources by default, which are inconvenient for us to use. Therefore, we can change to domestic sources, and the method is the same as in the Ubuntu system.

PS: Make sure to confirm the system version installed in WSL first. Here, the default installation is Ubuntu 22.04.3 LTS.

Domestic Sources

Tsinghua University:

1
2
3
4
5
6
7
8
9
10
11
12
13
# Default commented out source code mirror to improve apt update speed, uncomment if needed
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

# Prerelease software source, not recommended to enable
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

Alibaba Cloud:

1
2
3
4
5
6
7
8
9
10
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

University of Science and Technology of China:

1
2
3
4
5
6
7
8
9
10
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

NetEase:

1
2
3
4
5
6
7
8
9
10
deb http://mirrors.163.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ jammy-backports main restricted universe multiverse

After choosing a domestic source, execute the following commands:

1
2
sudo apt-get update
sudo apt-get upgrade

Install gcc on WSL in Windows 11

Open WSL and run the following commands:

1
2
apt-get update # Make sure apt-get is up-to-date, as it may report an error when installing gcc
sudo apt install gcc

Install Rust on WSL in Windows 11

Two commands are enough:

1
sudo apt install rustc
1
sudo apt install cargo

After the installation is complete, run cargo version in the terminal.

If you see the following output, it means Rust has been installed successfully.

image-20231126163636737

Install Remote-WSL on VS Code in Windows 11

Currently, VS Code provides a plugin that allows you to connect to WSL2 from the Windows version of VS Code. This plugin is called Remote-WSL.

image-20231126163800014

So, as long as you install this plugin in the Windows version of VS Code, you can connect to WSL2.

The first time you connect, you may need to wait for some time, as it needs to perform some initialization work.

After the initialization is complete, click "Open Folder" to select a folder in WSL2.

image-20231126163942891

Install rust-analyzer on VS Code in Windows 11

After connecting VS Code to WSL, the installed plugins have two "versions": one for use on Windows, and one installed in WSL.

image-20231126164254143

Here, we only need one for now, which is rust-analyzer.

In the VS Code window connected to WSL, search for rust-analyzer and install the plugin.

Simple Verification

In WSL, navigate to any directory and enter cargo init hello-world.

Then, enter code . to open the Rust project in VS Code.

image-20231126164600855

In the VS Code terminal, run cargo run to verify if the environment is configured successfully. If successful, the output should look like the following:

image-20231126164727968

At this point, the basic Rust environment has been set up successfully.

Run and Debug

image-20231126170853688

After entering code . in WSL to open VS Code, click the "Run" button in the top-left corner of the code editor to execute the program.

To debug, you need to install the CodeLLDB plugin.

After installing the plugin, you can use the debugging functionality.

image-20231126171239141

Currently, the plugins installed on WSL are as follows:

image-20231126171146275

Windows 11 + WSL + VS Code + Rust Environment Setup
http://jingmengzhiyue.top/2024/03/11/rust-wal2-vscode/
作者
Jingmengzhiyue
发布于
2024年3月11日
许可协议