Ubuntu 安裝 ns-3最全 教程
1. 環境更新
sudo apt update
sudo apt install git
2. Ns3 最低依賴要求
2.1 安裝依賴
安裝依賴網址:根據自己安裝的版本安裝對應依賴。
https://www.nsnam.org/wiki/Installation
Ubuntu/Debian/Mint
以下軟件包列表在 Ubuntu 22.10 版本中應該是準確的;其他版本或其他基于 Debian 的系統可能略有不同。Ubuntu 16.04 LTS 版本可能是已知可與最新 ns-3 版本配合使用的最古老的版本。
軟件包列表取決于您嘗試構建的 ns-3 版本。
版本 3.36 及更高版本的最低要求:
sudo apt install g++ python3 cmake ninja-build git
如果您只下載源存檔,則不需要 Git。
Ubuntu 自帶 “make” 構建工具,但如果缺少它(其他一些基于 Debian 的發行版上),可能需要安裝 make
或 ninja-build
。Ninja 是 make 的替代品。
推薦用于 3.37 及更高版本:
Ccache 是一種編譯器緩存優化,加快構建速度,但會額外使用多達 5 GB 的磁盤空間。
sudo apt install ccache
注意:對于 Ubuntu 20.04 及更早版本,apt 提供的 ccache 版本(3.7.7 或更早)可能無法提供性能優勢,建議安裝版本 4 或更高。對于 Ubuntu 22.04 及更高,可以直接使用 apt 安裝。
版本 3.30-3.35 的最低建議要求:
sudo apt install g++ python3
3.29 及更早版本的最低建議要求:
sudo apt install g++ python2
注意:
從 ns-3.30 版本(2019 年 8 月)開始,ns-3 默認使用 Python 3,但早期版本依賴 Python 2 包,建議至少使用 Python 2 解釋器。
2022 年 1 月(ns-3.36 及 ns-3-dev),最低 g++ 版本為 g+±8。
Ubuntu 18.04/16.04 默認 g++ 版本較老,可通過 StackOverflow 和 Launchpad 獲取更高版本。
3. 可選/推薦組件依賴
3.1 Python 可視化工具和綁定(ns-3.37 及更高)
sudo apt install python3-pip
python3 -m pip install --user cppyy
sudo apt install gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3
3.2 Python API 用戶(3.30 ~ 3.36)
sudo apt install g++ python3 python3-dev pkg-config sqlite3 cmake
3.3 Python(開發)
需要 ns-3-allinone
倉庫(Git 克隆),還需:
sudo apt install python3-setuptools git
3.4 Netanim 動畫工具
sudo apt install qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools
Ubuntu 20.10 及更早版本:
sudo apt install qt5-default
3.5 支持 ns-3-pyviz 可視化工具(3.36 及更早版本)
sudo apt install gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3
3.28 及更早版本:
sudo apt install python-pygraphviz python-kiwi python-pygoocanvas libgoocanvas-dev ipython
3.6 基于 MPI 的分布式仿真
sudo apt install openmpi-bin openmpi-common openmpi-doc libopenmpi-dev
3.7 支持 bake 構建工具
sudo apt install mercurial unzip
3.8 調試 Debug
sudo apt install gdb valgrind
3.9 代碼樣式檢查(自 ns-3.37 起)
sudo apt install clang-format
注意:需要 clang-format-14 到 clang-format-16 版本。
3.10 Doxygen 和內聯文檔
sudo apt install doxygen graphviz imagemagick
sudo apt install texlive texlive-extra-utils texlive-latex-extra texlive-font-utils dvipng latexmk
3.11 ns-3 手冊和教程 (Sphinx)
sudo apt install python3-sphinx dia
ns-3.15 需要 Sphinx >= 1.12,檢查版本用
sphinx-build
3.12 GNU 科學圖書館 (GSL)
sudo apt install gsl-bin libgsl-dev libgslcblas0
3.13 讀取 pcap 數據包
sudo apt install tcpdump
3.14 統計框架數據庫支持
sudo apt install sqlite sqlite3 libsqlite3-dev
3.15 基于 XML 的配置存儲
sudo apt install libxml2 libxml2-dev
3.16 支持生成修改后的 python 綁定(ns-3.36 及更早)
sudo apt install cmake libc6-dev libc6-dev-i386 libclang-dev llvm-dev automake python3-pip
python3 -m pip install --user cxxfilt
需要手動安裝 CastXML 和 Pygccxml(建議源代碼構建)。
3.17 基于 GTK 的配置系統
sudo apt install libgtk-3-dev
3.18 虛擬機和 ns-3
sudo apt install vtun lxc uml-utilities
3.19 openflow 模塊和 Boost 開發庫
sudo apt install libxml2 libxml2-dev libboost-all-dev
4. 下載和編譯 ns-3
cd ~
mkdir workspace
cd workspace
git clone https://gitlab.com/nsnam/ns-3-allinone.git
cd ns-3-allinone
ls
python3 download.py
下載指定版本**(例如 ns-3.44):
python3 download.py -n ns-3.44
5. 編譯和測試
5.1 Debug 編譯
./ns3 configure --build-profile=debug --enable-examples --enable-tests
%8B.assets%2Fimage-20250730164830248.png&pos_id=img-KPP7m60g-1753924561480)
5.2 編譯
./ns3 build
5.3 測試用例執行
./test.py
5.4 顯示編譯版本
./ns3 show profile
%E5%AE%89%E8%A3%85%20ns-3%20%E6%95%99%E7%A8%8B.assets%2Fimage-20250730171923064.png&pos_id=img-fkmvXIGM-1753924561481)
5.5 執行程序
./ns3 run first
5.6 編寫程序
在 scratch/
文件夾下,只能有一個 .cc
文件。
6. NetAnim3.109 編譯
cd ~/workspace/ns-3-allinone/netanim-3.109
qmake
make
編譯完成后會出現一個 NetAnim 執行文件,然后:
./NetAnim