Skip to content

Mojo Tools & Setup

When you installed Mojo, you got more than a compiler. One command, mojo, runs your code, builds programs, formats files and more. This page is a tour of that toolbox, plus the VS Code extension and a project layout that stays tidy as it grows.

Every command and output here was run on Linux with Mojo 1.2, inside a uv project like the one from Mojo Get Started. Long file paths in the output are shortened.

uv add mojo installs a handful of packages. You can see them with uv tree:

Terminal window
uv tree

Output:

hello-world v0.1.0
└── mojo v1.2.0
├── mblack v26.7.0
│ ├── click v8.5.1
│ ├── mypy-extensions v1.2.0
│ ├── pathspec v1.1.1
│ └── platformdirs v4.14.8
├── mojo-compiler v1.2.0
│ └── mojo-compiler-mojo-libs v1.2.0
└── mojo-lldb-libs v1.2.0

In plain words, you get:

  • the mojo command and the compiler (mojo-compiler),
  • the standard library,
  • a code formatter (mblack, which mojo format uses),
  • a debugger based on LLDB (mojo-lldb-libs),
  • a language server (the mojo-lsp-server program, which comes with the mojo package itself), which editors use for completion and error squiggles.

You can also install mojo-compiler on its own: the Mojo FAQ mentions it as a smaller package for machines that only need to build or run code.

Ask it what it can do:

Terminal window
mojo --help

Output (shortened):

COMMANDS
run — Builds and executes a Mojo file.
build — Builds an executable from a Mojo file.
repl — Launches the Mojo REPL.
debug — Launches the Mojo debugger using the command-line interface or an external editor.
precompile — Precompiles a Mojo package.
format — Formats Mojo source files.
doc — Compiles docstrings from a Mojo file.
demangle — Demangles the given name.

Each command has its own help, for example mojo build --help. The Mojo CLI reference has every option. And to check which version you have, use mojo --version (or the short mojo -v):

Terminal window
mojo --version

Output:

Mojo 1.2.0 (2016d0cd)

mojo run file.mojo compiles your file and runs it straight away. mojo file.mojo does the same thing; run is optional.

Anything you write after the file name goes to your program, not to Mojo. This program prints its own arguments using argv() from the standard library:

from std.sys import argv
def main():
var args = argv()
for i in range(len(args)):
print(i, args[i])
Terminal window
mojo run args.mojo apple banana

Output:

0 args.mojo
1 apple
2 banana

Item 0 is the program’s own name. Options for Mojo itself go before the file name, for example mojo run -O0 args.mojo to turn optimizations off (the default level is 3). Reading arguments properly gets its own lesson, Mojo Command-Line Arguments.

mojo build compiles your file into an executable and stops there. -o picks the name:

Terminal window
mojo build args.mojo -o fruit
./fruit one two

Output:

0 ./fruit
1 one
2 two

The built program starts right away, because the compiling already happened. Use mojo run while you’re writing code and mojo build when you want the finished program.

mojo format rewrites your files in the standard Mojo style. Here’s a messy file, messy.mojo:

def add(a:Int,b:Int)->Int:
return a+b
def main():
var nums:List[Int]=[1,2,3]
print( add(2,3) , nums )

Format it:

Terminal window
mojo format messy.mojo

Output:

reformatted messy.mojo
All done! ✨ 🍰 ✨
1 file reformatted.

And the file now looks like this:

def add(a: Int, b: Int) -> Int:
return a + b
def main():
var nums: List[Int] = [1, 2, 3]
print(add(2, 3), nums)

It still does exactly the same thing when you run it.

Output:

5 [1, 2, 3]

Spaces around operators, 4-space indentation, and two blank lines between functions. You can format a whole folder with mojo format ., change the line length with -l 100 (the default is 80), and hide the messages with -q.

If you’ve used cargo test or pytest, you might try mojo test. In Mojo 1.2 it doesn’t exist:

mojo: error: no such command 'test'

Instead, tests are ordinary Mojo programs. You write functions whose names start with test_, check results with helpers from std.testing, and let TestSuite find and run them:

from std.testing import assert_equal, TestSuite
def double(n: Int) -> Int:
return n * 2
def test_double_small() raises:
assert_equal(double(2), 4)
def test_double_zero() raises:
assert_equal(double(0), 0)
def main() raises:
TestSuite.discover_tests[__functions_in_module()]().run()
Terminal window
mojo run test_math.mojo

Output:

Running 2 tests for test_math.mojo
PASS [ 0.001 ] test_double_small
PASS [ 0.001 ] test_double_zero
--------
Summary [ 0.001 ] 2 tests run: 2 passed , 0 failed , 0 skipped

A failing test prints FAIL with the two values that didn’t match, and the program exits with an error. The official testing guide has the full story, and so will Mojo Testing.

mojo doc reads the docstrings in your code and writes them out as JSON, which documentation sites can use. Given this shapes.mojo:

"""Small helpers for working with squares."""
def area(side: Float64) -> Float64:
"""Returns the area of a square.
Args:
side: The length of one side.
Returns:
The area.
"""
return side * side
Terminal window
mojo doc shapes.mojo -o shapes.json

This writes shapes.json. A small piece of it:

"name": "area",
"signature": "def area(side: Float64) -> Float64",
"summary": "Returns the area of a square.",

It doesn’t make HTML pages; the official docs call it an early version whose output may change. Without -o, the JSON is printed to the terminal.

Run mojo repl (or just mojo with nothing after it) to get an interactive prompt. You type code, then press Enter on an empty line to run it:

Welcome to Mojo! 🔥
Expressions are delimited by a blank line.
Type `:quit` to exit the REPL and `:mojo help` for further assistance.
1> var x = 2 + 3
2.
(Int) x = 5
2> print(x * 10)
3.
50
3> :quit

It’s fine for quick experiments, but don’t rely on it. The CLI’s own help says the REPL has known issues and isn’t under active development. For anything longer, a small file and mojo run work better, or a notebook (next lesson).

Mojo ships with an LLDB-based debugger. You can start it from the terminal with mojo debug file.mojo, but most people use it through VS Code: click next to a line number to set a breakpoint, then run the file in debug mode. The manual’s debugging page walks through it, and Mojo Debugging will too.

The official Mojo extension gives you syntax colors, completion, hover help, error squiggles as you type, formatting and debugging. It’s on the VS Code Marketplace and on Open VSX for editors such as Cursor.

To use it:

  1. Install the extension.
  2. Open your project folder (the one with pyproject.toml) in VS Code.
  3. Open a .mojo file.

Here’s a layout that works well for small and medium projects:

hello-world/
├── .venv/ # Mojo lives here (made by uv, don't commit it)
├── .gitignore
├── pyproject.toml # project name and dependencies, including mojo
├── uv.lock # exact versions, commit this
├── README.md
├── main.mojo # your program
└── test_main.mojo # tests for it

A few habits that save trouble later:

  • Pin the Mojo version. Mojo still changes between releases, so write the exact version: uv add "mojo==1.2.0". Your pyproject.toml then says "mojo==1.2.0", and everyone on the project gets the same compiler.
  • Commit uv.lock, not .venv. The .gitignore that uv init creates already skips .venv. Add your built programs to it too.
  • Use uv run in scripts. uv run mojo build main.mojo works without activating the environment first, which is handy in CI and scripts.
  • Delete main.py if you don’t need Python in the project. uv init creates it, but Mojo doesn’t use it.

Bigger projects split code into several files and folders. That’s covered in Mojo Modules & Packages.

Save this as untidy.mojo, run mojo format untidy.mojo, then run it. What does the formatted file look like, and what does it print?

def main():
var x=3
var y=4
print(x*y,x+y)

Hint: the formatter changes spacing and indentation, never what the code does.

Show solution
def main():
var x = 3
var y = 4
print(x * y, x + y)

Output:

12 7

mojo format switched to 4-space indentation and added spaces around =, *, + and after commas. The program does exactly the same as before.

Write greet.mojo that prints Hello, <name>! for the first argument. Build it as hi and run ./hi Sam.

Hint: argv()[1] is the first argument after the program name.

Show solution
from std.sys import argv
def main():
var args = argv()
if len(args) > 1:
print(t"Hello, {args[1]}!")
else:
print("Usage: hi <name>")
Terminal window
mojo build greet.mojo -o hi
./hi Sam

Output:

Hello, Sam!

-o hi names the program. The if check avoids a crash when no name is given. The t"..." is a t-string, which puts a value inside text; you’ll learn it in Mojo Output.

  • One command does it all: mojo run, mojo build, mojo format, mojo doc, mojo repl, mojo debug.
  • Arguments after the file name go to your program; Mojo’s own options go before it.
  • mojo build -o name makes an executable, but it still needs Mojo’s runtime libraries from the .venv.
  • mojo format fixes style for you; use it before every commit.
  • There’s no mojo test: tests are programs that use std.testing and TestSuite, run with mojo run.
  • Install the VS Code extension for completion, errors as you type and debugging.
  • In a uv project, pin the Mojo version and commit uv.lock, not .venv.

Next up: Mojo Notebooks, running Mojo code inside Jupyter.

Quick check

1. In mojo run -O0 app.mojo data.txt, who receives data.txt?
2. How do you run Mojo tests in version 1.2?
3. What does mojo format change?
4. Which file should you commit to Git in a uv project?