What this is about
This article gets you from nothing to a running Mojo program. You will install the tool, write a short file, run it, and then break it on purpose. The last part matters most. The first errors you see will not feel friendly, but each one says what to fix, and we will read four real ones together.
We used Mojo 1.2.0 on Linux. The steps are short, and the messages below are what our machine printed.
Why it matters
A language is only useful once your first program runs. Most people stop at the install step, so we keep that part small. Learning to read an error message is the second hurdle. If you can read the compiler's words, you can fix most mistakes on your own.
How to do it, step by step
1. Install a tool called pixi. Pixi sets up a folder with exactly the software your project needs and nothing else on your machine. Find it at pixi.sh. Follow its install line for your system.
2. Make a project folder and add Mojo. The libraries in our mojo-libs page use these lines to set up a project:
pixi init app && cd app
pixi workspace channel add https://conda.modular.com/max https://prefix.dev/amarbaro/amarbaro
pixi add mojo
The first channel is where Modular publishes Mojo. The second is where we publish our libraries, so you can add one later with pixi add mojo-json. Modular may change its install steps over time, so check the Mojo documentation if a line fails.
There is a second way. Our engine, mojo-baro, uses uv sync, which creates a folder with a pinned toolchain. Its README says the pin max[all]==26.7.0 is Mojo 1.2.0. Pixi is the simpler route for a first try.
3. Write a file. Save this as hello.mojo:
def main():
print("Hello, Mojo")
for i in range(3):
print("step", i)
4. Run it. Inside the folder, run the file through pixi so it finds Mojo:
pixi run mojo run hello.mojo
You should see:
Hello, Mojo
step 0
step 1
step 2
mojo run compiles your file and runs it at once. When you want a program you can keep, use mojo build hello.mojo -o hello and then run ./hello. Our mojo-port tool builds this way.
If your shell says command not found: mojo, Mojo is not on your PATH. Run it through pixi run, or open a shell with pixi shell.
The four errors you will meet first
We made each of these mistakes on purpose and copied what the compiler said.
1. You forgot var.
def main():
x = 5
print(x)
error: implicit declaration of 'x' is not allowed; add 'var' to declare a new name
Fix: write var x = 5. Python lets you make a name by assigning it. Mojo asks you to say so.
2. The wrong kind of value.
def main():
var n: Int = "five"
print(n)
error: cannot implicitly convert 'StringLiteral["five"]' value to 'Int'
Fix: you said n is a whole number and then gave it text. Change one of them.
3. A name the compiler has never seen.
def main():
print(count)
error: use of unknown declaration 'count'; did you mean to import it from 'std.itertools'?
Fix: check the spelling, or add the import line the message suggests. Here the compiler guessed that count might come from a standard module. If you meant a variable of your own, create it with var.
4. Code outside a function.
def main():
print("hi")
error: expressions must not appear at file scope; move this into a function body
error: body must not be empty; use 'pass' or check that the lines below are indented
Fix: indent the print line by four spaces. Python needs the indent too, but Mojo also wants your code inside main.
One more you will meet soon. If a function can fail, it must be marked raises, and so must any function that calls it. Otherwise the compiler says it cannot call a function that may raise in a context that cannot raise. Add raises to the def line, and the message goes away.
How to read an error message
Every error from the compiler has the same shape. Above we showed only the error: line, but the full message on your screen starts with a path, then a line and column number, as in hello.mojo:2:5. That says: file hello.mojo, line 2, column 5. Below that, the compiler prints your line again and puts a caret, a ^ sign, under the exact spot.
So the habit is short.
- Read the first line of the message. It names the problem in words.
- Look at the line and column. Open the file at that place.
- Read the caret. It points at the part the compiler could not accept.
- Fix one error and run again. Several errors often come from one cause, and a single fix can clear them all.
Do not worry if a message is long. Look for the sentence that starts with error:. After a failed compile, the last line, failed to parse the provided Mojo source module, only says that the compile did not finish.
What a run does when everything is right
When the program compiles, mojo run runs it at once and prints its output. If a program raises an error while it runs, you see a line that starts with Unhandled exception caught during execution: followed by the message. For example, when we asked our subprocess library to stop a sleep after one second, it printed Command 'sleep' timed out after 1.0 seconds. That is a run-time error, which is different from a compile-time error. A compile-time error means your file is not a valid program yet. A run-time error means a valid program hit a problem while working.
Words you may not know
- PATH: the list of folders your shell searches when you type a command.
- Channel: an online shelf that a package tool downloads software from.
- Compile: turn your code into instructions the processor can run.
- Binary: the compiled program file you can run again without the source.
- Import: a line that brings code from another file into yours.
Try it
- Install pixi from pixi.sh, then run the three project lines above.
- Browse the Modular repository on GitHub for examples and the issue tracker.
- Keep the Mojo documentation open in a tab while you write your first file.
- When hello works, add a library from mojo-libs with
pixi add mojo-json.
Read next
- What Mojo is, for people who know a little Python if you want the bigger picture first.

Comentarios
Aún no hay comentarios.
Iniciar sesión para comentar.