You are on page 1of 2

Welcome back to the Mojo Programming language course

After installing Mojo, you can use the Mojo CLI to build and compile Mojo programs.
Here’s how you can create the classic “Hello, world!” program:

Setting Environment Variables:

You need to set the MODULAR_HOME and PATH environment variables as described in the
output when you ran modular install mojo. If you’re using bash or zsh, add the
following lines to your configuration file (.bash_profile, .bashrc, or .zshrc):

export MODULAR_HOME="$HOME/.modular"

export PATH="$MODULAR_HOME/pkg/packages.modular.com_mojo/bin:$PATH"

Then source the file you just updated, for example:

source ~/.bash_profile

Running Code in the REPL:

You can run 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).

Building and Running Mojo Source Files:

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.

Running a Mojo File:

Create a file named hello.mojo and add the following code:

Python

AI-generated code. Review and use carefully. More info on FAQ.

fn main():

print("Hello, world!")

Save the file and return to your terminal. Now run it with the mojo command:

mojo hello.mojo

Building an Executable Binary:

Create a stand-alone executable with the build command:

mojo build hello.mojo

Then run the executable:

./hello

Next Steps:
If you’re developing in VS Code, install the Mojo extension for features like code
completion, quick fixes, and hover help for Mojo APIs. 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 the Mojo
repo to see some examples. 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.

Thanks for your attention, see you in the next lecture.

You might also like