Hello, world!
After you install Mojo, you can use the Mojo CLI to build and compile Mojo programs. So let’s create the classic starter program that prints “Hello, world!”
Before you start:
You must set the MODULAR_HOME
and PATH
environment variables, as described in the output when you ran modular install mojo
. For example, if you’re using bash, you can set them as follows:
echo 'export MODULAR_HOME="$HOME/.modular"' >> ~/.bashrc
echo 'export PATH="$MODULAR_HOME/pkg/packages.modular.com_mojo/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
If you have other issues during install, check our known issues.
Run code in the REPL
First, let’s try running some code in the Mojo REPL, which allows you to write and run Mojo code directly 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` 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.
Build and run Mojo source files
Now let’s print “Hello, world” with a source file. Mojo source files are identified with either the .mojo
or .🔥
file extension.
You can quickly execute a Mojo file by passing it to the mojo
command, or you can build a compiled executable with the mojo build
command. Let’s try both.
Run a Mojo file
First, write the Mojo code and execute it:
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 your code looks exactly like the code in step 1, and make sure you correctly installed Mojo.
Build an executable binary
Now, build and run an executable:
Create a stand-alone executable with the
build
command:mojo build hello.mojo
It creates the executable with the same name as the
.mojo
file, but you can change that with the-o
option.Then run the executable:
./hello
The executable runs on your system like any C or C++ executable.
Next steps
If you’re developing in VS Code, install the Mojo extension so you get syntax highlighting, code completion, diagnostics, and more.
If you’re new to Mojo, read the Mojo language basics.
If you want to package your code as a library, read about Mojo modules and packages.
If you want to explore some Mojo code, clone our repo to see some examples:
git clone https://github.com/modularml/mojo.git
Then open the
/examples
directory in your IDE to try our examples:The code examples offer a variety of demos with the standard library to help you learn Mojo’s features and start your own projects.
The Mojo notebooks are the same Jupyter notebooks we publish in the Mojo Playground, which demonstrate a variety of language features. Now with the Mojo SDK, you can also run them in VS Code or in JupyterLab.
For a deep dive into the language, check out the Mojo programming manual.
To see all the available Mojo APIs, check out the Mojo standard library reference.
Note: The Mojo SDK is still in early development, but you can expect constant improvements to both the language and tools. Please see the known issues and report any other issues on GitHub.