Rust on Linux
Enter the new uncomfort zone with Linux and Rust. This post documents the process and will serve as a reference for later usage.
Setup a Linux box
Having another machine with Linux operation system is cool but not necessary. Welcome to the Windows Subsystem for Linux 2 (WSL 2).
I am on Windows 11, I follow the instruction from Microsoft Docs.
With the Terminal and Powershell installed, run the command
wsl
The terminal displays the help with possible commands. I want to understand them a bit before actually executing any command.
wsl --list --online
Displays all the distributions. The Ubuntu
is the default if none is specified. Let’s use the default. Remember to run as Administrator
# Explicitly specify the distribution for clarity
wsl --install --distribution Ubuntu
Nice! Installed and rebooted.
Create a folder (dir) to store code
$ mkdir code
Visual Studio Code with WSL
I am following the document here.
- Install Remote Development extension
- Navigate to the Ubuntu terminal, type
code .
. Magic begins
Rust on Linux
The Rust documentation is rich. I follow its programming book
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
After successfully installed Rust on Linux box, I need to restart the shell. It is to ensure that system understands the new environment variables. Otherwise, it does not understand the changes
# This will not recognize as a command
$ cargo
# Reset the profile/shell (bash shell)
$ source ~/.profile
# This will work
$ cargo
Write some code and struggle
fn main() {
println!("Hello Rust 101");
}
I got the first error "linker ‘cc’ not found"
Ok. Get it the linker
$ sudo apt install build-essential
Enjoy the fruit
# Compile the code
$ rustc ./main.rs
# Run it
$ ./main
Summary
So what have I accomplished so far? I have setup a new development environment which includes
- A Linux box running on Windows 11 using WSL 2
- Visual Studio Code remote development. It allows me to stay on the Windows and write code in the Linux box. It is neat and straightforward. VS Code is amazing
- Install and write a "hello word" rust application
What a great way to start a weekend!