Skip to content
systemcoding.dev
RTOS

Install Zephyr and Build Blinky With west build

Install Zephyr 4.1 and its SDK with west on Linux or macOS, build the blinky sample for a Nucleo board, flash it, and learn what each command does.

7 min read
Summarize in AI
Flat illustration of RTOS tasks taking turns on a timeline, with the C, Rust and Espressif logos.

Zephyr is a real-time operating system with its own build tool, its own configuration language and a hardware description format, and the first hour with it is mostly setup. The setup is not hard, but it has five separate steps that each depend on the previous one, and a missed step produces an error far from the cause. This post walks through them on Linux or macOS for Zephyr 4.1 with the Zephyr SDK 0.17, then builds and flashes the blinky sample for an STM32 Nucleo-F401RE board with west build and west flash. For where this fits in the whole system, see the complete Zephyr getting-started guide. The authoritative procedure, which changes between releases, is the official Getting Started Guide; check it for your operating system.

What you are installing

Zephyr is not one download. A working setup has four parts.

  • The host tools: CMake, Ninja, Python, the device tree compiler and a few others, installed with your system's package manager.

  • west: Zephyr's meta-tool, a Python program that manages the workspace of repositories and wraps the build, flash and debug commands.

  • The Zephyr source tree: the kernel, drivers, boards and samples, fetched by west into a workspace directory.

  • The SDK: cross-compilers and host tools for the target architectures, installed with west sdk install.

Each step writes to a different place, and Python packages go into a virtual environment so that they do not disturb the system.

Step 1: host dependencies

On Ubuntu or Debian, install the tools the documentation lists:

install_deps.shShell
sudo apt update
sudo apt install --no-install-recommends git cmake ninja-build gperf \
  ccache dfu-util device-tree-compiler wget python3-dev python3-venv \
  python3-tk xz-utils file make gcc gcc-multilib g++-multilib libsdl2-dev libmagic1

On macOS with Homebrew:

install_deps_mac.shShell
brew install cmake ninja gperf python3 ccache qemu dtc libmagic wget openocd

The package lists in the official guide are the source of truth, and they change as the requirements do. You need CMake 3.20 or newer and a recent Python 3. Check them with cmake --version and python3 --version.

Step 2: a virtual environment and west

Create a directory for the workspace and a Python virtual environment inside it:

venv.shShell
mkdir ~/zephyrproject
python3 -m venv ~/zephyrproject/.venv
source ~/zephyrproject/.venv/bin/activate
pip install west

After activation your shell prompt shows the environment name. You must activate it in every new terminal before using west, which is the most common reason for west: command not found on a second day.

Step 3: get the source

west init creates the workspace and reads the manifest from the main Zephyr repository, and west update fetches every repository the manifest lists, which includes drivers' vendor libraries and takes some minutes:

get_source.shShell
west init ~/zephyrproject
cd ~/zephyrproject
west update
west zephyr-export
west packages pip --install

west zephyr-export registers the Zephyr CMake package so that build files find it. west packages pip --install installs the Python requirements of the tree into your environment; in older releases the same step was pip install -r zephyr/scripts/requirements.txt. If your west lacks the packages command, use the older form.

To choose a specific release instead of the main branch, check out the tag in the zephyr directory and run west update again:

pin_version.shShell
cd ~/zephyrproject/zephyr
git checkout v4.1.0
west update

Pinning matters because the main branch moves daily, and board names and options change between releases.

Step 4: the SDK

The SDK provides the cross toolchains and the host tools that the build uses. From the zephyr directory:

sdk.shShell
cd ~/zephyrproject/zephyr
west sdk install

By default the command downloads and installs the SDK for all architectures. To install only what you need and save disk space, pass the toolchain name, for example west sdk install --toolchains arm-zephyr-eabi for Arm Cortex-M targets. The command also sets up the CMake package so builds find it. The SDK is large, and its size per toolchain is listed on the releases page; it varies by version.

Step 5: build blinky

Blinky is a sample that blinks the board's first LED. It lives in samples/basic/blinky. From the zephyr directory:

build.shShell
cd ~/zephyrproject/zephyr
west build -p always -b nucleo_f401re samples/basic/blinky

The arguments mean:

ArgumentMeaning
-p alwaysPristine build: delete the build directory first, so stale configuration cannot leak in
-b nucleo_f401reThe target board, using Zephyr's board name
samples/basic/blinkyThe application source directory

West runs CMake to configure, which processes the board's devicetree and Kconfig, then Ninja to compile and link. On success the end of the output is a memory summary and the location of the images:

Text
Memory region         Used Size  Region Size  %age Used
           FLASH:       13124 B       512 KB      2.50%
             RAM:        4408 B        96 KB      4.49%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from /home/user/zephyrproject/zephyr/build/zephyr/zephyr.elf for board: nucleo_f401re

The sizes are an example, because they depend on the Zephyr version, the toolchain and the configuration. The results are in build/zephyr: zephyr.elf for debugging, zephyr.bin and zephyr.hex for flashing.

Step 6: flash it

Connect the Nucleo board by USB; its on-board ST-LINK appears as a debug probe. Then:

flash.shShell
west flash

West picks a flash runner from the board's configuration, OpenOCD or the STM32CubeProgrammer for this board, and runs it. If the runner is missing, west tells you which program to install. When it finishes, the green LED on the board blinks once a second. Nothing is printed on the serial console by blinky itself, so the LED is the evidence.

If flashing fails with a permission error on Linux, the udev rules for the probe are not installed. The SDK includes a script for the common probes, and the documentation describes where to run it.

What the sample does

The source is short, and worth reading once. The sample's src/main.c, in the form used by Zephyr 4.x, is:

samples/basic/blinky/src/main.cc
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>

#define SLEEP_TIME_MS   1000

#define LED0_NODE DT_ALIAS(led0)

static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);

int main(void)
{
    int ret;

    if (!gpio_is_ready_dt(&led)) {
        return 0;
    }

    ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
    if (ret < 0) {
        return 0;
    }

    while (1) {
        ret = gpio_pin_toggle_dt(&led);
        if (ret < 0) {
            return 0;
        }
        k_msleep(SLEEP_TIME_MS);
    }
    return 0;
}

Three parts are worth understanding.

DT_ALIAS(led0) asks the devicetree for the node that the board file aliased as led0. For the Nucleo-F401RE that is the user LED on pin PA5, described in the board's devicetree source. The application never mentions PA5; the same source builds on a board with a different LED.

GPIO_DT_SPEC_GET() turns that node's gpios property into a structure holding the GPIO controller, the pin and the flags, such as active-low. It is computed at compile time.

k_msleep() suspends the calling thread for the time given in milliseconds, and the scheduler runs something else, or the idle thread, in the meantime. main() runs in a thread of its own, the main thread, which the kernel starts after initialisation.

The configuration file prj.conf for the sample contains one line, enabling the GPIO driver:

prj.confText
CONFIG_GPIO=y

Without it, the driver is not compiled in and gpio_is_ready_dt() returns false.

Changing the board

To build the same source for another board, change -b and rebuild with -p always. The names come from the board list:

other_boards.shShell
west boards | grep -i nucleo_f4
west build -p always -b rpi_pico samples/basic/blinky

west boards lists every board the tree supports. In Zephyr 4.x, boards can have multiple SoCs or cores, and the target name then includes a qualifier after a slash, such as nrf52840dk/nrf52840; the board's documentation page gives the exact string.

Common failures

SymptomCause
west: command not foundThe virtual environment is not activated in this terminal
No board named 'xyz' foundA misspelt or renamed board; check west boards for your version
Zephyr SDK not found or toolchain errorwest sdk install not run, or the SDK location not registered
CMake error about a missing Python modulewest packages pip --install was skipped
Build works, LED does not blinkThe wrong board target, or the flash did not complete; check west flash output
Old errors after changing a settingReuse of the build directory; add -p always

Where to go next

You now have a working toolchain. The things to try next, in order: change SLEEP_TIME_MS and rebuild with west build (no -p needed for a source change), look at the generated devicetree in build/zephyr/zephyr.dts, and open the configuration with west build -t menuconfig. The guide linked at the top covers each of these and the kernel services in turn.

When this advice does not apply

On Windows the same steps apply with different package managers and shell commands; use the official guide's Windows section. If you build inside a container, the SDK and host tools come in an image, and the steps collapse into running the image. The board name, the SDK version and the command options in this post are those of Zephyr 4.1, and the project moves quickly, so check the documentation for your release.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.