LoginSignup
6
7

Mojo🔥 - ダウンロードからHelloWorldまで

Posted at

すごい早いと言われているMojo🔥というプログラミング言語がダウンロード可能になっていたで試してみます。

これまではプレリリース的な感じでクラウド版だけで利用できましたが、最近ダウンロードしてローカルで実行できるようになっていましたね。

image.png

現時点はLinux向けのみ公開とのことなので、WSL2上でのローカルダウンロード版のMojo🔥のセットアップ方法をまとめます。

環境

  • WSL2
  • Ubuntu 22.04

1. Modularアカウントの作成

Mojoの公式サイトへ行ってDownload Nowボタンを押します。

するとアカウント作成を促されるので仕方なく作ります。

メールアドレスを入力して6桁のPIN認証をすれば完了するのですぐ終わりました。

image.png

2. ダウンロードとインストール

認証が終わるとダウンロード方法が説明されているページに自動的に切り替わるので、それを参考にMojoをインストールしていきます。

image.png

今回Windowsのブラウザでページを開いたのでWindows->WSL2->Ubuntu22.04上での実行が自動で表示されているみたいですね。

左下のSet up on WindowsのところはMac(Appleシリコン版)とかMac(Intel版)Linuxにも切り替えられて、Intel版MacではDocker使って入れる方法が書かれていました。Appleシリコン版Macでは、Linuxじゃないのになぜかそのまま入れられるみたいな感じでした。

今回Windows版の方法に従ってやっていきますが、VSCodeやらMojoの拡張機能やらをインストールするように書かれているのはいったん無視して03. In the Ubuntu terminal, install the Modular CLI:

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

を実行します。

3分ぐらい待つと↓が表示されてModular CLIのインストールが終わります。

  __  __           _       _
 |  \/  | ___   __| |_   _| | __ _ _ __
 | |\/| |/ _ \ / _` | | | | |/ _` | '__|
 | |  | | (_) | (_| | |_| | | (_| | |
 |_|  |_|\___/ \__,_|\__,_|_|\__,_|_|

Welcome to the Modular CLI!
For info about this tool, type "modular --help".

To install Mojo🔥, type "modular install mojo".

For Mojo documentation, see https://docs.modular.com/mojo.
To chat on Discord, visit https://discord.gg/modular.
To report issues, go to https://github.com/modularml/mojo/issues.

次に04. Install the Mojo SDK:

modular install mojo

を実行します。

5分ほど経つと↓のように怒られます

The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt install python3.10-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: /home/ubuntu/.modular/pkg/packages.modular.com_mojo/venv/bin/python3

modular: error: failed to run python:

Pythonが必要なようです。まあそれはそうですね。Python3.10をインストールしてからやってみます。

sudo apt install python3.10-venv
modular install mojo

さっきインストールしたときのごみが残っているようです。

modular: error: failure detected - please run `modular clean` and try again
==========================
Failure Information:
        - Creating symlink for /home/****/.modular/pkg/packages.modular.com_mojo/lib/liblldb.so.18.0.0git -> /home/****/.modular/pkg/packages.modular.com_mojo/lib/liblldb.so failed: File exists
        - Creating symlink for /home/****/.modular/pkg/packages.modular.com_mojo/lib/libMojoJupyter.so.18git -> /home/****/.modular/pkg/packages.modular.com_mojo/lib/libMojoJupyter.so failed: File exists
        - Creating symlink for /home/****/.modular/pkg/packages.modular.com_mojo/lib/liblldb.so.18.0.0git -> /home/****/.modular/pkg/packages.modular.com_mojo/lib/liblldb.so.18git failed: File exists
        - Creating symlink for /home/****/.modular/pkg/packages.modular.com_mojo/lib/libKGENCompilerRTShared.so.18git -> /home/****/.modular/pkg/packages.modular.com_mojo/lib/libKGENCompilerRTShared.so failed: File exists
        - Creating symlink for /home/****/.modular/pkg/packages.modular.com_mojo/lib/libMojoLLDB.so.18git -> /home/****/.modular/pkg/packages.modular.com_mojo/lib/libMojoLLDB.so failed: File exists
        - Creating symlink for /home/****/.modular/pkg/packages.modular.com_mojo/lib/libLLCLRuntimeGlobals.so.18git -> /home/****/.modular/pkg/packages.modular.com_mojo/lib/libLLCLRuntimeGlobals.so failed: File exists
        - Creating symlink for /home/****/.modular/pkg/packages.modular.com_mojo/lib/libMSupportGlobals.so.18git -> /home/****/.modular/pkg/packages.modular.com_mojo/lib/libMSupportGlobals.so failed: File exists
==========================

cleanしてから再度インストールします。(最初からPythonが入っている場合はこの操作は多分要らない)

modular clean
modular install mojo

インストールできたっぽい。

🔥 Mojo installed! 🔥

Mojo's Python virtual environment created at /home/ubuntu/.modular/pkg/packages.modular.com_mojo/venv

Now run the following commands if you are using bash:

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

If you are using ZSH, run the following commands:

echo 'export MODULAR_HOME="/home/ubuntu/.modular"' >> ~/.zshrc
echo 'export PATH="/home/ubuntu/.modular/pkg/packages.modular.com_mojo/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

Then enter 'mojo' to start the Mojo REPL.

For tool help, enter 'mojo --help'.
For more docs, see https://docs.modular.com/mojo.

環境変数を設定しろと書いてあるのでコピペして実行する。

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

これでインストールは完了。

対話形式でHelloWorld

ターミナルでmojoと打ち込む。

$ mojo
Welcome to Mojo! 🔥

Expressions are delimited by a blank line.
Type `:quit` to exit the REPL and `:mojo help` for further assistance.

  1>

Pythonぽいですね。

  1> print("Hello, world!")
  2.
Hello, world!
  2>

print("Hello, world!")と打って2回Enter押したらHello, world!と表示されました。
2回押さなけらばならないのはなんかめんどくさいですし、表示されるまで若干時間かかりました。

処理が終わったら:quitで脱出します。

  2> :quit
Process 14661 exited with status = 9 (0x00000009) killed
$

なるほど。

スクリプトファイルからHelloWorld

Mojoのスクリプトファイルの拡張子は.mojo.🔥だそうです。.🔥はきもいのでhello.mojoで作ります。

hello.mojo
fn main():
    print("Hello, world!")

ターミナルから実行します。

$ mojo hello.mojo
Hello, world!
$

なるほど。

まとめ

Ubuntuの環境さえ整っていれば、トータルで10分ぐらいでダウンロードからHelloWorldまで行けるので、簡単に使えるようになると思います。

暇だったら試してみるのもいいかもしれないですね。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7