LoginSignup
13
5

More than 1 year has passed since last update.

RaspberryPiでDenoをソースからビルドして使ってみる

Last updated at Posted at 2021-05-16

はじめに

どうも,ボクです。
これまでフロントエンドは敢えて避けてきたのですが,勉強しないといけないなぁということでロゴがかわいいDenoに手を染め始めました。

出張族で日頃から自宅PCに触れないのと数百W消費するPCを常時起動しておくと「電気代高すぎ!」と怒られてしまうので,RaspberryPiを複数台導入し,外出先からRaspberryPi上で作業ができるようにしています。

で,今回RaspberryPiでDenoに手を出そうとした際にハマったので備忘録的にメモを残しておくことにしました。DenoをRaspberryPi上で動かすための悪戦苦闘(というかいつの間にか動いていた)の記録です。

このポストがみなさまの何かの役に立てば幸いです。が,試すときは自己責任でお願いします。

「Deno動かねぇ」

Deno公式ページを参考にし,RaspberryPiのコンソールで以下のコマンドを叩いてインストール,サンプルスクリプトを実行。

$ curl -fsSL https://deno.land/x/install/install.sh | sh
$ deno run https://deno.land/std/examples/welcome.ts

そしたらこんなの出てきた。

cannot execute binary file: Exec format error

で,ふと気づく訳です。「RaspberryPiってARMやん」
fileコマンドでDenoのバイナリ調べたらx86ってキーワード出てくるよ。そりゃ動かんわ。。。

Denoをソースからコンパイル→ビルドするよ。

「Let's クロスコンパイル!」と思ったものの,クロスコンパイルって良い思い出がないので,ターゲットマシン上でコンパイル,ビルドすることに。環境は以下の通り。

  • RaspberryPi 4B (4GBモデル)
  • OS: Raspberry Pi OS

$ uname -aはこんな感じ。

Linux [host] 5.10.17-v8+ #1403 SMP PREEMPT Mon Feb 22 11:37:54 GMT 2021 aarch64 GNU/Linux

事前準備

まずはお決まりのapt-get update; apt-get upgradeでパッケージ群を最新化。その後はビルドに必要な追加パッケージを導入していく。

Denoをソースからビルドする場合は,cargoを使え。となっているのでその準備から。

Rustのインストール

$ curl https://sh.rustup.rs -sSf | sh

Current installation options:1を選んでおけばよろし。そうすると,コンソールに「環境変数追加しておいたぜ」的なメッセージが出てくるので,

$ source $HOME/.profile

とやっておきましょう。念のためちゃんとインストールされているか確認。

$ rustc --version
rustc 1.52.1 (9bc8c42bb 2021-05-09)
$ cargo --version
cargo 1.52.0 (69767412a 2021-04-21)

libssl-devのインストール

# apt-get install libssl-dev

cargo-editのインストール

cargo install cargo-edit

cargo-makeのインストール

$ git clone https://github.com/sagiegurari/cargo-make.git
$ cd cargo-make
$ cargo install --force cargo-make

Denoのビルド

まずはソースの入手からスタート!

ソースの入手

$ git clone --recurse-submodules https://github.com/denoland/deno.git

ビルドターゲットの追加

$ rustup target add wasm32-unknown-unknown
info: downloading component 'rust-std' for 'wasm32-unknown-unknown'
info: installing component 'rust-std' for 'wasm32-unknown-unknown'
info: using up to 500.0 MiB of RAM to unpack components
 13.2 MiB /  13.2 MiB (100 %)   2.3 MiB/s in  5s ETA:  0s
$ rustup target add wasm32-wasi
info: downloading component 'rust-std' for 'wasm32-wasi'
info: installing component 'rust-std' for 'wasm32-wasi'
info: using up to 500.0 MiB of RAM to unpack components
 13.5 MiB /  13.5 MiB (100 %)   2.1 MiB/s in  4s ETA:  0s

ビルド

で,git cloneしてきたディレクトリに移動して

 $ cargo install deno --locked
    Updating crates.io index
  Downloaded deno v1.10.1
  Downloaded 1 crate (2.5 MB) in 1.41s
  Installing deno v1.10.1
  Downloaded dissimilar v1.0.2
  Downloaded radix_trie v0.2.1
  (中略)
   Compiling deno v1.10.1
   Compiling dprint-plugin-typescript v0.45.0
    Building [=======================> ] 643/647
 Finished release [optimized] target(s) in 72m 16s
  Installing /home/xxx/.cargo/bin/deno
   Installed package `deno v1.10.1` (executable `deno`)

ここまでくればビルド完了。かなり時間かかりますが気長にまちましょう。当方環境は上記の通り72m 16sかかりました。

動作確認するよ!

いつものサンプル実行のコマンドを叩くんだ!

$ deno run https://deno.land/std/examples/welcome.ts
Download https://deno.land/std/examples/welcome.ts
Warning Implicitly using latest version (0.96.0) for https://deno.land/std/examples/welcome.ts
Download https://deno.land/std@0.96.0/examples/welcome.ts
Check https://deno.land/std/examples/welcome.ts
Welcome to Deno!

やったー!動いたー!!

おわりに

とりあえず公式サンプルはRaspberryPi上でも動きました。あとは使い込んでみて,おかしいところがないか確認してみないと。
RaspberryPiでDenoを動かすっていう情報が少なかった(無かった?)ので,まとめてみました。Denoライフ楽しみましょう!!

2021-05-19 追記
恐れ多いけどフロントエンド強化月間に乗っかってみる。

13
5
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
13
5