How To Install ROS 2 in Ubuntu 22.04 On M1/M2 Mac

Congratulations on taking the first step into the fascinating realm of robotics!
In this tutorial, I will walk you through the in-depth steps to install Ubuntu 22.04 with ROS 2 on the Macbook M1/M2 chip using UTM Virtualization software.

ROS 2 is supported in MacOS using the Homebrew installation process. Although, in my 7 years as a Roboticist, I have hardly seen anyone using MacOS for Robotics. What I have seen is the use of Virtual Desktops and Dockers with Ubuntu extensively. One of the primary reasons for configuring your development environment in this manner is because robotics software works on ARM-based development kits like Nvidia’s Jetson AGX series. When utilizing Virtual Desktops on Mac M1, you effectively have the same development environment as your robot's runtime environment while keeping the M1 chip's inherent speed performance.

We now understand why and what we are doing. Let’s dive right in.

  1. The first step is more of a reminder. The Macbooks with M1/M2 Chip are ARM-based platforms compared to older MacBooks and Windows which are AMD/Intel x86-based platforms.
  2. Different virtual desktops are available like Parallels Desktop, Oracle’s Virtual Box, and UTM each having its pros and cons. I would recommend the Parallels Desktop paid version if you are a professional developer. For students or anyone looking for free software, I would recommend Virtual Box for Windows and UTM for Macbooks.
  3. In this article, we will use free UTM virtualization software. UTM is a full-featured system emulator and virtual machine host for iOS and macOS. It is based on QEMU. In short, it allows you to run Windows, Linux, and more on your Mac, iPhone, and iPad. UTM allows Ubuntu to run with OpenGL, Hardware acceleration providing a highly efficient with near-native speed performance.
  4. I am using Macbook Pro with M1 Chip with MacOS 13. You will need about 50GB of free space for your new operating system.
  5. Go to https://mac.getutm.app/ and click the Download button. A UTM.dmg file will start downloading. article image 1
  6. Go to https://cdimage.ubuntu.com/jammy/daily-live/current/ and Download 64-bit ARM (ARMv8/AArch64) desktop image. A ".iso" desktop image will start downloading. Make sure you use the ARM image for a Macbook with M1/M2 chip and not an AMD image. This step might take time depending on your internet speed. article image 2
  7. Double-click on the UTM.dmg file and drag the icon to the Applications folder. article image 3
  8. Search for UTM and Open the UTM app from Launchpad. article image 4
  9. Once the app is launched, click Create a new Virtual Machine. article image 5
  10. Use the Virtualize option to simulate the bare metal performance. article image 6
  11. Select Linux and on the next screen, you will see Boot options. Click Browse and select the .iso image for Ubuntu 22.04 that you download in the above step. article image 7
    article image 8
  12. Keep pressing the Continue button with default settings till you reach the summary screen.
  13. Tap on the big Play button to boot Ubuntu 22.04 article image 9
  14. Select Try to Install Ubuntu. This takes a minute and you will see a blank screen. article image 11
  15. Once you see the Ubuntu 22.04 screen, type in ubuntu to login. article image 12
  16. The system will boot with the live image. On the right side, you will see Install Ubuntu. Continue with the standard installation process. article image 13
  17. Select Install Third Party Software in the following screen. article image 14
  18. Select Erase Ubuntu and reinstall or if you are doing it for the first time, the third option Erase disk and install Ubuntu will be your first one. Choose that. article image 15
  19. Save the username and password. You will need this later for login. article image 16
  20. Continue with installation. This will take time. article image 17
  21. Once Installation is done, You will see an option to restart the screen. Close the dialog box and Power Off the system. article image 18
    article image 19
  22. Now using the virtual machine Navigation bar, choose Drive Image Options and eject the image file. article image 20
    article image 21
  23. You can restart the VM now by tapping on the Play Button. Now you will wait for 10 seconds to see the login screen of Ubuntu. article image 22
  24. Congratulations! You have Ubuntu 22.04 running on your Macbook. You can log in now using your password.

How To Install ROS 2 in Ubuntu 22.04 On M1 Mac

Now that we have Ubuntu 22.04 running on our Macbook with M1/M2 Chip, we can install the ROS 2 HUMBLE version by following the steps for Linux Distribution.

  1. Open Terminal. article image 23
  2. To be able to copy and paste between your Ubuntu and Mac, run the following command in your terminal $ sudo apt install spice-webdavd spice-vdagent
  3. Set locale
    Make sure you have a locale that supports UTF-8.
    $ locale # check for UTF-8 skip this step if locale is utf-8 already $ sudo apt update && sudo apt install locales $ sudo locale-gen en_US en_US.UTF-8 $ sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 $ export LANG=en_US.UTF-8 $ locale # verify settings
  4. Setup resources $ sudo apt install software-properties-common $ sudo add-apt-repository universe
  5. ROS 2 GPG key with apt. $ sudo apt update && sudo apt install curl -y $ sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
  6. Add the repository to your sources list. $ 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
  7. Update and Upgrade your packages $ sudo apt update $ sudo apt upgrade
  8. Install Desktop Full $ sudo apt install ros-humble-desktop-full
  9. Setup environment: $ source /opt/ros/humble/setup.bash
  10. Add this to your bashrc script so that you don’t have to re-run the command every time you open a new terminal. $ echo “source /opt/ros/humble/setup.bash” >> ~/.bashrc
  11. To check the installation: $ ros2 run demo_nodes_cpp talker
  12. Another important thing to learn is how to build new ros packages:

  13. Go to https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Colcon-Tutorial.html.
    We need to install the `colcon` build tool. $ sudo apt install python3-colcon-common-extensions
  14. Welcome to creating your first ros workspace. $ mkdir -p ~/ros2_ws/src $ cd ~/ros2_ws
  15. Since we are doing a clone, we need git also, So install git using $ sudo apt install git
  16. Run all the below commands from the ros2_ws folder. $ git clone https://github.com/ros2/examples src/examples -b humble
  17. After cloning, you should have the entire code inside your src folder for examples in ros2.
  18. Now we will build examples of the ros package. In the root of the workspace, run $ colcon build
  19. You should have build, install, and log folders.
  20. You need to source before being able to launch the node. So run $ . install/setup.bash
  21. Try a demo
    With the environment sourced, we can run executables built by colcon. Let’s run a subscriber node from the examples: $ ros2 run examples_rclcpp_minimal_subscriber subscriber_member_function
    In another terminal, let’s run a publisher node (don’t forget to source the setup script): $ ros2 run examples_rclcpp_minimal_publisher publisher_member_function
    You should see messages from the publisher and subscriber with numbers incrementing.
    article image 24
    Voila!! You have ROS 2 Humble Running on Ubuntu 22.04 inside a Virtual Machine on Macbook.

If you liked the article, please buy me a ☕️ coffee

References: