Ubuntu 20.04 Install (Preferred)
...
Alternatively, you can also set up ROS2 using a virtualized system on your Windows, Linux, or MacOS machine. This is easier and less risky than setting up dual boot, but slower than a dual boot, but much faster than a VM.
...
- Install WSL2 on your computer. This is Windows Subsystem for Linux, which allows developers to run a Linux environment without a separate virtual machine or dual booting. It's used by Docker to run its backend. If you're on Windows 10 version 2004 and higher (Build 19041 and higher), visit this page; all you have to do is run one command in Windows Powershell! If you're on an older build, see these manual steps.
- Install Docker Desktop via the instructions.
- Install VcXsrv, an application for the X Window System Display server. The X Window System Display server used by various operating systems for running their graphical applications. We'll VcXserv on Windows, and then we'll send all the graphical application traffic from our Docker container to our X server running on Windows, allowing us to run our graphical applications straight out of the box.
- We'll need to change the display number on VcXsrv to 0 so Docker knows where to find our server. Navigate to `C:\Program Files\VcXsrv`, or wherever you installed VcXsrv, and launch
xlaunch.exe
. In the display settings window, navigate to the bottom and change Display Number from -1 to 0, and hit next until you can closexlaunch
. - Make sure Docker Desktop is running before we continue. It usually runs in the background, so check your hidden icons (the up arrow in the bottom right of the Windows taskbar) to make sure you see the Docker icon in there.
- From here, we're going to migrate to VSCode so you can develop properly. If you haven't already, install VSCode, then install the Dev Containers Extension.
- Create a folder on your computer for your UW Robotics team development–doesn't matter where it is, as long as you can access it quickly.
- Open up that folder in VSCode. Download and add our "Dockerfile.dev" file to that folder.
- Now, use Ctrl+Shift+P to open the Command Palette. In the Command Palette, search for and run "Dev Containers: Open Folder in Container".
- VSCode will then ask you to open a folder–open the folder you created earlier for your UW Robotics team development. Then, it will ask you which Dockerfile you'd like to use for the container–choose the Dockerfile.dev file you downloaded before, which is in that folder.
- It will then create a "devcontainer.json" for you inside a ".devcontainer" folder, as well as build and open the container. The devcontainer.json file tells Docker how to build and run your container.
- We need to add a specific line to your newly created
devcontainer.json
. On the next available line, add the following data. Make sure to add any commas required (before or after the data) so the JSON is valid."containerEnv": { "DISPLAY": "host.docker.internal:0.0" } - You'll need to save the file and restart your container for the change to your devcontainer.json. You can close the container by going in the bottom left in VSCode to the green icons/text, and clicking Close Remote Session. Then, do the same, but Rebuild Container.
- You are officially done! Let's go ahead and recap the instructions anytime you want to start coding in your container:
- Open Docker Desktop.
- Open VcXsrv (you may want to add the C:\Program Files\VcXsrv\xlaunch.exe file as a shortcut to your Start Menu or Desktop).
- Once you've opened VcXsrv's Xlasunch, change the display number to 0, then hit next until you see Finish, and click that.
- Open VSCode.
- Use Ctrl+Shift+P to open the VSCode Command Palette.
- In the Command Palette, search for and run "Dev Containers: Open Folder in Container".
- When prompted, open your UW Robotics team development folder.
- When prompted, use Dockerfile.dev as your development container. This container contains ROS2 Galactic running on Ubuntu 20.04!
- Done! When you want to run commands in your container, use Ctrl+` to open a terminal to your container. Anything you do inside the container will be reflected in your folder on your local machine.
Alternative Docker commands (kept for historical reasons).
- After step 5. Fantastic, you've installed everything you need to run Docker graphically! Next up, open Windows Powershell (as an administrator should help). Next, we're going to pull down and show you how to use a pre-built Ubuntu 20.04 image with ROS2 Foxy installed, so you can get going with the rest of the software training.
- Run
docker pull osrf/ros:foxy-desktop
in Powershell. This will download the image, straight from the Open Source Robotics Foundation (the people behind ROS). If you need ROS2 Galactic for post-training dev work, useosrf/ros-galactic-desktop
. - Next, we'll go ahead and run this image. Before we do this, we'll need to pass a lot of flags to the
docker run
command, so I'll explain them here:-
the -v flag shares a folder from your host machine (your Windows computer) to the Docker container. In this example, we're mappingv C:\Users\path\to\folder:/code/dev_ws
C:\Users\path\to\folder
to/code/dev_ws
in the container. Any changes made in the Docker container will be reflected on your folder on Windows, and any changes made to your folder on Windows will be reflected in the Docker container. This is incredibly useful, because now we can code on Windows but run and test on the container in Linux.-it
runs the container with an interactive shell. If you didn't pass-it
, the container would run in the background and you would have no way of interacting with it (unless you useddocker exec
later).-e
sets an environment variable inside the Docker container. We'll be using this to tell the Ubuntu container where to send the graphical traffic via theDISPLAY
environment variable.--name
sets a name for the Docker container for use later. We'll call our Docker containeruwrt_dev_container
.
- Now, the actual
docker run
command you'll be using; please change the first path of the-v
flag to an absolute path of wherever you plan on storing your ROS2 workspace. In this example, we're sharing our entireC:\Users\Owner\Documents\Code
folder to the container:-
docker run --name uwrt_dev_container -e DISPLAY=host.docker.internal:0.0 -it -v C:\Users\Owner\Documents\code:/code/dev_ws osrf/ros:foxy-desktop
-
- You should now be inside the Docker container! Feel free to look around by running any typical Linux commands. ROS2 Foxy (or Galactic, whichever you chose) is pre-installed in this container.
- Run
- Useful Docker commands from here out:
- Stopping the container: `docker stop container-name`
- Restarting the container after stopping:
docker start container-name
- Pausing the container:
docker pause container-name
- Unpausing the container: `docker pause container--name`
- Opening a new shell for the container: `docker exec -it <mycontainer> bash`
...