Get started with Mojo🔥 | Modular Docs (2024)

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

Updating?

If you already installed Mojo, see how to update.

Requirements​

  • Mac
  • Linux
  • Windows
  • 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 nextsection 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 asoften as possible (not necessarily every day).

  • Stable
  • Nightly
  1. Open a terminal and install the modular command line tool withthis helper script:

    curl -s https://get.modular.com | sh -
    Or, click here to see the manual install commands.
    • Mac
    • Linux
    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 touse. We suggest you do that with either venv or conda:

    • venv
    • 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:

    • Bash
    • ZSH
    • fish

    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 MojoREPL,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 youwant Mojo to evaluate the code, press Enter twice. If there'ssomething to print, Mojo prints it and then returns the prompt to you.

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

3. Run a Mojo file​

Now let's write the code in a Mojo source file and run it with themojo 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 thecode in step 1, and make sure you correctly installed either MAX(which includes Mojo) or 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, butyou 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 andlibraries it needs to run.

5. Install our VS Code extension (optional)​

To provide a first-class developer experience with features like codecompletion, quick fixes, and hover help, we've created a Mojo extension forVisual StudioCode.

Get started with Mojo🔥 | Modular Docs (1)

Next steps​

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

  • If you want to experiment with some code, clone the Mojorepo 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 Jupyternotebooksthat teach advanced Mojo features.

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

If you have issues during install, check our knownissues.

note

To help us improve Mojo, we collect some basic system information andcrash reports. Learnmore.

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 Mojochangelog. Or if you installed a nightly build, look forrelease announcements in this Discordchannel.

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

  1. Make sure you have the latest modular CLI:

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

    • Stable
    • Nightly
    modular update mojo
Get started with Mojo🔥 | Modular Docs (2024)
Top Articles
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated:

Views: 5827

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.