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.
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.9 - 3.11
- Xcode or Xcode Command Line Tools
- Homebrew
- Ubuntu 20.04/22.04 LTS
- x86-64 CPU (with SSE4.2 or newer) or AWS Graviton2/3 CPU
- Minimum 8 GiB RAM
- Python 3.9 - 3.11
- g++ or clang++ C++ compiler
Windows support is still in development.
In the meantime, you can use Mojo on Windows with WSL, using a compatible version of Ubuntu (see our requirements for Linux).
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).
- Stable
- Nightly
-
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.
- Mac
- Linux
brew update && brew install modularml/packages/modular
apt-get install -y apt-transport-https &&
keyring_location=/usr/share/keyrings/modular-installer-archive-keyring.gpg &&
curl -1sLf 'https://dl.modular.com/bBNWiLZX5igwHXeu/installer/gpg.0E4925737A3895AD.key' | gpg --dearmor >> ${keyring_location} &&
curl -1sLf 'https://dl.modular.com/bBNWiLZX5igwHXeu/installer/config.deb.txt?distro=debian&codename=wheezy' > /etc/apt/sources.list.d/modular-installer.list &&
apt-get update &&
apt-get install -y modular -
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:
- venv
- conda
For most users, we recommend venv (it's included with Python):
python3 -m venv mojo-venv && source mojo-venv/bin/activate
Only if you already use conda as your preferred environment, we suggest you use that:
conda create -n mojo python=3.10 -y && conda activate mojo
-
Install the Mojo SDK:
modular install mojo
-
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"If you're using ZSH, run this command:
MOJO_PATH=$(modular config mojo.path) \
&& echo 'export MODULAR_HOME="'$HOME'/.modular"' >> ~/.zshrc \
&& echo 'export PATH="'$MOJO_PATH'/bin:$PATH"' >> ~/.zshrc \
&& source ~/.zshrcIf you're using fish, run this command:
set MOJO_PATH (modular config mojo.path) \
&& set -Ux MODULAR_HOME $HOME/.modular \
&& fish_add_path $MOJO_PATH/bin
Nightly builds are not fully tested. They might include incomplete features,
performance regressions, and new bugs. When using code from the
mojo GitHub repo, be sure you checkout
the nightly
branch, because the main
branch might not be compatible with
nightly builds.
-
Open a terminal and install the
modular
command line tool with this helper script (this is the same for stable and nightly builds):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
apt-get install -y apt-transport-https &&
keyring_location=/usr/share/keyrings/modular-installer-archive-keyring.gpg &&
curl -1sLf 'https://dl.modular.com/bBNWiLZX5igwHXeu/installer/gpg.0E4925737A3895AD.key' | gpg --dearmor >> ${keyring_location} &&
curl -1sLf 'https://dl.modular.com/bBNWiLZX5igwHXeu/installer/config.deb.txt?distro=debian&codename=wheezy' > /etc/apt/sources.list.d/modular-installer.list &&
apt-get update &&
apt-get install -y modular -
Create a virtual environment for nightly builds:
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:
- venv
- conda
For most users, we recommend venv (it's included with Python):
python3 -m venv mojo-nightly-venv && source mojo-nightly-venv/bin/activate
Only if you already use conda as your preferred environment, we suggest you use that:
conda create -n mojo-nightly python=3.10 -y && conda activate mojo-nightly
-
Install the nightly Mojo SDK:
modular install nightly/mojo
-
Set environment variables so you can access the nightly
mojo
CLI:- Bash
- ZSH
- fish
If you're using Bash, run this command:
MOJO_NIGHTLY_PATH=$(modular config mojo-nightly.path) \
&& BASHRC=$( [ -f "$HOME/.bash_profile" ] && echo "$HOME/.bash_profile" || echo "$HOME/.bashrc" ) \
&& echo 'export MODULAR_HOME="'$HOME'/.modular"' >> "$BASHRC" \
&& echo 'export PATH="'$MOJO_NIGHTLY_PATH'/bin:$PATH"' >> "$BASHRC" \
&& source "$BASHRC"If you're using ZSH, run this command:
MOJO_NIGHTLY_PATH=$(modular config mojo-nightly.path) \
&& echo 'export MODULAR_HOME="'$HOME'/.modular"' >> ~/.zshrc \
&& echo 'export PATH="'$MOJO_NIGHTLY_PATH'/bin:$PATH"' >> ~/.zshrc \
&& source ~/.zshrcIf you're using fish, run this command:
set MOJO_NIGHTLY_PATH (modular config mojo-nightly.path) \
&& set -Ux MODULAR_HOME $HOME/.modular \
&& fish_add_path $MOJO_NIGHTLY_PATH/bin
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:
-
To start a REPL session, type
mojo
in your terminal and press Enter. -
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:
-
Create a file named
hello.mojo
(orhello.🔥
) and add the following code:fn main():
print("Hello, world!")That's all you need. Save the file and return to your terminal.
-
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 either MAX (which includes Mojo) or Mojo.
4. Build an executable binary​
Finally, let's build and run that same code as an executable:
-
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. -
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.
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:
-
Make sure you have the latest
modular
CLI:- Mac
- Linux
brew update \
&& brew upgrade modularsudo apt update \
&& sudo apt install modular -
Update the
mojo
package:- Stable
- Nightly
modular update mojo
modular update nightly/mojo