Mojo Installation Guide (100% Accurate and Tested Steps)

mojo installation

In this detailed guide, we’ll cover everything about installing Mojo on Linux, Windows, and Mac, from setting it up to writing and running your first Mojo program.

Introduction to Mojo

Mojo is a programming language that combines the simplicity of Python with the speed and efficiency of C++. This unique combination makes the Mojo programming language an ideal choice for projects where performance is crucial, from data science to system-level programming. Additionally, the Mojo language is designed to solve various AI development challenges that other programming languages often struggle with.

Key Features of Mojo

  • High Performance: Mojo is built to handle tasks that require a lot of calculations and processing quickly.
  • Python Compatibility: Users familiar with Python will find it easy to transition to Mojo, as it uses similar syntax, making it simple to run Mojo programs.
  • Versatile Applications: From AI development to machine learning, the Mojo language offers flexibility for various applications, including data processing and software development.

System Requirements for Mojo

Before you start the Mojo installation, confirm that your system meets the following requirements:

For macOS

  • Apple Silicon (M1/M2/M3)
  • macOS Ventura (13) or later
  • Python 3.9 – 3.12
  • Xcode or Xcode Command Line Tools
  • Homebrew

For Ubuntu

  • Ubuntu 22.04 LTS
  • x86-64 CPU (with SSE4.2 or newer) or AWS Graviton2/3 CPU
  • Minimum 8 GiB RAM
  • Python 3.9 – 3.12
  • g++ or clang++ C++ compiler

Note: Windows support is under development

Install Mojo on Windows

Currently, Mojo is not natively available for Windows, but you can use it with WSL2 (Windows Subsystem for Linux). Here’s how to set it up:

  1. Install Visual Studio Code, the WSL extension, and the Mojo extension.
  2. Install Ubuntu 22.04 for WSL and launch it.
  3. In the Ubuntu terminal, install the Modular CLI with this command: curl https://get.modular.com | MODULAR_AUTH=mut_092deaefe9014a22ae4076d8b29324a7 sh -
  4. Finally, install the Mojo SDK using: modular install mojo

Install Mojo via Terminal (Linux and Mac OS)

To install the Modular command-line tool, which allows you to manage and set up Mojo environments, use this command to install modular.

curl -ssL https://magic.modular.com/3199c19f-ef67-4a56-96a0-ea172d054f04 | bash
Installing mojo via terminal
Install Magic and Mojo

Run the command below to install the Mojo Language SDK. This will set up the necessary development environment for Mojo.

modular install mojo
Installing mojo via modular
Install Mojo

Configure Mojo Environment in Bash

Use the following command to configure the Mojo command-line tool in your Bash environment.

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"
setting mojo path
Setting the path

How to Check the Version of Mojo?

To check the installed version of the Mojo Language SDK, open your terminal and run the following command. This will display the current version

mojo --version
checking mojo version
Mojo version

Create Your First Mojo Project

Now that you have Mojo installed, you can start coding. Magic organizes projects in a clear way, making it simple to run Mojo programs in separate environments. This helps keep your work organized and avoids conflicts between different projects.

In Mojo, code files have a .mojo extension. To write your first program, open a text editor like VS Code, create a new file, and save it as hello.mojo. Inside hello.mojo, write a simple code that prints “hello world.” When you run this file, it will display “hello world” in the terminal.

fn main():
    print("Hello, World!")
hello world in mojo
Hello program in mojo

Run the Mojo Program: To run the Mojo program use the mojo command with the file name to execute your program, which will display “Hello, World!” in the terminal.

mojo hello.mojo
running mojo program via terminal
Running mojo program

Use Mojo in VS Code

Using VS Code with Mojo makes coding easier by providing features like color coding for different parts of the code, checking for errors, and having a terminal built into the editor.

Step 1: Install Visual Studio Code

Step 2: Open Your Mojo Project in VS Code

Once you have installed VS Code, navigate to your Mojo project directory (for example, hello-world).

navigating to directory in vs code
Navigating to directory

Step 3: Install Extension for Mojo Development

To install the Mojo extension in Visual Studio Code, follow these steps:

  1. Go to the Extensions tab on the left side and click on it.
  2. Type “Mojo” in the search bar.
  3. Click on the extension with the fire logo and the company name “Modular,” which has a verified tick.
  4. Look for the “Install” button and click it to install the extension.
installing mojo extension
Mojo extension

Step 4: Write and Run Mojo Code

With everything set up, you can now create and run Mojo programs within VS Code. Now, in the Explorer sidebar, right-click your project folder, select New File, and name it hello.mojo. Open hello.mojo and add a simple Mojo program and run the file.

fn main():
    print("Hello, World!")
running mojo program via vs code
Running program

Conclusion

This guide has walked you through installing Mojo, creating a project, setting up VS Code, and running your programs. Each step is well-researched, tested, and verified which is the aim of Syntax Scenarios so that beginner programmers can easily find and understand what they are looking for. With VS Code, you now have a strong setup for coding with Mojo, making it easier to work on complex tasks. For more detailed information and advanced topics, you can visit the official Mojo documentation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top