Mojo Building & Compilation Targets
mojo run is perfect while you’re writing code, but it compiles your program every single time. mojo build compiles once and leaves you a program you can start again and again. This page covers the options you’ll actually use, what a built program needs on the computer that runs it, and how to build for a CPU that isn’t yours.
Everything here was run with Mojo 1.2 on Linux (x86-64), inside a uv project like the one from Mojo Get Started.
The basics again
Section titled “The basics again”You met mojo build in Mojo Get Started. A quick reminder, with one new detail:
def main(): print("Hello from a built program!")mojo build hello.mojo./hellomojo build hello.mojo -o bin/helloYou’ll see:
Hello from a built program!mojo: error: unable to write file. The path 'bin' does not exist.Without -o, the program is named after the file (hello) and saved in the current folder. With -o you choose the name and place, but Mojo doesn’t create folders for you. Run mkdir bin first, then the same command works. Unlike with mojo run, options may also come after the file name (like -o here), because mojo build doesn’t pass anything on to your program.
Why build at all?
Section titled “Why build at all?”Here’s a program that does a bit of real work:
def main(): var total = 0 for i in range(100_000_000): total += i % 7 print(total)Output:
299999995On our test machine (a Linux virtual machine with 4 cores of an Intel Xeon at 2.1 GHz), mojo run count.mojo took between 1.2 and 2.9 seconds each time. After mojo build count.mojo, ./count took about 0.1 seconds. Almost all of the mojo run time is compiling, not running. Your numbers will be different, but the pattern holds: if you start a program often, build it.
Optimization levels
Section titled “Optimization levels”mojo build optimizes your code for speed by default (level 3). -O changes that:
| Option | What it does | Use it when |
|---|---|---|
(nothing) or -O3 |
full optimization | you want the finished program |
-O0 |
no optimization, faster to compile | you’re debugging and want code that matches your source |
-g |
adds debug information | you’ll use a debugger (see Mojo Debugging) |
Built with -O0, the counting program above took about 0.57 seconds instead of 0.1 on the same machine, so leave optimization on unless you have a reason. mojo run takes the same options, again before the file name: mojo run -O0 count.mojo.
Settings at build time: -D and -I
Section titled “Settings at build time: -D and -I”Two more options you’ve seen in other lessons:
-D NAME=valuepasses a value into your code at compile time, read with functions fromstd.sys. Mojo Feature Toggles covers it. For example,mojo build -D VERSION=1.4.0 app.mojobakes the version into the program.-I folderadds a folder to search for imports, as shown in Mojo Modules & Packages.
The difference from environment variables: -D values are fixed in the built program, while environment variables are read each time it starts.
What a built program needs to run
Section titled “What a built program needs to run”A built program isn’t fully standalone. On Linux you can see what it loads with ldd:
ldd ./helloYou’ll see (shortened):
libKGENCompilerRTShared.so => /home/you/hello-world/.venv/lib/python3.13/site-packages/modular/lib/libKGENCompilerRTShared.solibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6libMSupportGlobals.so => /home/you/hello-world/.venv/lib/python3.13/site-packages/modular/lib/libMSupportGlobals.solibAsyncRTRuntimeGlobals.so => /home/you/hello-world/.venv/lib/python3.13/site-packages/modular/lib/libAsyncRTRuntimeGlobals.soThree of those are Mojo runtime libraries, and the program remembers their full path inside your project’s .venv. So the program keeps working if you copy it to another folder on the same computer, but it fails if .venv is deleted or moved, or on a computer without them:
./hello: error while loading shared libraries: libKGENCompilerRTShared.so: cannot open shared object file: No such file or directoryYou have three options:
- Run it where the project is. Fine for your own tools.
- Install the same Mojo version on the other computer (
uv add "mojo==1.2.0"in a project there) and build or run it there. - Ship the libraries next to the program. This worked in our Linux tests: build with an extra search path that means “the folder this program is in”, then copy the three files alongside it:
mkdir distmojo build hello.mojo -o dist/hello -Xlinker -rpath -Xlinker '$ORIGIN'cp .venv/lib/python3.13/site-packages/modular/lib/{libKGENCompilerRTShared.so,libMSupportGlobals.so,libAsyncRTRuntimeGlobals.so} dist/-Xlinker passes the next word to the linker, and $ORIGIN (in single quotes, so the shell leaves it alone) tells the system to look in the program’s own folder. The dist folder, about 2 MB here, then ran on its own. The Python version in the path depends on your project. Programs that use Python libraries also need those on the other computer, as the mojo build reference points out. Before handing the Mojo libraries to other people, check that Modular’s license terms allow it for your case.
Which CPU are you building for?
Section titled “Which CPU are you building for?”By default, mojo build targets exactly the CPU you’re on, with every feature it has. Ask Mojo what it assumes:
mojo build --print-effective-targetYou’ll see (features shortened):
Effective target configuration: --target-triple x86_64-unknown-linux-gnu --target-cpu emeraldrapids --target-features +adx,+aes,+amx-bf16,+amx-int8,+amx-tile,+avx,+avx2,+avx512bf16,...That’s great for speed on this machine, but the compilation targets guide warns that code built for newer instructions (such as AVX-512) won’t run on hardware that lacks them. If your program will run on other x86-64 computers, pick an older, widely supported level:
mojo build --mcpu=x86-64-v2 hello.mojo -o hello-portablemojo build --mcpu=x86-64-v2 --print-effective-targetYou’ll see:
Effective target configuration: --target-triple x86_64-unknown-linux-gnu --target-cpu x86-64-v2 --target-features +cmov,+crc32,+cx16,+cx8,+fxsr,+mmx,+popcnt,+sahf,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87x86-64-v2 runs on almost any x86-64 computer from the last decade or so; x86-64-v3 adds AVX2 for more speed on newer ones. The guide also lists the Mojo-style flags (--target-cpu, --target-features); pick one family, since mixing them is an error. On Apple silicon you’re building for Apple’s own CPUs, and the same --print-effective-target shows what’s chosen.
Other outputs and other machines
Section titled “Other outputs and other machines”--emit makes something other than a program:
--emit |
You get |
|---|---|
exe (default) |
a program |
shared-lib |
a shared library other languages can call (see Mojo Exporting to C) |
object |
an object file, for linking with other tools |
asm |
readable assembly code, to see what the compiler made |
llvm |
LLVM IR, the compiler’s intermediate code |
--target-triple picks another platform. Producing object files for another CPU works:
mojo build --target-triple aarch64-unknown-linux-gnu --target-cpu cortex-a72 --emit object -o hello-arm.o hello.mojofile hello-arm.oYou’ll see:
hello-arm.o: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), not strippedBut a finished program for another CPU doesn’t, because Mojo doesn’t bring a linker for other platforms:
/usr/bin/ld: /tmp/mojo_archive-bedc97a.a: Relocations in generic ELF (EM: 183)collect2: error: ld returned 1 exit statusmojo: error: failed to link executableThe guide calls cross-compilation a work in progress: you link the object file yourself with a toolchain for the target, and the target still needs the Mojo runtime libraries. For a Raspberry Pi or an ARM server, it’s usually simpler to install Mojo there and build on that machine. GPU code has its own option, --target-accelerator, which the GPU lessons cover.
Practice
Section titled “Practice”1. Build into a folder
Section titled “1. Build into a folder”Build hello.mojo as bin/greeter, so that ./bin/greeter prints the greeting.
Hint: -o doesn’t create folders.
Show solution
mkdir -p binmojo build hello.mojo -o bin/greeter./bin/greeterYou’ll see:
Hello from a built program!mkdir -p creates the folder and doesn’t complain if it already exists.
2. A portable build with a version
Section titled “2. A portable build with a version”Build app.mojo for the x86-64-v3 level, with -D VERSION=2.0 baked in, and confirm the target with --print-effective-target. Use this app.mojo:
from std.sys import get_defined_string
def main(): print("version:", get_defined_string["VERSION", "dev"]())Output:
version: devHint: all the options go into one mojo build command; --print-effective-target is a separate check.
Show solution
mojo build --mcpu=x86-64-v3 -D VERSION=2.0 app.mojo -o app./appmojo build --mcpu=x86-64-v3 --print-effective-targetYou’ll see (features shortened):
version: 2.0Effective target configuration: --target-triple x86_64-unknown-linux-gnu --target-cpu x86-64-v3 --target-features +avx,+avx2,+bmi,+bmi2,+cmov,+crc32,+cx16,+cx8,+f16c,+fma,...Without -D VERSION=... the program prints version: dev, the default from the code. With it, 2.0 is fixed into this build.
mojo build file.mojomakes a program;-o pathnames it, but the folder must exist.- Building pays off when you start a program often:
mojo runcompiles every time. - Optimization is on by default (
-O3);-O0and-ghelp debugging. - A built program loads Mojo’s runtime libraries from your
.venvby full path. Keep the project, install Mojo on the other machine, or ship the libraries with a$ORIGINsearch path. - By default you build for your exact CPU; use
--mcpu=x86-64-v2(orv3) for programs that run on other x86-64 machines. --emitgives shared libraries, object files, assembly or LLVM IR; cross-compiling works for object files, not finished programs.
Next up: Mojo packaging, sharing your code as a package other projects can import.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- mojo build: every option of the command, including
-O,-D,-I,--emitand the target flags. - Compilation targets: the official guide to target triples, CPUs, features, GPUs and cross-compiling.
- mojo run: the run command, which takes most of the same options.
- x86-64 microarchitecture levels: what
x86-64-v2,v3andv4include.