---
# System prepended metadata

title: p4環境設定 for Mac M1/M2(arm64)

---

# p4環境設定 for Mac M1/M2(arm64)

:::info
這篇是給正在為[p4tutorial](https://github.com/p4lang/tutorials)進行環境設定的人寫的
我的主機環境：
macOS14.6、macbook Air2020(Ｍ1)
:::
## 虛擬機
用[UTM](https://mac.getutm.app)對arm64生態較友好（virtualbox適配arm的版本較少，不推薦)
## OS
推薦Ubuntu 24.04/22.04/20.04 ，去網站找.iso檔（記得要找arm64 desktop 版本而不是server版）
## 將OS匯入虛擬機
![截圖 2024-08-06 上午11.48.36](https://hackmd.io/_uploads/HkUrlglcA.png)
- 點擊左上角＋號
- 虛擬化（兔子圖標）
- 點擊linux
- 按照這樣設定![image](https://hackmd.io/_uploads/H1U1Wle50.png)
- 開機iso映像檔選擇剛剛下載的之後下一步
- 記憶體4096MB
- 儲存空間看自己，我選64GB
- 然後一直下一步到完成建立

## 開始建立環境
:::info
開機如果有跳出訊息說install Ubuntu 就先給他完成安裝後重啟虛擬機(重要，因為要先設定使用者名稱跟分割磁碟之類的東西)
:::
接下來運行（要一段時間）
```
$ sudo apt install git    
$ git clone https://github.com/jafingerhut/p4-guide
$ ./p4-guide/bin/install-p4dev-v7.sh
```
```install-p4dev-v7```可以替換為不同版本，請參照[此文](https://github.com/jafingerhut/p4-guide/blob/master/bin/README-install-troubleshooting.md#quick-instructions-for-successful-install-script-run)
## 測試
:::warning
安裝完成後記得要輸入```source p4setup.bash```不然會沒辦法跑

＊You can add the ```source p4setup.bash``` line to your ```$HOME/.bashrc```file, so that it will automatically be run for you in any new bash shell you create.
:::

在```＄ Home/``` clone下來測試
```
$ git clone https://github.com/p4lang/tutorials
$ cd tutorials/exercises/basic
$ cp solution/basic.p4 basic.p4
$ make run
```
如果成功看到 ```mininet>```,恭喜你成功了

## Q&A
### no mudule named p4.v1
```
$ make run
mkdir -p build pcaps logs
p4c-bm2-ss --p4v 16 --p4runtime-files build/basic.p4.p4info.txt -o build/basic.json basic.p4
sudo python ../../utils/run_exercise.py -t pod-topo/topology.json -j build/basic.json -b simple_switch_grpc
Traceback (most recent call last):
File "../../utils/run_exercise.py", line 33, in
import p4runtime_lib.simple_controller
File "/home/sundar/p4lang/tutorials/utils/p4runtime_lib/simple_controller.py", line 22, in
import bmv2
File "/home/sundar/p4lang/tutorials/utils/p4runtime_lib/bmv2.py", line 15, in
from switch import SwitchConnection
File "/home/sundar/p4lang/tutorials/utils/p4runtime_lib/switch.py", line 20, in
from p4.v1 import p4runtime_pb2
ImportError: No module named p4.v1
../../utils/Makefile:35: recipe for target 'run' failed
make: *** [run] Error 1
```
以上錯誤訊息會出現如果你沒有先輸入command ```source p4setup.bash```