Get started with Mojo🔥
On this page, we'll show you how to create the classic "Hello world" starter program with Mojo. If you'd rather read how to write Mojo code, see the introduction to Mojo.
By installing Mojo, you understand and agree to our software license.
1. Create a new project
To create a new Mojo project, we'll use Magic—a virtual environment manager and package manager based on conda.
-
Install Magic on macOS or Ubuntu Linux with this command:
curl -ssL https://magic.modular.com | bash
curl -ssL https://magic.modular.com | bash
Then run the
source
command printed in your terminal. -
Create a Mojo project called "hello-world":
magic init hello-world --format mojoproject
magic init hello-world --format mojoproject
This creates a directory named
hello-world
and installs the Mojo project dependencies—the only dependency for a Mojo project is themax
package (Mojo is bundled with MAX). -
Start a shell in the project virtual environment:
cd hello-world && magic shell
cd hello-world && magic shell
That's it! The magic shell
command activates the virtual environment so you
can now start using Mojo. For example, you can check your Mojo version like
this:
mojo --version
mojo --version
2. Run code in the REPL
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
and press Enter. -
Type
print("Hello, world!")
and press Enter twice (a blank line is required to indicate the end of an expression).The result looks like this:
$ 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")
Hello world$ 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")
Hello world -
To exit REPL, type
:quit
and press Enter, or press Ctrl + D.
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!")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
mojo hello.mojo
Hello, world!
Hello, world!
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
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
./hello
Hello, world!
Hello, world!
The build
command creates a statically compiled binary
file, so it contains all the code and libraries it needs to run.
You can now deactivate the virtual environment by just typing exit
:
exit
exit
Now let's try running an existing code example.
5. Run an example from GitHub
Our Mojo code examples in GitHub include a Magic configuration file so you can
simply clone the repo and run the code with magic
. For example:
-
Clone the Mojo repo:
git clone https://github.com/modularml/mojo.git
git clone https://github.com/modularml/mojo.git
Only if you installed the nightly build, also checkout the nightly branch:
git checkout nightly
git checkout nightly
-
Navigate to the examples:
cd mojo/examples
cd mojo/examples
-
Run some code:
magic run mojo hello_interop.mojo
magic run mojo hello_interop.mojo
Hello Mojo 🔥!
9
6
3
Hello from Python!
I can even print a numpy array: [1 2 3]Hello Mojo 🔥!
9
6
3
Hello from Python!
I can even print a numpy array: [1 2 3]
6. 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.
Update Mojo
To update the Mojo version in your project, use magic add
and specify the max
package version.
For example, if you want to always use the latest version of Mojo, you can use
the *
wildcard as the version and then simply run magic update
(you must
run magic add
within the project path):
cd hello-world
cd hello-world
magic add max
magic add max
magic update
magic update
To be more specific with your package version, you can use any of the Python package version specifiers. For example:
magic add "max~=24.4"
magic add "max~=24.4"
magic add "max>=24.4,<24.5"
magic add "max>=24.4,<24.5"
Next steps
-
If you're new to Mojo, we suggest you learn the language basics in the introduction to Mojo.
-
To learn more about the
magic
tool, read Get started with Magic. -
Explore more code examples in the the Mojo repo. 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.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
😔 What went wrong?