Skip to content

Mojo Get Started

Let’s get Mojo running on your computer and write your first program. It takes about ten minutes. You’ll install Mojo, run a tiny program, turn it into a real app, and learn how to read Mojo’s error messages.

Every Mojo example and output on this page was checked with Mojo 1.2 on Linux.

Mojo runs on three kinds of systems. The official system requirements list the details, but here’s the short version:

System What you need
macOS A Mac with Apple silicon (M1 or newer) and macOS 15 Sequoia or later
Linux A recent distribution, for example Ubuntu 22.04 or later
Windows Windows itself isn’t supported, but Mojo works inside WSL (Windows Subsystem for Linux)

Mojo is installed like a Python package. The easiest tool for that is uv, a small, fast package manager for Python projects. If you don’t have it yet, open a terminal and run:

Terminal window
curl -LsSf https://astral.sh/uv/install.sh | sh

Close the terminal and open a new one, so it picks up the new uv command.

Make a folder for your project and add Mojo to it:

Terminal window
uv init hello-world
cd hello-world
uv add mojo

uv init creates the folder with a few starter files (pyproject.toml, main.py, README.md). You can ignore those; they’re for Python. uv add mojo downloads Mojo into a hidden .venv folder inside your project. The first time takes a minute or two.

Ask Mojo for its version:

Terminal window
uv run mojo --version

Output:

Mojo 1.2.0 (2016d0cd)

If you see a version number, Mojo is installed. The number in brackets is the exact build, so yours may differ slightly.

uv run means “run this command using the tools in this project”. Typing it every time gets old, so turn on the project’s environment once per terminal window:

Terminal window
source .venv/bin/activate

Now you can type plain mojo. The rest of this tutorial assumes you’ve done this.

Create a file called hello.mojo in your hello-world folder (any text editor works) and put this in it:

def main():
print("Hello, Mojo!")

Run it:

Terminal window
mojo hello.mojo

Output:

Hello, Mojo!

That’s your first Mojo program.

There are only two lines, but each one matters:

  • def main(): defines a function called main. Every Mojo program starts running at main. If you know Python, def looks familiar: Mojo uses it for functions too.
  • print("Hello, Mojo!") prints the text between the quotes, followed by a new line.
  • The indentation (the four spaces before print) is how Mojo knows that print belongs inside main. Just like Python, there are no curly braces.

You can put more lines inside main, and they run from top to bottom:

def main():
print("Hello, Mojo!")
print("I am learning Mojo.")
print(2 + 3)

Output:

Hello, Mojo!
I am learning Mojo.
5

print(2 + 3) works out the sum first, then prints the result.

So far you’ve used mojo hello.mojo, which compiles and runs the file in one go. That’s what you’ll use most while learning.

When you want a finished program you can run again without compiling it each time, build it:

Terminal window
mojo build hello.mojo
./hello

Output:

Hello, Mojo!

mojo build turns your code into an executable file called hello (named after your .mojo file). You run it with ./hello, and it starts instantly because the compiling already happened. To pick a different name, add -o:

Terminal window
mojo build hello.mojo -o greeter
./greeter
Command What it does Use it when
mojo hello.mojo Compiles and runs right away You’re writing and testing code
mojo build hello.mojo Makes an executable file You want a finished program

Everyone makes typos. Here’s what happens if you misspell print:

def main():
prnt("Hello, Mojo!")
/home/you/hello-world/hello.mojo:2:5: error: use of unknown declaration 'prnt'
prnt("Hello, Mojo!")
^~~~
mojo: error: failed to parse the provided Mojo source module

It looks scary at first, but it’s telling you exactly where to look:

  • hello.mojo:2:5 means line 2, column 5 of hello.mojo.
  • use of unknown declaration 'prnt' means Mojo doesn’t know anything called prnt.
  • The ^~~~ marks the exact spot.

Fix the spelling and run it again. The last line (failed to parse...) just says the program couldn’t be built; the useful part is always the first error: line.

The official docs also show pixi, another package manager that some Mojo users like. It works just as well. If you’d rather use it, these are the commands from the official install page:

Terminal window
curl -fsSL https://pixi.sh/install.sh | sh
pixi init hello-world -c https://conda.modular.com/max/ -c conda-forge
cd hello-world
pixi add mojo
pixi run mojo --version

Pick one tool and stick with it. This tutorial uses uv.

You can write Mojo in any text editor, but the official extension for VS Code makes life much easier: colors for your code, suggestions as you type, and errors shown right where they happen.

Install the Mojo extension from the VS Code Marketplace, then open your hello-world folder in VS Code. If you use an editor based on VS Code that reads from the Open VSX Registry, the extension is there too.

Change hello.mojo so it prints three lines: your name, the sentence I'm learning Mojo., and the result of 7 * 6.

Hint: each print gets its own line inside main, indented with four spaces.

Show solution
def main():
print("My name is Sam.")
print("I'm learning Mojo.")
print(7 * 6)

Output:

My name is Sam.
I'm learning Mojo.
42

Each print runs in order, top to bottom. print(7 * 6) works out the multiplication first and prints 42.

Write a file greet.mojo that prints Hi from a real program!, then build it into an executable called greet and run it.

Hint: use mojo build with the -o option to choose the name.

Show solution
def main():
print("Hi from a real program!")
Terminal window
mojo build greet.mojo -o greet
./greet

Output:

Hi from a real program!

mojo build compiles the file into an executable. -o greet names it greet, and ./greet runs it from the current folder.

  • Mojo runs on macOS (Apple silicon), Linux, and Windows through WSL.
  • Install it into a project with uv: uv init, then uv add mojo.
  • Run source .venv/bin/activate once per terminal so you can type mojo.
  • Every program starts at def main():, and indentation shows what’s inside it.
  • mojo file.mojo compiles and runs; mojo build file.mojo makes an executable (it still needs Mojo’s libraries).
  • In an error, look for the file, line and column and the first error: line.

Next up: Mojo tools & setup, a tour of the mojo command and your editor.

Quick check

1. Where does every Mojo program start running?
2. Which command compiles and runs hello.mojo right away?
3. What does hello.mojo:2:5 in an error message mean?
4. How do you use Mojo on Windows?