What is NS2 and Why Do You Need It?
If you’re stepping into the world of network simulations or ooking to install NS2 in Ubuntu, chances are you’ve already come across NS2 (Network Simulator 2). It’s like the secret weapon for students, researchers, and professionals who need to model complex network behaviors without the expense of real-world hardware.
NS2 is a discrete event network simulator, built for simulating wired and wireless networks. You can test routing algorithms, protocol performance, and traffic models, all from your Ubuntu machine. And the best part? It’s open-source and highly customizable.
What You Need Before You Start
Before diving in, make sure your setup is ready. Here’s your NS2 starter pack:
- Ubuntu (20.04 or 22.04 recommended)
- A stable internet connection
- At least 4GB RAM
- Terminal access and basic command-line know-how
First, let’s update your system:
bash
sudo apt update && sudo apt upgrade -y
Step 1: Install Dependencies
NS2 depends on a mix of libraries and compilers. Let’s install them all in one go:
bash
sudo apt install build-essential autoconf automake libxmu-dev gcc g++ libtool libotcl1 libotcl-dev tcl8.6 tcl8.6-dev tk8.6 tk8.6-dev libx11-dev libxt-dev -y
This ensures your system has all the tools NS2 needs to build and run smoothly.
Step 2: Download the NS2 Source Package
The most stable version is NS2.35, so let’s grab that:
bash
cd ~
wget https://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz
Then extract it:
bash
tar -xvzf ns-allinone-2.35.tar.gz
cd ns-allinone-2.35
🛠️ Step 3: Fix Compatibility Issues (for Ubuntu 20.04/22.04)
Edit Headers for Compatibility with Modern GCC
Edit this file:
bash
nano ns-2.35/linkstate/ls.h
Replace:
cpp
#include <std::map>
With:
cpp
#include <map>
using namespace std;
Then, open:
bash
nano ns-2.35/common/packet.h
Comment out these lines:
cpp
#define NIL 0
#define TRUE 1
Save and close. These tweaks help NS2 play nice with newer compilers.
Step 4: Install NS2
Now, let’s build it:
bash
./install
This will take several minutes. Watch for any red error messages—those are important.
Step 5: Set Up Environment Variables
To run NS2 from anywhere in your terminal, update your .bashrc
file:
bash
nano ~/.bashrc
Add this at the end:
bash
# NS2 environment
export PATH=$PATH:$HOME/ns-allinone-2.35/bin:$HOME/ns-allinone-2.35/tcl8.6.10/unix:$HOME/ns-allinone-2.35/tk8.6.10/unix
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/ns-allinone-2.35/otcl-1.14:$HOME/ns-allinone-2.35/lib
Apply changes:
bash
source ~/.bashrc
Step 6: Test Your NS2 Installation
Run:
bash
ns
If you see a %
prompt, congrats—NS2 is installed! Exit with:
bash
exit
Want to test a simulation?
bash
cd ~/ns-allinone-2.35/ns-2.35/tcl/ex
ns simple.tcl
Then view it:
bash
nam out.nam
Boom. Your first network simulation is live.
Bonus Tips & Troubleshooting
- NS2 not found? Check your
.bashrc
for path typos. - Compile errors? Go back and double-check your edits to the C++ files.
- NAM won’t launch? Make sure X11 is installed and properly configured.
FAQs: Installing NS2 on Ubuntu
Q1. What is NS2 used for?
NS2 is used for simulating computer networks to test performance of routing protocols, congestion control, TCP/IP models, and more—without needing actual network hardware.
Q2. Can I install NS2 on Ubuntu 22.04?
Yes! Just make sure you apply the compatibility fixes during the install process (covered in Step 3). NS2.35 works fine with minor tweaks.
Q3. What’s the difference between NS2 and NS3?
NS2 uses C++ and OTcl, and is more script-heavy. NS3 is newer, uses Python and C++, and is better for modern, real-time simulations. NS2 still has strong community support for academic projects.
Q4. How do I uninstall NS2 if needed?
Just delete the installation directory:
bash
rm -rf ~/ns-allinone-2.35
Also, remove any lines you added in .bashrc
.
Q5. Why do I get segmentation faults when I run nam
?
This usually happens if you didn’t set the correct environment paths or if your system is missing display dependencies. Make sure X11 is installed:
bash
sudo apt install x11-apps
Then try again.
Wrapping Up: You’re Ready to Simulate Like a Pro
And there you have it—your very own NS2 environment up and running on Ubuntu! Whether you’re researching new routing algorithms, testing TCP models, or building simulations for your thesis, NS2 has your back.
Now’s the time to dive in, explore simulation scripts, and start experimenting. The power of network modeling is just a few lines of code away.