Introduction
The Zephyr OS is based on a small-footprint kernel designed for use on resource-constrained and embedded systems.
Goal
This article is a step-by-step guide to get started with Zephyr OS. We will create reproducible Zephyr builds in a clean Ubuntu container. After that, we will build and run hello_world on QEMU.
Preperations
Docker
- Docker File
# syntax=docker/dockerfile:1.7
ARG RELEASE=24.04
FROM ubuntu:${RELEASE}
ARG DEBIAN_FRONTEND=noninteractive
ARG USER=ugur
ARG UID=1000
ARG GID=1000
LABEL org.opencontainers.image.ref.name="ubuntu" \
org.opencontainers.image.version="${RELEASE}"
RUN apt-get update && \
apt-get install -y --no-install-recommends \
wget xz-utils cmake build-essential gdb \
python3 python3-dev python3-pip python3-setuptools \
python3-tk python3-wheel python3.12-venv xz-utils \
file make gcc libsdl2-dev libmagic1 ninja-build \
ca-certificates tzdata locales sudo passwd vim git openssh-client && \
rm -rf /var/lib/apt/lists/*
# locale (isteğe bağlı)
RUN sed -i 's/# *en_US.UTF-8/en_US.UTF-8/' /etc/locale.gen && locale-gen
ENV LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 TERM=xterm-256color
# kullanıcı ve çalışma alanı
RUN set -eux; \
if ! getent group "${GID}" >/dev/null; then \
groupadd -g "${GID}" "${USER}"; \
fi; \
useradd -m -u "${UID}" -g "${GID}" -s /bin/bash "${USER}"; \
usermod -aG sudo "${USER}"; \
mkdir -p /home/${USER}/workspace; \
chown -R "${UID}:${GID}" /home/${USER}
# Embed password as a hash
ARG PASS_HASH
RUN echo "${USER}:${PASS_HASH}" | chpasswd -e
# prompt
RUN printf "PS1='\\[\\e[32m\\]\\u\\[\\e[00m\\]:\\[\\e[33m\\]\\w\\[\\e[00m\\]\\\\n$ '\n" >> /etc/bash.bashrc
WORKDIR /home/${USER}/workspace
USER ${USER}
CMD ["bash"]
- Build Docker Image
# Dockerfile tips:
# - include all Zephyr deps and QEMU
# - minimize layers and clean apt cache
$ docker build -t ugur_ubuntu:24.04 \
--build-arg RELEASE=24.04 \
--build-arg USER=ugur \
--build-arg UID="$(id -u)" \
--build-arg GID=1000 \
--build-arg PASS_HASH="$(openssl passwd -6 '<password>')" \
.
Notes:
- Replace
<password>with your desired password. - Replace
ugurwith your desired username. - Replace
ugur_ubuntuwith your desired image name.
- Run Docker Container
$ docker run --rm -it \
-v <workspace_path>:/home/ugur/workspace \
--name zephyr_lab ugur_ubuntu:24.04
Notes:
- Replace
<workspace_path>with your workspace path. - The bind mount path must exist on the host.
- Matching UID/GID avoids permission issues on the mounted tree.
Python Virtual Environment
$ mkdir -p ~/workspace/zephyr_env && cd ~/workspace/zephyr_env
$ python3 -m venv .venv
$ source .venv/bin/activate
$ python -m pip install -U pip
Zephyr SDK
(.venv) $ pip install west pyelftools jsonschema
(.venv) $ git clone https://github.com/zephyrproject-rtos/zephyr.git
Workspace layout after this step:
workspace/
├── zephyr-sdk-0.17.4/ # Zephyr SDK
└── zephyr_env/ # Python virtual environment
└── zephyr/ # Zephyr source code
Notes:
- Latest Zephyr SDK version: 0.17.4, it may change over time.
Let's initialize West (local manifest)
(.venv) $ cd zephyr
(.venv) $ west init -l .
(.venv) $ west update
Notes:
- "-l ." must run inside the Zephyr repo where west.yml lives.
- west update fetches modules into the workspace (siblings of zephyr).
Zephyr SDK Path
(.venv) $ export ZEPHYR_SDK_INSTALL_DIR=/home/ugur/workspace/zephyr-sdk-0.17.4
(.venv) $ export PATH="$ZEPHYR_SDK_INSTALL_DIR/bin:$PATH"
Notes:
- Adjust the version to what you installed.
- Path must be inside the container filesystem.
Build and Run Hello World
(.venv) $ west build -b qemu_cortex_m3 samples/hello_world
(.venv) $ ls -al build/zephyr/zephyr.elf
(.venv) $ west build -t run
Expected output includes:
*** Booting Zephyr OS ...
Hello World! qemu_cortex_m3
Re-start Container
$ docker stop zephyr_lab
$ docker start -ai zephyr_lab
Common Issues
- west init -l . fails: you are not in the Zephyr repo root. cd zephyr_env/zephyr first.
- ZEPHYR_SDK_INSTALL_DIR ignored: verify path and that the SDK is inside the container, not only on the host.
- Permissions on mounted dir: ensure container user UID/GID matches host or chown the mount.
- Re-run cleanly: rm -rf build or west build -p always.