# Building Open5GS from Sources
###### tags: `Open5gs`
[TOC]
## 1. Getting MongoDB
```linux=
$ sudo apt update
$ sudo apt install mongodb
$ sudo systemctl start mongodb
$ sudo systemctl enable mongodb
```
## 2. Setting up TUN device
* Create the TUN device with the interface name ogstun.
```linux=
$ sudo ip tuntap add name ogstun mode tun
$ sudo ip addr add 10.45.0.1/16 dev ogstun
$ sudo ip addr add 2001:db8:cafe::1/48 dev ogstun
$ sudo ip link set ogstun up
```
## 3. Building Open5GS
* Install the dependencies for building the source code.
```linux=
$ sudo apt install python3-pip python3-setuptools python3-wheel ninja-build build-essential flex bison git libsctp-dev libgnutls28-dev libgcrypt-dev libssl-dev libidn11-dev libmongoc-dev libbson-dev libyaml-dev libnghttp2-dev libmicrohttpd-dev libcurl4-gnutls-dev libnghttp2-dev libtins-dev libtalloc-dev meson
```
* Git clone.
```linux=
$ git clone https://github.com/open5gs/open5gs
```
* To compile with meson:
```linux=
$ cd open5gs
$ meson build --prefix=`pwd`/install
$ ninja -C build
```
:::danger
The test program has been run on [VirtualBox - CPU: 1, Memory: 4.00 GB]
:::
* Check whether the compilation is correct.
```linux=
$ ./build/tests/attach/attach ## EPC Only
$ ./build/tests/registration/registration ## 5G Core Only
```
* Run all test programs as below.
```linux=
$ cd build
$ meson test -v
```
* You need to perform the installation process.
```linux=
$ cd build
$ ninja install
$ cd ../
```
# Install the WebUI of Open5GS
- Node.js is required to build WebUI of Open5GS
```linux=
$ sudo apt install curl
$ curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
$ sudo apt install nodejs
```
- Install the dependencies to run WebUI
```linux=
$ cd webui
$ npm ci --no-optional
```
- The WebUI runs as an npm script.
```linux=
$ npm run dev
```
- **Connect to http://localhost:3000 and login with admin account.**
:::info
Username : admin
Password : 1423
:::
# Connect UERANSIM
## 1. Configure Open5GS
* Modify open5gs/install/etc/open5gs/amf.yaml to set the NGAP IP address, PLMN ID, TAC and NSSAI.

* Modify open5gs/install/etc/open5gs/upf.yaml to set the GTP-U and PFCP IP address.

## 2. Running Open5GS
* Running Open5GS method 1: 需開多個視窗每個NF分別執行
```linux=
$ ./install/bin/open5gs-${nf}d
```
* Running Open5GS method 2: 一次啟動5gc的部分
```linux=
$ ./build/tests/app/5gc
```
注意:這裡執行的yaml檔為open5gs/build/configs/sample.yaml需修改這個yaml檔。
## 3. Turn on gNB and UE
- Turn on gNB
```linux=
$ cd ~/UERANSIM
$ build/nr-gnb -c config/open5gs-gnb.yaml
```
- Turn on UE
```linux=
$ cd ~/UERANSIM
$ sudo build/nr-ue -c config/open5gs-ue.yaml
```
## 4. Test ping
```linux=
$ ping -I uesimtun0 google.com
```
# Test
## Compile code
* To compile with meson:
```linux=
$ ninja -C build
```
* Check whether the compilation is correct.
```linux=
$ cd build
$ meson test -v
```
* Install Open5GS and initialize configuration files.
```linux=
$ rm -Rf ../install
$ ninja install
```
## Add printf
* Add printf in ~/open5gs/src/amf/app.c

* Result

## Add function
* Add function in ~/open5gs/src/amf/init.c

* Call function in ~/open5gs/src/amf/app.c

* Result
