Install and setup Rust development environment on WSL2

Rust aka rust-lang is gaining a lot of adoption in the community for building reliable and efficient software. The more I have used it, the more I like it. I use Mac or Linux for development mostly. I recently got a new Windows system after 5 years of using a MacOS. Windows has come a long way over the years since I was away. I followed the development in the space but never tried any of the new features until recently. One the major reasons that I wanted to try the windows machine is the new WSL2 (Windows Subsystem for Linux 2) that allows users to run a Linux VM inside windows and integrates nicely with many applications and gives a near native Linux experience. For setting up WSL2, see the official instructions from Microsoft here.

Installing rust on WSL2 is pretty straight forward.

  1. Start the WSL2 system by running your Linux App from the Start Menu. I use Ubuntu for WSL2.
Picking the Linux App from the Windows Start Menu

If you don’t have the Start Menu shortcut, you can run it from PowerShell or the command prompt using the command:

wsl

2. Once inside WSL2, run the following commands to install Rust:

sudo apt install build-essential # Install pre-reqs

Press 1 and then the enter key when prompted for installation options if you’d like to use the default options. The installation would something like this:

Rust installation progress on WSL2

You can verify the installation by running:

source $HOME/.cargo/env #Import the environment config for rust
rustc --version

You should see an output like `rustc 1.44.1 (c7087fe00 2020–06–17)`

Now that rust is installed, let’s setup our Rust development environment using Visual Studio Code.

  1. Install Visual Studio Code if you haven’t already.
  2. Install the WSL 2 and the Rust extension for VSCode by searching the extensions catalog.
VS Code extensions for WSL2 and Rust

3. In your WSL2 shell, run the following command to write a new Rust program using VS Code.

code main.rs

Write your program in VS Code and run the following commands in the terminal to compile and run the program.

rustc main.rs
./main
Run the Hello World Rust program on WSL2 using VS Code

Have fun with your new setup :)

You may also like to read about how to setup debugging for Rust on WSL2.

Personal views