Install ROS2 + Carla on WSL2

Author: Yunhao Cao

In this article I will be describing how to install a Ubuntu Image on WSL2 and how to install ROS2 and connect to a native running instance of Carla on Windows 10 / 11.

Reference: ROAR ROS Setup Doc

📎
The solution we developed here is pretty efficient as virtualization of graphics card is not used and Carla is directly using d3d12 to render. But it is also due to Windows currently not offering Vulkan API on WSL subsystem.
⚠️
Windows Terminal App from Windows Store is highly recommended before this tutorial is proceeded, it provides tab feature to any terminal applications and I found it really useful.

Preview:

ROS-Carla-Bridge in WSL connected to a native Carla UE4 Instance on WSL Host
Other ROS Foxy Simulator GUIs ran in WSL but actually displayed in host Windows OS through WSLg (GPU Accelerated Graphics through Virtual GPU, support OpenGL 3.1-3.3 and OpenGL ES 3 Standard)

Install WSL

Fire up a new terminal and hit the following demand to see if you have a valid WSL setup

wsl --install

Then we want to make sure we’re using WSL2, and also make sure that the WSL runtime is updated to the newest version

wsl --update
wsl --set-default-version 2

Now we’re ready to install the ubuntu distro. Make sure you have at least 5GB of space in your C:\ disk. You can move your WSL image to a new disk later on.

wsl --install -d Ubuntu

You will be prompted to enter a username and password for the default user.

After installing, runnning

wsl -l -v

should show the following

  NAME                   STATE           VERSION
* Ubuntu                 Stopped         2

Make sure your VERSION of Ubuntu Distro says 2. The star(*) symbol can be missing here. We just need to run the following command to make it the default distro.

wsl -s Ubuntu

Now you’ve done WSL installable, you can follow this step to move WSL to another drive if wanted.

Move WSL File System to another Drive
Language: [🇪🇸] Español - [🇺🇸] English If the C: drive is running out of space or do you like to have your files in a different disk than your Operating System, we can move our WSL2 file system to another drive in a few steps.
https://dev.to/equiman/move-wsl-file-system-to-another-drive-2a3d

Install Carla On Windows

This step assumes that you have installed appropriate graphics drivers for your GPU. Since it is easy to install GPU in a Windows setup, the steps will not be covered.

Then download Carla for Windows (you don’t need to build it yourself, as on the github page they usually ship with Windows Binaries)

⚠️
ROAR requires you to download CARLA 0.9.12, not 0.9.13.

Then fire up CarlaUE4.exe, it will automatically start listening to 0.0.0.0:2000, and 0.0.0.0:2001, I added the following param to boost performance since my GPU is a trash MX350

CarlaUE4.exe -quality-level=Low -fps=5 -windowed -ResX=300 -ResY=200
📢
Also make sure you allow Carla to access your local network in your firewall settings

Install ROS on WSL

Fire up WSL prompt, and follow the instructions to install WSL using Debian Packages

Installing ROS 2 via Debian Packages - ROS 2 Documentation: Foxy documentation
Installing University or Evaluation versions of RTI Connext DDS
https://docs.ros.org/en/foxy/Installation/Ubuntu-Install-Debians.html

Install Carla on WSL

The reason we want to install Carla is because we want the PythonAPI provided by the Carla installation, we don’t actually need to fire up Carla in our WSL environment.

Plus in the near future if Vulkan API is supported by Dozen in Mesa3D, we could potentially potentially fire up Carla in the WSL environment.

Quick start package installation
This guide shows how to download and install the packaged version of CARLA. The package includes the CARLA server and two options for the client library. There are additional assets that can be downloaded and imported into the package.
https://carla.readthedocs.io/en/0.9.12/start_quickstart/
📢
If you need to install older version of carla, execute sudo apt install carla-simulator=0.9.12 by specifying a version

Install ROS Bridge for CARLA

📢
You can optionally follow the guide to export the environment for CARLA_HOME and PYTHON_PATH if you want, but its not required since we will use a special script to fire up the bridge later.
Install ROS Bridge for ROS 2 - CARLA Simulator
This section is a guide on how to install the ROS bridge on Linux for use with ROS 2. You will find the prerequisites, installation steps, how to run a basic package to make sure everything is working well and commands to run tests. Important ROS is still experimental for Windows.
https://carla.readthedocs.io/projects/ros-bridge/en/latest/ros_installation_ros2/

Firing up ROS Bridge

First you need to open up Carla on your host OS.

Then add a new file under home(~) directory called launchROSBridge.sh

Here below should be the content you add to the shell file.

parent_host=`grep -oP '(?<=nameserver ).+' /etc/resolv.conf`
echo Exporting Environmental Variables
export CARLA_ROOT=/opt/carla-simulator
export PYTHONPATH=$PYTHONPATH:$CARLA_ROOT/PythonAPI/carla/dist/carla-0.9.12-py3.7-linux-x86_64.egg:$CARLA_ROOT/PythonAPI/carla
echo Launching...Parent Host is $parent_host

ros2 launch carla_ros_bridge carla_ros_bridge.launch.py host:=$parent_host timeout:=5000

In this script, parent_host variable is obtained by grepping contents of the /etc/resolv.conf file, and we can use this IP to connect to our host OS’s listening ports.

Sanity Check for OpenGL/Vulkan

To run OpenGL / Vulkan APPs in your WSL environment, you want to run the following scripts

export DISPLAY=:0 #this makes the graphics output go to our host os

And runing glxinfo -B should export something similar to the following

name of display: :0
display: :0  screen: 0
direct rendering: Yes
Extended renderer info (GLX_MESA_query_renderer):
    Vendor: Microsoft Corporation (0xffffffff)
    Device: D3D12 (NVIDIA GeForce MX350) (0xffffffff)
    Version: 22.0.2
    Accelerated: yes
    Video memory: 10086MB
    Unified memory: no
    Preferred profile: core (0x1)
    Max core profile version: 3.3
    Max compat profile version: 3.3
    Max GLES1 profile version: 1.1
    Max GLES[23] profile version: 3.1
OpenGL vendor string: Microsoft Corporation
OpenGL renderer string: D3D12 (NVIDIA GeForce MX350)
OpenGL core profile version string: 3.3 (Core Profile) Mesa 22.0.2 - kisak-mesa PPA
OpenGL core profile shading language version string: 3.30
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile

OpenGL version string: 3.3 (Compatibility Profile) Mesa 22.0.2 - kisak-mesa PPA
OpenGL shading language version string: 3.30
OpenGL context flags: (none)
OpenGL profile mask: compatibility profile

OpenGL ES profile version string: OpenGL ES 3.1 Mesa 22.0.2 - kisak-mesa PPA
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.10

If you have your physical graphics card name displaying here, you are in good shape. Also you want to update your driver by running the following

sudo add-apt-repository ppa:kisak/kisak-mesa
sudo apt-get update
kisak-mesa fresh : kisak
The goal of this PPA is to provide the latest point release of Mesa plus select non-invasive early backports. Deviations from upstream packages are listed on the package details page. --- Support status --- Bionic (18.04) - Supported (Please use the HWE X and kernel or equivalent) Focal (20.04) - Supported Impish (21.10) - Supported Note: Please report any issues to mesa.
https://launchpad.net/~kisak/+archive/ubuntu/kisak-mesa

To verify that WSLg is working, run the following

sudo apt install mesa-utils vulkan-tools
glxgears
vkcube

running glxgears and vkcube should each pop up a window in your host OS.

Further Readings

ML Acceleration using CUDA and DirectML on WSL2

GPU acceleration in WSL
This documentation covers setting up GPU accelerated machine learning (ML) training scenarios for the Windows Subsystem for Linux (WSL) and native Windows. This functionality supports both professional and beginner scenarios. Below you'll find pointers to step-by-step guides on how to get your system set up depending on your level of expertise in ML, your GPU vendor, and the software library that you intend to use.
https://docs.microsoft.com/en-us/windows/ai/directml/gpu-accelerated-training