Close

Installing a Rust toolchain

A project log for A RISCy move

Moving development to the RISC-V series of MCUs

ken-yapKen Yap 7 days ago0 Comments

This log will be amended as I fill in details that I may have missed.

Your OS may have Rust available in their package repositories. But due to the rapid pace of development it's best to install a Rust toolchain outside of the package system. Rust developers have streamlined this task.

You need two important programs: rustup which is a toolchain installer (and maintainer), and cargo, installed by rustup, which is a project manager. The other programs like rustc, the compiler, will be installed by rustup.

Before you install rustup, you need to decide if you accept the standard location for the software, in the case of Linux, under your home directory in ~/.cargo, as explained here. If like me, you dislike lots of package files under home (unfortunately this is the norm these days with Arduino, PlatformIO, etc. etc.), you can change the directories. There are two environment variables, and I've set them thus:

export RUSTUP_HOME=/usr/local/lib/rustup CARGO_HOME=/usr/local/lib/cargo

If you choose to change them, edif your ~/.bash_profile to export these environment variables. Also put this command in ~/.bash_profile after so that the correct executables are found.

. "$CARGO_HOME/env"

Logout and login again if necessary to ensure that these settings are in place before you install rustup following the instructions at the website.

The help text for rustup indicates what it does:

$ rustup --help
rustup 1.29.0 (28d1352db 2026-03-05)

The Rust toolchain installer

Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]

Commands:
  install      Install or update the given toolchains, or by default the active toolchain
  uninstall    Uninstall the given toolchains
  toolchain    Install, uninstall, or list toolchains
  default      Set the default toolchain
  show         Show the active and installed toolchains or profiles
  update       Update Rust toolchains and rustup
  check        Check for updates to Rust toolchains and rustup
  target       Modify a toolchain's supported targets
  component    Modify a toolchain's installed components
  override     Modify toolchain overrides for directories
  run          Run a command with an environment configured for a given toolchain
  which        Display which binary will be run for a given command
  doc          Open the documentation for the current toolchain
  man          View the man page for a given command
  self         Modify the rustup installation
  set          Alter rustup settings
  completions  Generate tab-completion scripts for your shell
  help         Print this message or the help of the given subcommand(s)

Arguments:
  [+toolchain]  Release channel (e.g. +stable) or custom toolchain to set override

Options:
  -v, --verbose  Set log level to 'DEBUG' if 'RUSTUP_LOG' is unset
  -q, --quiet    Disable progress output, set log level to 'WARN' if 'RUSTUP_LOG' is unset
  -h, --help     Print help
  -V, --version  Print version

Discussion:
  Rustup installs The Rust Programming Language from the official
  release channels, enabling you to easily switch between stable,
  beta, and nightly compilers and keep them updated. It makes
  cross-compiling simpler with binary builds of the standard library
  for common platforms.

  If you are new to Rust consider running `rustup doc --book` to
  learn Rust.

Common commands:

  Update Rust toolchains and rustup

    $ rustup update
    
  Install the current stable release of Rust for your host platform

    $ rustup toolchain install stable

Toolchain means a release channel, e.g. default, nightly. If you are thinking about MCU targets, then the term is target.

Here's a relevant fact: gcc requires a different toolchain for each MCU family. That's why the toolchain for the classic Arduino starts with avr-, that for the STM32 starts with arm-, and for the WCH32V series riscv-. LLVM however uses the same compiler for all targets, and you can install a new target without installing another compiler. Here are the targets I have installed:

$ rustup target list | grep installed
riscv32i-unknown-none-elf (installed)
thumbv6m-none-eabi (installed)
x86_64-unknown-linux-gnu (installed)

Riscv32i is for the WCH32V MCUs, thumbv6m is for the RP2040, and x86_64 is for my Linux workhorse. 

Now we come to cargo. This is a project manager which allows you to manage all aspects of a project, initialising the project directory structure, installing dependencies, building binaries, running them, debugging them, installing them, and so forth. It's modelled after the successful Ruby on Rails tool rails which triggered similar programs for other development environments, like gradle for Groovy (java). Here is the help text for cargo which gives you an idea of what it does:

$ cargo --help
Rust's package manager

Usage: cargo [+toolchain] [OPTIONS] [COMMAND]
       cargo [+toolchain] [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]...

Options:
  -V, --version                  Print version info and exit
      --list                     List installed commands
      --explain <CODE>           Provide a detailed explanation of a rustc error message
  -v, --verbose...               Use verbose output (-vv very verbose/build.rs output)
  -q, --quiet                    Do not print cargo log messages
      --color <WHEN>             Coloring [possible values: auto, always, never]
  -C <DIRECTORY>                 Change to DIRECTORY before doing anything (nightly-only)
      --locked                   Assert that `Cargo.lock` will remain unchanged
      --offline                  Run without accessing the network
      --frozen                   Equivalent to specifying both --locked and --offline
      --config <KEY=VALUE|PATH>  Override a configuration value
  -Z <FLAG>                      Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
  -h, --help                     Print help

Commands:
    build, b    Compile the current package
    check, c    Analyze the current package and report errors, but don't build object files
    clean       Remove the target directory
    doc, d      Build this package's and its dependencies' documentation
    new         Create a new cargo package
    init        Create a new cargo package in an existing directory
    add         Add dependencies to a manifest file
    remove      Remove dependencies from a manifest file
    run, r      Run a binary or example of the local package
    test, t     Run the tests
    bench       Run the benchmarks
    update      Update dependencies listed in Cargo.lock
    search      Search registry for crates
    publish     Package and upload this package to the registry
    install     Install a Rust binary
    uninstall   Uninstall a Rust binary
    ...         See all commands with --list

See 'cargo help <command>' for more information on a specific command.

If you're thinking what does package have to do with my project, your project is a package too, and cargo is how you manage it. Crate is the Rust term for a standard package.

Fun fact: Look at the executables in $CARGO_HOME/bin. You'll see that a lot of them are just links to rustup, including cargo and rustc. Rustup is a chameleon program, playing different roles depending on how it's invoked.

(To be continued)

Discussions