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.
What comes with Mojo
Section titled “What comes with Mojo”uv add mojo installs a handful of packages. You can see them with uv tree:
uv treeOutput:
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.0In plain words, you get:
- the
mojocommand and the compiler (mojo-compiler), - the standard library,
- a code formatter (
mblack, whichmojo formatuses), - a debugger based on LLDB (
mojo-lldb-libs), - a language server (the
mojo-lsp-serverprogram, which comes with themojopackage 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.
The mojo command
Section titled “The mojo command”Ask it what it can do:
mojo --helpOutput (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):
mojo --versionOutput:
Mojo 1.2.0 (2016d0cd)mojo run: compile and run
Section titled “mojo run: compile and run”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])mojo run args.mojo apple bananaOutput:
0 args.mojo1 apple2 bananaItem 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: make a program
Section titled “mojo build: make a program”mojo build compiles your file into an executable and stops there. -o picks the name:
mojo build args.mojo -o fruit./fruit one twoOutput:
0 ./fruit1 one2 twoThe 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: tidy code for free
Section titled “mojo format: tidy code for free”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+bdef main(): var nums:List[Int]=[1,2,3] print( add(2,3) , nums )Format it:
mojo format messy.mojoOutput:
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.
Testing: no mojo test command
Section titled “Testing: no mojo test command”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()mojo run test_math.mojoOutput:
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 skippedA 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: docstrings as data
Section titled “mojo doc: docstrings as data”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 * sidemojo doc shapes.mojo -o shapes.jsonThis 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.
mojo repl: try one line at a time
Section titled “mojo repl: try one line at a time”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 + 32.(Int) x = 52> print(x * 10)3.503> :quitIt’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 debug and the debugger
Section titled “mojo debug and the debugger”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 VS Code extension
Section titled “The VS Code extension”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:
- Install the extension.
- Open your project folder (the one with
pyproject.toml) in VS Code. - Open a
.mojofile.
A tidy project layout with uv
Section titled “A tidy project layout with uv”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 itA 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". Yourpyproject.tomlthen says"mojo==1.2.0", and everyone on the project gets the same compiler. - Commit
uv.lock, not.venv. The.gitignorethatuv initcreates already skips.venv. Add your built programs to it too. - Use
uv runin scripts.uv run mojo build main.mojoworks without activating the environment first, which is handy in CI and scripts. - Delete
main.pyif you don’t need Python in the project.uv initcreates it, but Mojo doesn’t use it.
Bigger projects split code into several files and folders. That’s covered in Mojo Modules & Packages.
Practice
Section titled “Practice”1. Format a file
Section titled “1. Format a file”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 7mojo format switched to 4-space indentation and added spaces around =, *, + and after commas. The program does exactly the same as before.
2. Build with a name and arguments
Section titled “2. Build with a name and arguments”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>")mojo build greet.mojo -o hi./hi SamOutput:
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 namemakes an executable, but it still needs Mojo’s runtime libraries from the.venv.mojo formatfixes style for you; use it before every commit.- There’s no
mojo test: tests are programs that usestd.testingandTestSuite, run withmojo 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
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Mojo CLI reference: every
mojocommand and option. - mojo build: output names, optimization levels and target options.
- Testing: assertions,
TestSuiteand how to organize test files. - Debugging: breakpoints and the debugger in VS Code and the terminal.
- Mojo extension for VS Code: install the official editor extension.