Skip to Content
Course content

1: Installing Rust and Cargo

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've just spun up a fresh development environment, and before we can actually write any code, we need the tooling. I usually start by checking if anything is already pre-installed on the system, just so I don't waste time downloading things I already have.

Testing the waters

$ rustc --version
bash: rustc: command not found: rustc

Right. Nothing. In some enterprise environments, you might find a version of Rust already there, but it's usually outdated. If it had returned something like rustc 1.40.0, I'd be worried because the Rust ecosystem moves fast. We want the latest stable release to ensure we have the newest language features and the best compiler optimizations.

Letting rustup take the wheel

Instead of downloading a binary manually from a website—which is a nightmare to update later—I use rustup. It's the toolchain manager for Rust. It doesn't just install the compiler; it lets me swap between stable, beta, and nightly versions of the language without breaking my system.

I'll run the standard installation script:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

I'm choosing the default installation here. It asks a few questions, but for 99% of us, the "Proceed with installation (default)" option is the right call. While it's running, it's grabbing rustc (the compiler), cargo (the build tool and package manager), and rustdoc (the documentation generator).

The "Command Not Found" dance

Now, I'll try to check the version again, expecting a victory.

$ rustc --version
bash: rustc: command not found: rustc

Wait, what? I just installed it. This is the part that trips people up. The installer puts the Rust binaries in a hidden folder in our home directory (~/.cargo/bin), but my current terminal session doesn't know that folder exists yet. The installer told me to "source" the environment, but I'll just do it manually to show you what's happening.

$ source $HOME/.cargo/env
$ rustc --version
rustc 1.76.0 (07d945517 2024-02-04)

There we go. Now the shell knows where to look. If you're on Windows, you'd usually just need to restart your PowerShell or Command Prompt for the PATH changes to kick in.

Hitting the linker wall

I have the compiler, but Rust doesn't work in a vacuum. It needs a "linker" to actually package the compiled code into an executable. To test if my system is actually ready, I'll try to create a tiny project using Cargo.

$ cargo new build_test
$ cd build_test
$ cargo build

On a fresh Linux install, this is where I usually hit a wall. I'm seeing a giant wall of red text ending in error: linker 'cc' not found. This happens because Rust leverages the system's C linker. If you don't have build-essential tools installed, Rust can compile the code but it can't "link" it into a final program.

I'll fix this by installing the base development tools for my OS. On Ubuntu/Debian, that's:

$ sudo apt update
$ sudo apt install build-essential

Now, let's try that build again.

$ cargo build
    Compiling build_test v0.1.0 (/home/user/build_test)
     Finished dev [unoptimized + debuginfo] target(s) in 0.42s

Success. We have rustup managing our versions, rustc doing the heavy lifting, and cargo orchestrating the whole process. We're officially ready to write some actual logic.




📋 Practical Task

Validating the Toolchain with a Cargo Project

To ensure your installation is fully functional and your system linker is configured correctly, perform the following steps:

  • Open your terminal and use Cargo to create a new project named toolchain_verify.
  • Navigate into the project directory.
  • Run the command cargo check. This command is faster than cargo build because it only verifies that the code compiles without actually producing a binary.
  • Run cargo run to compile and execute the default project code.
  • Confirm that you see the output Hello, world! in your terminal. If you encounter a "linker not found" error, install the build-essential (Linux) or C++ build tools (Windows) and try again.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.