M1 MacにUbuntu 22.04を入れてみた

# はじめに
自分用メモです。ReactやVueなど、フロントエンドを試してみたくなったので、まずは環境としてUbuntuを入れてみることに。
[Multipass](https://canonical.com/multipass)というものが[Homebrew](https://brew.sh/ja/)で簡単にインストール出来るようなので、普段放置しているHomebrewのアップデートから。

# Homebrewのアップデート
```
% brew --version
Homebrew 4.3.9

% brew update
<略>
% brew upgrade
<略>
% brew cleanup

% brew --version       
Homebrew 4.4.16
```
4.3.9から4.4.16まで上がりました！
※brew upgradeはめちゃくちゃ時間かかりました。そんなに古くなれればやらなくて良いかも。

# Multipassのインストール
```
% brew install multipass

==> Downloading https://raw.githubusercontent.com/Homebrew/homebrew-cask/7c87874abd9c74a463f70d7860389484a7dc8d2e/Casks/m/multipass.rb
############################################################################################################################################################# 100.0%
==> Downloading https://github.com/canonical/multipass/releases/download/v1.15.0/multipass-1.15.0+mac-Darwin.pkg
==> Downloading from https://objects.githubusercontent.com/github-production-release-asset-2e65be/114128199/5d320799-6c98-42e0-8c4f-45b57a08b655?X-Amz-Algorithm=AWS
############################################################################################################################################################# 100.0%
==> Installing Cask multipass
==> Running installer for multipass with sudo; the password may be necessary.
Password:
installer: Package name is multipass
installer: Installing at base path /
installer: The install was successful.
🍺  multipass was successfully installed!
```
# Ubuntu 22.04のVMを作成
```
% multipass launch --name ubuntu --cpus 1 --memory 1G --disk 5G 22.04
Launched: ubuntu

% multipass ls

Name                    State             IPv4             Image
ubuntu                  Running           192.168.205.3    Ubuntu 22.04 LTS

% multipass info ubuntu
Name:           ubuntu
State:          Running
Snapshots:      0
IPv4:           192.168.205.3
Release:        Ubuntu 22.04.5 LTS
Image hash:     65a95b38656a (Ubuntu 22.04 LTS)
CPU(s):         1
Load:           0.02 0.06 0.02
Disk usage:     1.6GiB out of 4.8GiB
Memory usage:   150.5MiB out of 962.2MiB
Mounts:         --
```
おおー、Ubuntuがもう動いている！
ログインしてみます。

```
% multipass shell ubuntu

Welcome to Ubuntu 22.04.5 LTS (GNU/Linux 5.15.0-130-generic aarch64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Thu Jan 16 20:36:25 JST 2025

  System load:             0.08
  Usage of /:              34.7% of 4.68GB
  Memory usage:            19%
  Swap usage:              0%
  Processes:               94
  Users logged in:         0
  IPv4 address for enp0s1: 192.168.205.3
  IPv6 address for enp0s1: fda7:bed3:4a0d:9d17:5054:ff:fe86:ad12


Expanded Security Maintenance for Applications is not enabled.

3 updates can be applied immediately.
3 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status

New release '24.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubuntu@ubuntu:~$ 

```
おおー動いた。
```
ubuntu@ubuntu:~$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
tmpfs              98528    1120     97408   2% /run
/dev/sda1        4903276 1702112   3184780  35% /
tmpfs             492628       0    492628   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
/dev/sda15         99791    6427     93365   7% /boot/efi
tmpfs              98524       4     98520   1% /run/user/1000

ubuntu@ubuntu:~$ sudo apt update
Hit:1 http://ports.ubuntu.com/ubuntu-ports jammy InRelease
Hit:2 http://ports.ubuntu.com/ubuntu-ports jammy-updates InRelease
Hit:3 http://ports.ubuntu.com/ubuntu-ports jammy-backports InRelease
Hit:4 http://ports.ubuntu.com/ubuntu-ports jammy-security InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
3 packages can be upgraded. Run 'apt list --upgradable' to see them.

ubuntu@ubuntu:~$ exit
logout
% 
```
普通にsudoなども動きました。
exitでシェルから抜けます。

VMの停止、再開はこんな感じ。
```
% multipass stop ubuntu  
% multipass ls         
Name                    State             IPv4             Image
ubuntu                  Stopped           --               Ubuntu 22.04 LTS

% multipass start ubuntu
% multipass ls          
Name                    State             IPv4             Image
ubuntu                  Running           192.168.205.3    Ubuntu 22.04 LTS
```
疎通確認のためpingを実行
```
% ping 192.168.205.3
PING 192.168.205.3 (192.168.205.3): 56 data bytes
64 bytes from 192.168.205.3: icmp_seq=0 ttl=64 time=0.593 ms
64 bytes from 192.168.205.3: icmp_seq=1 ttl=64 time=0.914 ms
64 bytes from 192.168.205.3: icmp_seq=2 ttl=64 time=1.128 ms
^C
--- 192.168.205.3 ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.593/0.878/1.128/0.220 ms
``` 

# フォルダ共有
mac側とフォルダの共有もしたい。
mac側の ~/ubuntuと、VM側の ~/share を共有する例。
```
% mkdir ~/ubuntu
% echo hello >~/ubuntu/hello.txt

% multipass mount ~/ubuntu ubuntu:~/share  

% multipass shell ubuntu                
Last login: Thu Jan 16 20:36:25 2025 from 192.168.205.1

ubuntu@ubuntu:~$ cd share
ubuntu@ubuntu:~/share$ ls -l
total 4
-rw-r--r-- 1 ubuntu ubuntu 6 Jan 16 21:01 hello.txt

ubuntu@ubuntu:~/share$ cat hello.txt 
hello
```
hello.txtも見れました。

# おわりに
とにかくめちゃくちゃ簡単にUbuntuの環境が作れました！
sshでログインなど、色々出来そうですが、とりあえずこの辺で。


# 参考
https://qiita.com/empty948/items/684a0c1e515fa3f2997d

https://qiita.com/k-keita/items/4cf8c697bacba38ab0f5

https://qiita.com/satokaz/items/ab974af5632d1389add2

https://zenn.dev/s_ryuuki/articles/db7eb23dde7084