Skip to main content

Get started with Mojo🔥

On this page, we'll show you how to install Mojo and create the classic "Hello world" starter program with Mojo, in three different ways. If you'd rather read how to write Mojo code beyond just printing text, see the introduction to Mojo.

Updating?

If you already installed Mojo, see how to update.

Requirements

  • Apple silicon (M1 or M2 processor)
  • macOS Ventura (12) or later
  • Python 3.8 - 3.11
  • Xcode or Xcode Command Line Tools
  • Homebrew

1. Install Mojo

If you already installed MAX, you can skip to the next section because MAX includes Mojo.

The Mojo SDK is available as either a stable build or a nightly build. We strive to release stable builds once a month and release nightly builds as often as possible (not necessarily every day).

  1. Open a terminal and install the modular command line tool with this helper script:

    curl -s https://get.modular.com | sh -
    Or, click here to see the manual install commands.
    brew update && brew install modularml/packages/modular
  2. Create a virtual environment:

    Because Mojo interoperates with Python, it's important to define a predictable Python version and package library to use. We suggest you do that with either venv or conda:

    For most users, we recommend venv (it's included with Python):

    python3 -m venv mojo-venv && source mojo-venv/bin/activate
  3. Install the Mojo SDK:

    modular install mojo
  4. Set environment variables so you can access the mojo CLI:

    If you're using Bash, run this command:

    MOJO_PATH=$(modular config mojo.path) \
    && BASHRC=$( [ -f "$HOME/.bash_profile" ] && echo "$HOME/.bash_profile" || echo "$HOME/.bashrc" ) \
    && echo 'export MODULAR_HOME="'$HOME'/.modular"' >> "$BASHRC" \
    && echo 'export PATH="'$MOJO_PATH'/bin:$PATH"' >> "$BASHRC" \
    && source "$BASHRC"

Now you're ready to go.

2. Run code in the REPL

Now that you've installed Mojo, let's write some code!

First, let's use the Mojo REPL, which allows you to write and run Mojo code in a command prompt:

  1. To start a REPL session, type mojo in your terminal and press Enter.

  2. Then type print("Hello, world!") and press Enter twice (a blank line is required to indicate the end of an expression).

That's it! For example:

$ mojo
Welcome to Mojo! 🔥

Expressions are delimited by a blank line.
Type `:quit` to exit the REPL and `:mojo help repl` for further assistance.

1> print("Hello, world!")
2.
Hello, world!

You can write as much code as you want in the REPL. You can press Enter to start a new line and continue writing code, and when you want Mojo to evaluate the code, press Enter twice. If there's something to print, Mojo prints it and then returns the prompt to you.

The REPL is primarily useful for short experiments because the code isn't saved. So when you want to write a real program, you need to write the code in a .mojo source file.

3. Run a Mojo file

Now let's write the code in a Mojo source file and run it with the mojo command:

  1. Create a file named hello.mojo (or hello.🔥) and add the following code:

    fn main():
    print("Hello, world!")

    That's all you need. Save the file and return to your terminal.

  2. Now run it with the mojo command:

    mojo hello.mojo

    It should immediately print the message:

    Hello, world!

If this didn't work for you, double-check that your code looks exactly like the code in step 1, and make sure you correctly installed mojo (it includes Mojo).

4. Build an executable binary

Finally, let's build and run that same code as an executable:

  1. Create an executable file with the build command:

    mojo build hello.mojo

    The executable file uses the same name as the .mojo file, but you can change that with the -o option.

  2. Then run the executable:

    ./hello

This creates a statically compiled binary file, so it contains all the code and libraries it needs to run.

5. Install our VS Code extension (optional)

To provide a first-class developer experience with features like code completion, quick fixes, and hover help, we've created a Mojo extension for Visual Studio Code.

Next steps

  • If you're new to Mojo, we suggest you learn the language basics in the introduction to Mojo.

  • If you want to experiment with some code, clone the Mojo repo to try our code examples:

    git clone https://github.com/modularml/mojo.git

    If you installed the nightly build, also checkout the nightly branch:

    git checkout nightly

    In addition to several .mojo examples, the repo includes Jupyter notebooks that teach advanced Mojo features.

  • To see all the available Mojo APIs, check out the Mojo standard library reference.

If you have issues during install, check our known issues.

note

To help us improve Mojo, we collect some basic system information and crash reports. Learn more.

Update Mojo

To check your current Mojo version, use the --version option:

mojo --version

And compare your version to the latest stable version in the Mojo changelog. Or if you installed a nightly build, look for release announcements in this Discord channel.

If it's time to update, here's what to do:

  1. Make sure you have the latest modular CLI:

    brew update \
    && brew upgrade modular
  2. Update the mojo package:

    modular update mojo