Getting Started with Mojo  ️‍🔥

Getting Started with Mojo ️‍🔥

Mojo, the new Programming Language for all AI developers is as simple as Python and as fast as C.

What is Mojo?

Mojo is a programming language developed by the Modular team to simplify AI and machine learning infrastructure. It offers powerful compile-time metaprogramming, support for various accelerators, and full compatibility with Python.

Mojo combines the usability of Python with the performance of C, unlocking unparalleled programmability of AI hardware and extensibility of AI models.

Mojo's goal is to create a unified language that streamlines AI development while retaining compatibility with Python.

Why you would love it.

If you are a developer, and you love AI, and you love speed, and you love simplicity, you will love mojo.

Here are the best parts:

  1. Usability and Programmability: Write everything in one language.

    Write Python and scale all the way down to the metal. Program the multitude of low-level AI hardware. No C++ or CUDA required.

  2. Performance: Unlock Python performance.

    Utilize the full power of the hardware, including multiple cores, vector units, and exotic accelerator units, with the world's most advanced compiler and heterogenous runtime. Achieve performance on par with C++ and CUDA without the complexity.

  3. Interoperability: Access the entire Python ecosystem.

    Experience true interoperability with the Python ecosystem. Seamlessly intermix arbitrary libraries like Numpy and Matplotlib and your custom code with Mojo.

Why is there always a fire emoji beside the Mojo 🔥?

Because it's freaking hot!

And, mainly because the emoji is its file extension. You can save your code as hello.🔥 or hello.mojo)

From the Mojo team:

We paired Mojo with fire emoji 🔥 as a fun visual way to impart onto users that Mojo empowers them to get their Mojo on—to develop faster and more efficiently than ever before. We also believe that the world can handle a unicode extension at this point, but you can also just use the .mojo extension. :)


How to Install Mojo on Linux?

Currently, Mojo is available locally only for Ubuntu Linux systems. For now, Windows and Mac users can find their way around using WSL, containers, or a remote Linux system.

System Requirements:

To get started, make sure your system meets these requirements:

  • Ubuntu 20.04 and later

  • x86-64 CPU and minimum 4GiB RAM

  • Python 3.8 - 3.10

  • g++ or clang++ compiler (g++ comes pre-installed on Ubuntu)

Install the Modular CLI and Mojo SDK

The Modular CLI works like a package manager to install and update Mojo.

Install the Modular CLI:

curl https://get.modular.com | \
  MODULAR_AUTH=mut_fe303dc5ca504bc4867a1db20d897fd8 \
  sh -

The Mojo SDK includes everything you need for local Mojo development, including the Mojo standard library and the Mojo command-line interface (CLI). The Mojo CLI can start a REPL programming environment, compile and run Mojo source files, format source files, and more.

Install the Mojo SDK:

modular install mojo

Time for Hello World

Before you can write your first program in Mojo, you have to set the MODULAR_HOME and PATH variables on your shell.

echo 'export MODULAR_HOME="$HOME/.modular"' >> ~/.bashrc

echo 'export PATH="$MODULAR_HOME/pkg/packages.modular.com_mojo/bin:$PATH"' >> ~/.bashrc

source ~/.bashrc

Run Mojo on REPL

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 `: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 put the code in a .mojo source file.

Run a Mojo file

To write the Mojo code and execute it:

  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!
    

Mojo Language Basics

Here are some things to note about the language:

1. Syntax and Semantics:

Mojo supports Python's syntax and semantics, using line breaks and indentation for code blocks. It has most Python control-flow features.

2. Functions:

Functions in Mojo can be declared using either fn (strongly-typed, memory-safe) or def (Python-style dynamic). fn functions enforce type safety.

3. Variables:

Variables in Mojo can be declared with var (mutable) or let (immutable). Declaring types for variables is optional.

Example:

fn do_math():
    let x: Int = 1
    let y = 2
    print(x + y)

do_math()

4. Structures:

Mojo allows the creation of structs for high-level abstractions. A struct in Mojo is similar to a class in Python. But structs are static and don't allow dynamic dispatch.

Example:

struct MyPair:
    var first: Int
    var second: Int

    fn __init__(inout self, first: Int, second: Int):
        self.first = first
        self.second = second

    fn dump(self):
        print(self.first, self.second)

Usage:

let mine = MyPair(2, 4)
mine.dump()

5. Python Integration:

Mojo provides a mechanism to import Python modules as-is, leveraging existing Python code.

Example:

from python import Python

let np = Python.import_module("numpy")

ar = np.arange(15).reshape(3, 5)
print(ar)
print(ar.shape)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
(3, 5)

Mojo aims to combine the best of systems programming with the ease and flexibility of Python.


What's Next

  • For those using VS Code, you can install the Mojo extension, so you get syntax highlighting, code completion, diagnostics, and more.

  • You can also experiment with Mojo using the web-based Mojo Playground.

  • To learn more about Mojo, Read the Docs.

  • And you can always chat with the Mojo community on Discord.

Mojo🔥 is the future programming language for all AI developers.