Skip to content

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.

You met mojo build in Mojo Get Started. A quick reminder, with one new detail:

def main():
print("Hello from a built program!")
Terminal window
mojo build hello.mojo
./hello
mojo build hello.mojo -o bin/hello

You’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.

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:

299999995

On 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.

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.

Two more options you’ve seen in other lessons:

  • -D NAME=value passes a value into your code at compile time, read with functions from std.sys. Mojo Feature Toggles covers it. For example, mojo build -D VERSION=1.4.0 app.mojo bakes the version into the program.
  • -I folder adds 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.

A built program isn’t fully standalone. On Linux you can see what it loads with ldd:

Terminal window
ldd ./hello

You’ll see (shortened):

libKGENCompilerRTShared.so => /home/you/hello-world/.venv/lib/python3.13/site-packages/modular/lib/libKGENCompilerRTShared.so
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6
libMSupportGlobals.so => /home/you/hello-world/.venv/lib/python3.13/site-packages/modular/lib/libMSupportGlobals.so
libAsyncRTRuntimeGlobals.so => /home/you/hello-world/.venv/lib/python3.13/site-packages/modular/lib/libAsyncRTRuntimeGlobals.so

Three 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 directory

You have three options:

  1. Run it where the project is. Fine for your own tools.
  2. 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.
  3. 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:
Terminal window
mkdir dist
mojo 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.

By default, mojo build targets exactly the CPU you’re on, with every feature it has. Ask Mojo what it assumes:

Terminal window
mojo build --print-effective-target

You’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:

Terminal window
mojo build --mcpu=x86-64-v2 hello.mojo -o hello-portable
mojo build --mcpu=x86-64-v2 --print-effective-target

You’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,+x87

x86-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.

--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:

Terminal window
mojo build --target-triple aarch64-unknown-linux-gnu --target-cpu cortex-a72 --emit object -o hello-arm.o hello.mojo
file hello-arm.o

You’ll see:

hello-arm.o: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), not stripped

But 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 status
mojo: error: failed to link executable

The 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.

Build hello.mojo as bin/greeter, so that ./bin/greeter prints the greeting.

Hint: -o doesn’t create folders.

Show solution
Terminal window
mkdir -p bin
mojo build hello.mojo -o bin/greeter
./bin/greeter

You’ll see:

Hello from a built program!

mkdir -p creates the folder and doesn’t complain if it already exists.

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: dev

Hint: all the options go into one mojo build command; --print-effective-target is a separate check.

Show solution
Terminal window
mojo build --mcpu=x86-64-v3 -D VERSION=2.0 app.mojo -o app
./app
mojo build --mcpu=x86-64-v3 --print-effective-target

You’ll see (features shortened):

version: 2.0
Effective 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.mojo makes a program; -o path names it, but the folder must exist.
  • Building pays off when you start a program often: mojo run compiles every time.
  • Optimization is on by default (-O3); -O0 and -g help debugging.
  • A built program loads Mojo’s runtime libraries from your .venv by full path. Keep the project, install Mojo on the other machine, or ship the libraries with a $ORIGIN search path.
  • By default you build for your exact CPU; use --mcpu=x86-64-v2 (or v3) for programs that run on other x86-64 machines.
  • --emit gives 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

1. You run mojo build tool.mojo -o out/tool, but there's no out folder. What happens?
2. Why is starting a built program much faster than mojo run on the same file?
3. You delete your project's .venv folder. What happens to a program you built earlier?
4. You want one x86-64 program that runs on older computers too. Which option helps?