How to install ROS-Noetic on WSL2
Recently, after a lot of struggles, I ran my first launch file in Gazebo with ROS Noetic. It's been a long time that I had been wanting to do this. But, because, ROS isn't supported for Windows (stably) I wasn't able to work with it. Solutions available on YouTube and community forums aren't that beginner-friendly. I found it very difficult to Dual-boot the system or use a VM, as I was afraid of breaking anything (on the software part).
I knew if I have to stay on a pure Windows OS and still be able to install Linux-based software, then I need a Linux system for Windows. And there comes WSL (2) in the picture.
WSL or Windows Subsystem for Linux is a compatibility layer for running Linux-binary executables natively on Windows 10, Windows 11, and Windows Server 2019. WSL 2 was announced, introducing important changes such as a real Linux kernel, through a subset of Hyper-V features.
I have seen a lot of people, including me, unable to use Linux-based software on Windows OS. Folks then rely on Dual-booting or installing a Virtual Machine to run Linux on it.
If we consider Dual Booting as an option, then it can be overwhelming for some folks who have just started with technology. The lack of knowledge about Operating Systems is the reason people do not prefer dual booting. And in case of any failure, they won't be able to fix the issue or understand them without external help!
On the other hand, Virtual Machine or VM is the virtualization/emulation of a computer system. Virtual machines are based on computer architectures and provide thefunctionality of a physical computer. Their implementations may involve specialized hardware, software, or a combination. VM(s) require hardware allocation and take up the system's allotted hardware capabilities entirely. Configuring a VM can also be hectic sometimes (as per my experience).
So, the solution is WSL2.
Now, it is very crucial to have your system updated before installing ROS or WSL.
Step 1: Update Windows 10
A recent version of Windows 10 needs to be installed for ROS to work. To check which version is installed go to Settings -> System -> About
. Check the version and Update Windows 10 (Version 20H2 or later is recommended for optimal performance).
Install WSL2
Install WSL on your system via MicrosoftStore or via this (follow Step 4 — Download the Linux kernel update package).
Now, open Turn Windows features on or off
Make sure that Windows Subsystem for Linux is checked. If not, then do it manually.
Set WSL 2 as your default version
This is important for you to install ROS via WSL. You need an updated WSL.
Open PowerShell and paste the following command:
wsl --set-default-version 2
Now, paste the following command:
wsl -l -v
There are two possibilities of the outcome. If the output after
wsl -l -v
is:
NAME STATE VERSION
* Ubuntu-20.04 Stopped 2
Then, congratulations. It was a successful step!
But if the output is something like:
NAME STATE VERSION
* Ubuntu-20.04 Stopped 1
Then you have not updated accurately as the version is still 1. You should then download WSL via this link.
Installing ROS on WSL2
Have you tried installing ROS2 on WSL 1? If yes, then, you might have to purge those files and reinstall ROS on WSL 2. But if you did not, then, let’s continue ahead.
For Ubuntu 20.04, you might have to install ROS Noetic. I simply followed the guide here by Jan Bernlöhr.
Let us follow few steps:
Paste the following commands in WSL 2:
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
curl -sSL 'http://keyserver.ubuntu.com/pks/lookup?op=get&search=0xC1CF6E31E6BADE8868B172B4F42ED6FBAB17C654' | sudo apt-key add -
sudo apt update
sudo apt install -y ros-noetic-desktop python3-rosdep
sudo rosdep init
rosdep update
Warning: If you encountered any error, just simply close the current WSL and open a new one. Then paste these commands one by one at a time.
To source ROS Noetic automatically forever bash session, paste the following commands in WSL.
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
To check if the above step was successful, open .bashrc file with nano
command. Paste this command nano ~/.bashrc
. This command will open the file inside WSL. Go at the bottom. If you find source /opt/ros/noetic/setup.bash
there.
Congratulations! These steps were successful.
If not, then, type it manually there and save it ctrl+x -> y -> Enter
.
Testing Installation of ROS
Open three new WSL bash prompts.
- In the first WSL bash, run the following commands:
cd
roscore
- In the second WSL bash, here, we will create a new python file with the name
publish.py
with "touch" and edit it using thenano
command. Paste the following commands:
touch publish.py
nano publish.py
When the file opens in editor mode, paste the following Python Code:
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
pub = rospy.Publisher('chatter', String, queue_size=10)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
hello_str = "hello world %s" % rospy.get_time()
rospy.loginfo(hello_str)
pub.publish(hello_str)
rate.sleep()
Save the file ctrl+x -> y -> Enter
. Now, run the file using python3 publish.py
. If it ran successfully, then congratulations!
- In the third WSL bash, we will create another python file with the name
subscribe.py
withtouch
and edit it using thenano
command. Paste the following commands:
touch subscribe.py
nano subscribe.py
- When the file opens in editor mode, paste the following Python Code:
#!/usr/bin/env python
import rospy
from std_msgs.msg import Stringdef callback(data):
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)def listener():
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("chatter", String, callback)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()if __name__ == '__main__':
listener()
Save the file ctrl+x -> y -> Enter
. Now, run the file using python3 subscribe.py
. If it ran successfully, then congratulations!
This step ensures that we have successfully installed ROS using WSL.
Setting up ROS for Graphical Output (GUI)
To run applications with graphical output, you need to install an X Server on Windows. To me, VcXsrv (recommended) works best.
After you have installed VcXsrv, you also need to configure WSL to use it. To do so modify you .bashrc as follows:
export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
source ~/.bashrc
OR
You can simply add DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
inside .bashrc file (as mentioned in step-4).
Finally, launch VcXsrv from the start menu. You need to change the following two settings:
- Native OpenGL needs to be unchecked. Otherwise, applications such as Rviz do not run as expected.
- Disable access control needs to be checked. Otherwise, applications within WSL cannot access the x server.
- When prompted by the Windows Firewall make sure that you enable access for both private and public networks — unless you are 100% sure that you only want to use the x server when connected to private networks.
Note: These settings might look different depending on when in the future you have downloaded VcXsrv. Warning: Every time you restart your PC, you need to check and un-check the options again (mentioned above). The solution for this is can be to save a config file in VcXsrv.
Running Applications with Graphical Output (GUI)
The popular turtle_sim tutorial works fine WSL as well.
Make sure you have an X Server (eg: VcXsrv) installed, configured (checked and unchecked), and running as described above👆.
- Start a new bash prompt and run the following command:
cd
roslaunch turtle_tf turtle_tf_demo.launch
You can control the turtle by using the arrow keys by going back to the second prompt.
- RVIZ: Start a new WSL bash and run the following command:
cd
rosrun rviz rviz -d `rospack find turtle_tf`/rviz/turtle_rviz.rviz
If everything ran successfully, then BOOM. You’ve finally done it!
Creating Workspace
Note: For Ros Noetic and above, you will have to build the Turtlebot package from the source.
Try the following command to see, if the workspace is available or not:
cd ~/catkin_ws/src
If you have not yet set up your catkin workspace, you can do that by:
cd
mkdir -p catkin_ws/src
Here clone the following repositories using ‘Git’.
git clone https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git
git clone https://github.com/ROBOTIS-GIT/turtlebot3.git
git clone https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git
Building Workspace
Paste the following command:
cd ..
catkin_make
Now, Add the following commands in .bashrc manually or using echo
command (as mentioned in step-4):
export TURTLEBOT3_MODEL=burger
source ~/catkin_ws/devel/setup.bash
export SVGA_VGPU10=0
Test your Turtlebot and Gazebo setup by launching a sample launch file using the following command:
roslaunch turtlebot3_gazebo turtlebot3_empty_world.launch