Fetch the Code to Build

Once your build tools are installed, fetch the CircuitPython source code from its GitHub repository ("repo") and also fetch the git "submodules" it needs. The submodules are extra code that you need that's stored in other repos.

In the commands below, you're cloning from Adafruit's CircuitPython repo. But if you want to make changes, you might want to "fork" that repo on GitHub to make a copy for yourself, and clone from there. For more information about using GitHub and forking a repo, see the Contribute to CircuitPython with Git and GitHub guide.

If you are on macOS and you had to install the brew version of make, use gmake in all examples below instead of make.

git clone https://github.com/adafruit/circuitpython.git
cd circuitpython

Install Required Python Packages

After you have cloned the repo, you'll need to install some Python packages that are needed for the build process. You only need to do this the first time, though you may want to run this again from time to time to make sure the packages are up to date.

# Install pip if it is not already installed (Linux only). Try running pip first.
sudo apt install python3-pip

# Install needed Python packages from pypi.org.
pip3 install --upgrade -r requirements-dev.txt
pip3 install --upgrade -r requirements-doc.txt

The --upgrade flag will force installation of the latest packages, except where a version is explicitly specified in the requirements-*.txt files.

If you get an error indicating that rust is needed to install minify-html, install rust:

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

# You can verify the installation by running:

source $HOME/.cargo/env #Import the environment config for rust
rustc --version

Checking out a Specific Branch or Version

If you want to build a version other the latest, checkout the branch or tag you want to build. For example:

# Build using the latest code on the main branch.
git checkout main

# Build the latest code on the 9.0.x branch
git checkout 9.0.x

# Build the 9.0.5 version exactly.
git checkout 9.0.5

Note the build process has evolved, and earlier versions will need to be built somewhat differently than how the instructions in this guide specify. If you have trouble, ask on Discord or the forums.

Fetch Submodules

We are not using git submodule update --init --recursive or git submodule update --init. Instead you run the special Makefile target make fetch-all-submodules at the top level, or make fetch-port-submodules when you in a particular ports/port-name directory. The target fetches only as many commits from each submodule as is necessary, using either a blobless partial clone (if available) or a shallow clone. This saves downloading the complete trees of some large submodules.

make fetch-all-submodules fetches all the submodules in the entire repository, and can take several minutes and use up a lot of storage. If you are planning to build only in one or a few port directories, you'll save time by using make fetch-port-submodules, which fetches only the submodules need for that particular port.

If you are having trouble with your submodules, you can clean out all the fetched modules by doing make remove-all-submodules at the top level. Then run make fetch-all-submodules or make fetch-port-submodules again to fetch a fresh copy of all the submodules. (There is no make remove-port-submodules.)

Using a Version of git that can do Partial Clones

As mentioned above, the submodule fetching targets try to do a blobless partial clone if possible. This is better than a shallow clone: it is slightly faster, and the partial clone acts like a full clone if you want to maneuver around in the submodule by checking out other commits, examining the history, etc.

Git version 2.36 or later supports blobless partial clones. To find out which version of git you are using, do git --version. Consider installing a newer version of git if it is available. On Ubuntu, you can install the git PPA and get the latest stable version of git. On macOS, homebrew may provide a later version for you.

cd circuitpython   # go to the top level
make fetch-all-submodules

# OR, for example, to fetch only the submodules needed for RP2040 builds:

cd circuitpython/ports/raspberrypi
make fetch-port-submodules

Install pre-commit

We are using the pre-commit system to check submitted code for problems before it gets to GitHub. For more information, see this Learn Guide page. To add pre-commit to your repository clone, do:

cd <your repository clone directory>
# You only need to do this once in each clone.
pre-commit install

Are you seeing errors when pre-commit runs? See this hint

Build mpy-cross

Build the mpy-cross compiler first, which compiles Circuitpython .py files into .mpy files. It's needed to include library code in certain boards.

(If you get a make: msgfmt: Command not found error, you have not installed gettext. Go back to the Setup page for your operating system.)

Normally you do not need to rebuild mpy-cross on every pull or merge from the circuitpython repository or for your own changes. The .mpy format does not change very often. But occasionally when we merge from MicroPython, the format changes. You will find that your old .mpy files or frozen libraries give an error, and you will need to rebuild mpy-cross.

make -C mpy-cross

Build CircuitPython

Now you're all set to build CircuitPython. If you're in the main branch of the repo, you'll be building the latest version. Choose which board you want to build for. The boards available are all the subdirectories in ports/atmel-samd/boards/.

cd ports/atmel-samd
make BOARD=circuitplayground_express

By default the en_US version will be built. To build for a different language supply a TRANSLATION argument.

cd ports/atmel-samd
make BOARD=circuitplayground_express TRANSLATION=es

Run Your Build!

When you've successfully built, you'll see output like:

Create build-circuitplayground_express/firmware.bin
Create build-circuitplayground_express/firmware.uf2
python2 ../../tools/uf2/utils/uf2conv.py -b 0x2000 -c -o build-circuitplayground_express/firmware.uf2 build-circuitplayground_express/firmware.bin
Converting to uf2, output size: 485888, start address: 0x2000
Wrote 485888 bytes to build-circuitplayground_express/firmware.uf2.

Copy firmware.uf2 to your board the same way you'd update CircuitPython: Double-click to get the BOOT drive, and then just copy the .uf2 file:

# Double-click the reset button, then:
cp build-circuitplayground_express/firmware.uf2 /media/yourname/CPLAYBOOT

The board will restart, and your build will start running.

If you're using a board without a UF2 bootloader, you'll need to use bossac and the firmware.bin file, not the .uf2 file. Detailed instructions are here.

Use All Your CPUs When Building

Most modern computers have CPU chips with multiple cores. For instance, you may have a 2-core, 4-core, or 6-core or more CPU. Your CPU may also allow 2 "threads" per core, so that it appears to have even more cores. You can run much of the build in parallel by using the make -j flag. This is will speed up the build noticeably.

If you don't know how many cores or threads your CPU has, on Linux you can use this command:

getconf _NPROCESSORS_ONLN
12
# This CPU has 6 cores and 12 threads.

Then, when you run make, add the -j<n> option to use as many cores or threads as possible. For example:

make -j12 BOARD=trinket_m0

When to make clean

After you make changes to code, normally just doing make BOARD=... will be sufficient. The changed files will be recompiled and CircuitPython will be rebuilt.

However, there are some circumstance where you must do:

make clean BOARD=...

If you have changed the #include file structure in certain ways, or if you have defined QSTR's (a way of defining constants strings in the CircuitPython source), then you must make clean before rebuilding. If you're not sure, it's always safe to make clean and then make. It might take a little longer to build, but you'll be sure it was rebuilt properly.

Updating Your Repo

When there are changes in the GitHub repo, you might want to fetch those and then rebuild. Just "pull" the new code (assuming you haven't made changes yourself), update the submodules if necessary, and rebuild:

git pull
 # only if necessary, from the top level directory
make fetch-submodules
# Then make again.

Those are the basics. There's a lot more to know about how to keep your forked repo up to date, merge "upstream" (Adafruit's) changes into your code, etc. We cover this in the Contribute to CircuitPython with Git and GitHub guide

This guide was first published on Apr 26, 2018. It was last updated on Jul 22, 2024.

This page (Build CircuitPython) was last updated on Jun 02, 2024.

Text editor powered by tinymce.