ROS 2 Environment Setup
Initialize and configure the ROS 2 development environment for both local and remote development workflows.
When to Use This Skill
- •Initialize workspace after cloning
- •Set up local development environment
- •Configure remote development with devcontainer
- •Initialize Python virtual environment
- •Update environment variables for build system
- •Configure IDE for ROS 2 development
- •Reset or troubleshoot environment issues
- •Switch between development and production Python environments
Prerequisites
- •ROS 2 Jazzy installed at
/opt/ros/jazzy/ - •Workspace cloned to your workspace root
- •Bash shell available (sourcing scripts)
- •For local development: system dependencies installed (CMake, compilers, etc.)
- •For remote development: Docker and VS Code Remote Extensions
- •Python 3.8+ available
Environment Architecture
ROS 2 Environment Hierarchy:
├── Base ROS 2 (/opt/ros/jazzy/setup.bash)
│ ├── ROS_DISTRO=jazzy
│ ├── ROS_PYTHON_VERSION=3
│ └── Core ROS 2 packages
├── Workspace Overlay (install/local_setup.bash)
│ ├── Workspace packages
│ └── Built packages
└── Python venv (.venv/)
└── Development dependencies
Step-by-Step Workflows
Workflow 1: Initialize Local Development Environment
Set up a fresh workspace for local development.
- •
Clone or navigate to workspace:
bashcd <workspace_root>
- •
Source the ROS environment setup script:
bashsource scripts/setup.bash
This script:
- •Sources
/opt/ros/jazzy/setup.bash(base ROS) - •Sources
install/local_setup.bashif available (workspace overlay) - •Activates production Python environment
- •Sets ROS_DISTRO, ROS_PACKAGE_PATH, and other ROS variables
- •Sources
- •
Verify ROS environment is configured:
bashecho $ROS_DISTRO # Should print: jazzy echo $ROS_PACKAGE_PATH # Should show workspace packages which ros2 # Should find ROS 2 executable
- •
Environment is ready for development
Workflow 2: Set Up Python Virtual Environment for Development
Create isolated Python environment for development tools.
- •
Create virtual environment (one-time setup):
bashpython3 -m venv .venv
- •
Activate the virtual environment:
bashsource .venv/bin/activate
Your shell prompt changes to show
(.venv)prefix - •
Install development dependencies:
bashpython3 -m pip install --upgrade pip python3 -m pip install -r requirements.txt --use-pep517
Installs:
- •Testing frameworks (pytest, coverage)
- •Code quality tools (ruff, mypy, type checkers)
- •Documentation tools (sphinx, autodoc)
- •Jupyter notebooks for prototyping
- •
Verify installation:
bashpython3 -m pytest --version python3 -c "import ruff; print('ruff ready')" - •
Exit venv when done:
bashdeactivate
Note: Virtual environment is for development only; ROS 2 runtime uses system Python
Workflow 3: Update Python Virtual Environment
Refresh venv dependencies after requirements.txt changes.
- •
Activate the virtual environment:
bashsource .venv/bin/activate
- •
Use convenience flag with setup script:
bashdeactivate # Exit any active venv source scripts/setup.bash --update-venv
This automatically:
- •Re-creates/updates the venv
- •Installs latest dependencies from requirements.txt
- •Leaves venv activated for immediate use
- •
Verify updates completed:
bashpip list | grep pytest
- •
Deactivate when done:
bashdeactivate
Workflow 4: Configure Remote Development with Devcontainer
Set up VS Code devcontainer for remote/GitHub Codespaces development.
- •
Open workspace in VS Code:
bashcode <workspace_root>
- •
Install "Dev Containers" extension (if not already installed)
- •
Open Command Palette:
Cmd+Shift+P(Mac) orCtrl+Shift+P(Linux/Windows) - •
Search for: "Dev Containers: Reopen in Container"
- •
VS Code rebuilds the devcontainer:
- •Base image:
ghcr.io/dr-qp/jazzy-ros-desktop:edge - •Build time: 5-15 minutes (first time only)
- •Subsequent opens: < 10 seconds
- •Base image:
- •
Devcontainer automatically:
- •Sets up ROS 2 Jazzy environment
- •Creates Python venv
- •Installs all dependencies via rosdep
- •Configures development tools (clangd, debuggers, test viewers)
- •
Verify devcontainer environment:
bashecho $ROS_DISTRO python3 -m pytest --version
Devcontainer Features:
- •Persistent volumes: Build artifacts, install, logs, caches persist across sessions
- •Network: Proper ROS communication (DDS, IPC)
- •Development tools: Clangd, debuggers, test viewers pre-configured
- •Consistent environment: Identical across all developer machines and CI
Workflow 5: Configure Build System Environment Variables
Set compiler and build system options for optimal performance.
- •
Source the base ROS environment:
bashsource scripts/setup.bash
- •
Configure for development builds with clang:
bashexport CMAKE_EXPORT_COMPILE_COMMANDS=1 export CC=clang export CXX=clang++
What these do:
- •
CMAKE_EXPORT_COMPILE_COMMANDS=1: Generates compile_commands.json for IDE - •
CC=clang: Use Clang C compiler (faster, better error messages) - •
CXX=clang++: Use Clang C++ compiler
- •
- •
Verify environment:
bashecho $CC # Should print: clang echo $CXX # Should print: clang++ ls -la build/compile_commands.json # IDE integration
- •
Build with optimized environment:
bashpython3 -m colcon build --packages-up-to <package_name>
Workflow 6: Verify Complete Environment Setup
Check that all components are properly configured.
- •
Verify ROS 2 core:
bashsource scripts/setup.bash ros2 topic list # Should work (may be empty if no nodes running) ros2 service list # Should work ros2 node list # Should work
- •
Verify workspace packages:
bashros2 pkg list | grep drqp # Should show workspace packages
- •
Verify Python setup:
bashpython3 -c "import rclpy; print('ROS 2 Python client ready')" - •
Verify development tools:
bashsource .venv/bin/activate python3 -m pytest --version python3 -m ruff --version deactivate
- •
If all commands succeed, environment is fully configured
Workflow 7: Reset Environment (Troubleshooting)
Clean up environment and start fresh.
- •
Deactivate any active Python venv:
bashdeactivate
- •
Clear environment variables:
bashunset ROS_DISTRO ROS_PACKAGE_PATH CC CXX CMAKE_EXPORT_COMPILE_COMMANDS
- •
Open a new terminal or shell
- •
Re-run setup:
bashsource scripts/setup.bash
- •
For clean venv, remove and recreate:
bashrm -rf .venv python3 -m venv .venv source .venv/bin/activate python3 -m pip install -r requirements.txt --use-pep517
Environment Variables Reference
| Variable | Purpose | Set By | Value |
|---|---|---|---|
ROS_DISTRO | ROS 2 distribution | setup.bash | jazzy |
ROS_PACKAGE_PATH | Package search paths | setup.bash | $(pwd)/install/ |
AMENT_PREFIX_PATH | Installed package prefix | setup.bash | $(pwd)/install/ |
CMAKE_EXPORT_COMPILE_COMMANDS | IDE integration | Manual | 1 |
CC | C compiler | Manual | clang |
CXX | C++ compiler | Manual | clang++ |
PYTHONPATH | Python module search path | setup.bash | Includes workspace packages |
VIRTUAL_ENV | Active venv path | venv activation | $(pwd)/.venv |
Directory Structure
<workspace_root>/
├── scripts/
│ └── setup.bash # Main environment setup script
├── install/ # Built/installed packages
│ ├── local_setup.bash # Workspace overlay setup
│ └── <packages>/
├── build/ # Build artifacts
│ └── compile_commands.json # For IDE integration
├── .venv/ # Python virtual environment
│ ├── bin/activate # Activation script
│ └── lib/python3.x/
└── .devcontainer/
└── devcontainer.json # Remote development config
Troubleshooting
| Issue | Cause | Solution |
|---|---|---|
| "Command 'ros2' not found" | ROS 2 not sourced | Run source scripts/setup.bash |
| "Package not found" error | Workspace overlay not loaded | Ensure install/local_setup.bash exists, rebuild if needed |
| Python import "rclpy" fails | ROS 2 Python client not available | Install: rosdep install --from-paths packages --ignore-src -y |
| Venv not activating | Venv not created or corrupted | Delete .venv and recreate: python3 -m venv .venv |
| Devcontainer won't start | Docker not running or insufficient space | Start Docker daemon and check disk space |
| Conflicting Python versions | Multiple Python environments active | Deactivate venv: deactivate, then verify python3 --version |
| Build uses wrong compiler | Environment variables not set | Export: export CC=clang && export CXX=clang++ |
| IDE can't find includes | Compile commands missing | Build with CMAKE_EXPORT_COMPILE_COMMANDS=1 enabled |
References
- •Devcontainer configuration:
.devcontainer/devcontainer.json