Site Overlay

在Ubuntu 18.04上安装NS3.34和NS3-gym

主要参考胡博文章,在18.04的WSL和Vultr主机上测试上可行。

安装依赖

gcc-9

首先,安装gcc-9和g++-9,并设为默认,否则会有部分的头文件报错,形如:

/attribute-container-accessor-helper.h:138:79: error: expected ‘>’ before ‘(’ token
  138 | template <typename V, typename T, template <typename...> class U, typename ...I,

参考:https://github.com/tkn-tub/ns3-gym/issues/67

首先添加软件源:

sudo echo "deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic main" >> /etc/apt/sources.list
sudo echo "deb-src http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic main" >> /etc/apt/sources.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 60C317803A41BA51845E371A1E9377A2BA9EF27F

然后安装并设为默认gcc:

sudo apt update
# sudo apt upgrade
sudo apt install gcc-9 g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9
sudo update-alternatives --config gcc

参考:https://blog.csdn.net/chy_65199/article/details/119109189

zeromq

zeromq本体可以使用apt安装,然后再编译安装一下cpp的headers就行。如果没有headers,会报类似下面的错:

../src/opengym/model/opengym_interface.cc:192:36: error: ‘zmq::send_flags’ has not been declared
   m_zmq_socket.send (request, zmq::send_flags::none);
                                    ^~~~~~~~~~

参考:https://github.com/tkn-tub/ns3-gym/issues/33

sudo apt install libzmq5 libzmq3-dev

cpp headers要求的cmake版本高于18.04自带的,因此下载一个cmake备用:

cd /tmp
wget -O /tmp/cmake-3.26.3-linux-x86_64.tar.gz https://github.com/Kitware/CMake/releases/download/v3.26.3/cmake-3.26.3-linux-x86_64.tar.gz
tar -zxvf cmake-3.26.3-linux-x86_64.tar.gz
cd ~

然后使用刚才下载的cmake编译安装zeromq的cpp headers:

cd ~
wget https://github.com/zeromq/cppzmq/archive/refs/tags/v4.9.0.tar.gz
tar -zxvf v4.9.0.tar.gz
cd cppzmq-4.9.0/
mkdir build
cd build
/tmp/cmake-3.26.3-linux-x86_64/bin/cmake ..
sudo make -j$(nproc) install

参考:https://github.com/zeromq/cppzmq

NS3依赖

应该有一些可以删减的空间,但是先不管了:

sudo apt install gcc g++ python gcc g++ python python-dev mercurial python-setuptools git qt5-default gir1.2-goocanvas-2.0 python-gi python-gi-cairo python-pygraphviz python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar gdb valgrind uncrustify doxygen graphviz imagemagick texlive texlive-extra-utils texlive-latex-extra texlive-font-utils texlive-lang-portuguese dvipng latexmk python-sphinx dia gsl-bin libgslcblas0 libgsl-dev flex bison libfl-dev tcpdump sqlite sqlite3 libsqlite3-dev libxml2 libxml2-dev cmake libc6-dev libc6-dev-i386 libclang-dev llvm-dev automake libgtk2.0-0 libgtk2.0-dev vtun lxc libboost-signals-dev libboost-filesystem-dev g++ python3 python3-dev pkg-config sqlite3 cmake libc6-dev libc6-dev-i386 libclang-6.0-dev llvm-6.0-dev automake python3-pip python-dev python-pygraphviz python-kiwi

protobuf

版本不能太新也不能太老,必须大于3.6,否则会报错:

wrapper_Message.cpp:149:30: error: ‘class google::protobuf::Message’ has no member named ‘ByteSizeLong’; did you mean ‘ByteSize’?
     R_xlen_t size = message->ByteSizeLong();
                              ^~~~~~~~~~~~
                              ByteSize

参考:https://github.com/eddelbuettel/rprotobuf/issues/70

选了3.10.x,看着顺眼,并非一定要这个版本。

cd ~
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git checkout 3.10.x
git submodule update --init --recursive
./autogen.sh
./configure
make -j$(nproc)
make check -j$(nproc)
sudo make install
sudo ldconfig # refresh shared library cache.

参考:https://github.com/protocolbuffers/protobuf/tree/3.10.x/src

Python 3.7

protobuf要求python版本不能低于3.7,而18.04只有3.6,需要装一个3.7:

sudo apt install python3.7-dev

这样安装的python在调用时必须显式指明,也就是说,运行脚本时应当使用python3.7 main.py的方式,使用pip安装包时也应当使用python3.7 -m pip install numpy的方式。

这个的问题是,可视化的那一堆pip包是安装给3.6的,而尝试用pip给3.7装的包ns3不认,所以实际上3.7是用不了可视化的。(待解决)

也可以使用update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1 && update-alternatives --config python3命令替换默认的python,但是似乎会影响一些系统组件,比如apt会报错no module named 'apt_pkg',目前暂未解决。

参考:https://blog.csdn.net/White_Idiot/article/details/78240298

下载NS3.34

使用3.34版本是因为之前的测试结果,在18.04上使用gcc-7.4能安装3.34和3.35,其他版本未作测试,目前仅测试了3.34版本和ns3-gym的兼容性。

cd ~
wget https://www.nsnam.org/releases/ns-allinone-3.34.tar.bz2 # 3.34或3.35,之后的版本会提示gcc版本低于8,暂未测试
tar -jxvf ns-allinone-3.34.tar.bz2

编译测试NS3

cd ns-allinone-3.34
# ./build.py
cd ns-3.34
./waf configure --enable-test --enable-examples
./waf

运行测试

python3.7 test.py -c core

运行可视化

python3.7 waf --run third --vis

在此基础上安装ns3-gym

准备源码

cd ~/ns-allinone-3.34/ns-3.34/contrib
git clone https://github.com/tkn-tub/ns3-gym.git ./opengym
cd opengym/
git checkout app

如果之前没有升级gcc版本到9,则需要删除一个有问题的example避免报错,本质上属于掩耳盗铃,不建议。

编辑contrib/opengym/examples/wscript,注释以下内容:

    obj = bld.create_ns3_program(
        "interference-pattern", ["core", "internet", "wifi", "opengym"]
    )
    obj.source = ["interference-pattern/sim.cc", "interference-pattern/mygym.cc"]

编译

cd ~/ns-allinone-3.34/ns-3.34
./waf configure --enable-test --enable-examples
./waf build
# python3.7 waf configure --enable-test --enable-examples
# python3.7 waf build

安装python module

cd ~/ns-allinone-3.34/ns-3.34/contrib/opengym/
python3.7 -m pip install cython
python3.7 -m pip install --user ./model/ns3gym

运行测试

测试一:

cd ~/ns-allinone-3.34/ns-3.34/contrib/opengym/examples/opengym/
python3.7 simple_test.py

测试二:

# Terminal 1
cd ~/ns-allinone-3.34/ns-3.34
python3.7 waf --run "opengym"
# Terminal 2
cd ~/ns-allinone-3.34/ns-3.34/contrib/opengym/examples/opengym/
python3.7 test.py --start=0

参考:https://github.com/tkn-tub/ns3-gym/tree/app

复制这些就行

编译

sudo echo "deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic main" >> /etc/apt/sources.list
sudo echo "deb-src http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic main" >> /etc/apt/sources.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 60C317803A41BA51845E371A1E9377A2BA9EF27F

sudo apt update
# sudo apt upgrade
sudo apt install -y gcc-9 g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9
sudo update-alternatives --config gcc

sudo apt install -y libzmq5 libzmq3-dev

cd /tmp
wget -O /tmp/cmake-3.26.3-linux-x86_64.tar.gz https://github.com/Kitware/CMake/releases/download/v3.26.3/cmake-3.26.3-linux-x86_64.tar.gz
tar -zxvf cmake-3.26.3-linux-x86_64.tar.gz
cd ~

cd ~
wget https://github.com/zeromq/cppzmq/archive/refs/tags/v4.9.0.tar.gz
tar -zxvf v4.9.0.tar.gz
cd cppzmq-4.9.0/
mkdir build
cd build
/tmp/cmake-3.26.3-linux-x86_64/bin/cmake ..
sudo make -j$(nproc) install

sudo apt install -y gcc g++ python gcc g++ python python-dev mercurial python-setuptools git qt5-default gir1.2-goocanvas-2.0 python-gi python-gi-cairo python-pygraphviz python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar gdb valgrind uncrustify doxygen graphviz imagemagick texlive texlive-extra-utils texlive-latex-extra texlive-font-utils texlive-lang-portuguese dvipng latexmk python-sphinx dia gsl-bin libgslcblas0 libgsl-dev flex bison libfl-dev tcpdump sqlite sqlite3 libsqlite3-dev libxml2 libxml2-dev cmake libc6-dev libc6-dev-i386 libclang-dev llvm-dev automake libgtk2.0-0 libgtk2.0-dev vtun lxc libboost-signals-dev libboost-filesystem-dev g++ python3 python3-dev pkg-config sqlite3 cmake libc6-dev libc6-dev-i386 libclang-6.0-dev llvm-6.0-dev automake python3-pip python-dev python-pygraphviz python-kiwi

cd ~
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git checkout 3.10.x
git submodule update --init --recursive
./autogen.sh
./configure
make -j$(nproc)
make check -j$(nproc)
sudo make install
sudo ldconfig # refresh shared library cache.

sudo apt install -y python3.7-dev

cd ~
wget https://www.nsnam.org/releases/ns-allinone-3.34.tar.bz2 # 3.34或3.35,之后的版本会提示gcc版本低于8,暂未测试
tar -jxvf ns-allinone-3.34.tar.bz2

cd ns-allinone-3.34
# ./build.py
cd ns-3.34
# ./waf configure --enable-test --enable-examples
# ./waf

cd ~/ns-allinone-3.34/ns-3.34/contrib
git clone https://github.com/tkn-tub/ns3-gym.git ./opengym
cd opengym/
git checkout app

cd ~/ns-allinone-3.34/ns-3.34
./waf configure --enable-test --enable-examples
./waf build
# python3.7 waf configure --enable-test --enable-examples
# python3.7 waf build

cd ~/ns-allinone-3.34/ns-3.34/contrib/opengym/
python3.7 -m pip install cython
python3.7 -m pip install --user ./model/ns3gym

测试

cd ~/ns-allinone-3.34/ns-3.34/
python3.7 test.py -c core
python3.7 waf --run third --vis
cd ~/ns-allinone-3.34/ns-3.34/contrib/opengym/examples/opengym/
python3.7 simple_test.py
# Terminal 1
cd ~/ns-allinone-3.34/ns-3.34
python3.7 waf --run "opengym"

# Terminal 2
cd ~/ns-allinone-3.34/ns-3.34/contrib/opengym/examples/opengym/
python3.7 test.py --start=0

5 thoughts on “在Ubuntu 18.04上安装NS3.34和NS3-gym

  1. 你好,我用的是ubuntu18.04+ns3.35,在重新build的时候报错error: ‘zmq::send_flags’ has not been declared,请问不用CMAKE可以解决吗?

    1. 请问什么叫做“重新build的时候”,具体是指的那一部分?我用自己的电脑跑了一遍是没有问题的。额外下载的cmake只是由于cppzmq要求的cmake版本比较高,这个cmake只有cppzmq用到了,其他的不受影响的。

      不过我的建议是不如换成20.04,18.04缺了很多依赖,不缺的依赖也有不少有版本过低的问题,而且18.04太古老了,在五月底就结束支持,如果不是特别需要的话不如升级一下可以省很多力气。

  2. 感谢,我已经换成ubuntu20.04了,我查了一下确实有说法是18.04提供的库太老了的原因。另外我还想问一下,ns3-gym需要安装在anaconda的虚拟环境下吗,还是只要和py文件在一个linux系统下就行啊?

    1. 虚拟环境不是必须的,我就没有用虚拟环境。而且感觉ns3的依赖非常复杂,使用虚拟环境可能比想象中的更加麻烦。如果需要将ns3的开发环境和其他的隔开,可以考虑用docker进行ns3的开发。

  3. 好的,非常感谢,虚拟环境下用ns3确实很麻烦,毕竟下载各种安装包的时候经常把环境搞坏,我现在直接用双系统了,以后有机会再尝试docker,谢谢🙏

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据