How do you get a ROS2 system up and running on a virtual machine, and participating on your local network as if it were a real pc? This tutorial helps you set up two virtual machine clones that can talk to each other without any special hardware other than your own pc, but look to any other device on the ROS network as if they are two full-fledged computers
This tutorial does not delve into ROS2 networking setup, but rather focuses on the plain install. For more information on setting up ROS2 to work without multicast or over a vpn, please see the following articles:
Ubuntu is one of the Linux distributions supported by ROS2 out of the box, with a repository and pre-compiled binaries. For this tutorial, we assume you have already installed Ubuntu 22.04 LTS (Server Edition, 64 bit) on a virtual machine.
Why Ubuntu Server? I recommend Ubuntu server edition because there are some tools used by the server edition that are different in the desktop edition. Installing the server edition and then installing
ubuntu-desktop-minimal
will more closely mimic the installation on a desktop-less server than a full-fledged desktop install from the beginning.
Suggested machine parameters and install steps:
Memory: Assign somewhere between 4096 and 8192 Mb of memory
Hard Disk settings:
In the system –> processor tab, select 2-4 cpus if possible, keeping the slider out of the red region.
Networking: use “Bridged Networking” with your wifi module selected (if possible).
Note: Bridged networking mode is extremely important for ROS2 to see this virtual machine and to be able to communicate with it.
Ubuntu Installation:
install virtual machine additions prerequisites using
sudo apt install -y gcc make perl build-essential
then install virtual machine additions from the virtualbox menu. with your desktop installed and your virtual machine running,
consider
removing snap support. You will not need it and it can hang indefinitely in poor internet conditions
disabling automatic updates:
sudo apt remove unattended-upgrades
if you get a message like:
Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 2131 (uWaiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 2131 (unattended-upgr)
sudo killall unattended-upgr
Finally first update and upgrade your apt installation, power down the machine and save a snapshot.
sudo apt update && sudo apt upgrade -y
Starting from a blank ubuntu server 22.04 vm, check for updates (if the vm has been off for a while, especially) and install the minimal ubuntu-desktop metapackage
sudo apt update && sudo apt upgrade -y
sudo apt install -y ubuntu-desktop-minimal
I also went through the work of removing the firefox snap and its dependencies, using this post
I noted my machine’s ip address
ip a
I did another full upgrade
sudo apt update && sudo apt upgrade -y
I then installed ros2, using the instructions from here
sudo apt install -y software-properties-common
sudo add-apt-repository universe
sudo apt update && sudo apt install -y curl -y
sudo curl -sSL <https://raw.githubusercontent.com/ros/rosdistro/master/ros.key> -o /usr/share/keyrings/ros-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] <http://packages.ros.org/ros2/ubuntu> $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
sudo apt update && sudo apt upgrade -y
sudo apt install -y ros-humble-desktop ros-dev-tools
sudo apt install -y python3-rosdep
sudo apt install -y ros-humble-plotjuggler-ros #for plotting data
sudo apt update && sudo apt install -y \
build-essential \
cmake \
git \
python3-colcon-common-extensions \
python3-flake8 \
python3-pip \
python3-pytest-cov \
python3-rosdep \
python3-setuptools \
python3-vcstool \
wget
sudo rosdep init
rosdep update
I then configured some environment variables in .bashrc
echo "export ROS_DOMAIN_ID=0" >> ~/.bashrc
echo "export ROS_LOCALHOST_ONLY=0" >> ~/.bashrc
echo "source /opt/ros/humble/setup.bash" >> .bashrc
and checked the variables with the command:
printenv | grep -i ROS
First in one terminal window:
ros2 run demo_nodes_cpp talker
and in a second terminal window:
ros2 run demo_nodes_cpp listener
ros2 daemon stop
Shutdown the virtual machine.
Now to clone to two virtual machines. I modified the server’s netplan, according to this post, which helped eliminate getting an identical ip address
sudo nano /etc/netplan/00-installer-config.yaml
from:
# This is the network config written by 'subiquity'
network:
ethernets:
enp0s3:
dhcp4: true
version: 2
to
# This is the network config written by 'subiquity'
network:
ethernets:
enp0s3:
dhcp4: true
dhcp-identifier: mac
version: 2
This config can be modified further as needed, but since I was just getting a demo up and running, I didn’t need to define static ip’s
sudo netplan try
sudo netplan apply
Next, I shutdown the vm and made a linked clone of it
sudo shutdown now
I then started both the original and clone vms and checked my work:
vm 1:
ip a
vm2:
ip a
vm1:
ros2 run demo_nodes_cpp talker
vm2:
ros2 run demo_nodes_cpp listener