Skip to content

从源码安装ROS 2 | Building ROS 2 from source

Homalozoa X edited this page Oct 3, 2021 · 4 revisions

中文 | English

中文

本文旨在介绍源码编译ROS 2及相关依赖,平台为Debian/Ubuntu系发行版系统;目前的版本是Galactic Geochelone。

若要直接安装二进制包,请参考 可直接参考Installing ROS2. 其他系统请酌情按照自己的环境修改。如果是Arch Linux,可直接参考ROS - Arch Linux Wiki.

1.修改字符格式(一般情况不用执行)

locale  # check for UTF-8
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

2.更新Ubuntu源

注意,这里需要分辨自己的版本。

Ubuntu 18.04

sudo apt install curl gnupg2 lsb-release 
sudo vim /etc/apt/sources.list

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu/ $(lsb_release -cs) main" > /etc/apt/sources.list.d/ros2-latest.list'
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
sudo apt update

Ubuntu 20.04:

sudo apt install curl gnupg2 lsb-release 
sudo vim /etc/apt/sources.list

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu/ $(lsb_release -cs) main" > /etc/apt/sources.list.d/ros2-latest.list'
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
sudo apt update

3.安装编译所需依赖

最小项
  • 安装ROS 2的依赖(如遇报错,跳过该库,手动安装即可)
sudo apt update && sudo apt install -y \
  build-essential \
  git \
  wget \
  python3-colcon-common-extensions \
  python3-colcon-mixin \
  python3-rosdep \
  python3-vcstool \
  libopencv-dev \
  libzmq3-dev \
  libgtest-dev \
  cmake \
  libacl1-dev \
  libsqlite3-dev \
  libtinyxml2-dev \
  libbullet-dev \
  libncurses-dev \
  libbison-dev \
  libcurl4-openssl-dev \
  bison \
  liblog4cxx-dev \
  libeigen3-dev \
  libasio-dev \
  python3-dev \
  libboost-all-dev \
  libglib2.0-dev \
  libprotobuf-dev \
  libprotoc-dev \
  protobuf-compiler \
  protobuf-compiler-grpc \
  libasound2-dev \
  libconsole-bridge-dev \
  libmpg123-dev \
  libv4l-dev \
  libssl-dev

特别地,ROS2依赖gtest静态链接文件,如果本地没有通过源码编译并安装过gtest,需要手动安装,参考方法:

sudo apt install libgtest-dev
cd /usr/src/gtest
sudo mkdir build
cd build
sudo cmake ..
sudo make
sudo *.a /usr/lib
  • 安装python依赖
  python3 -m pip install -U \
  argcomplete \
  flake8 \
  flake8-blind-except \
  flake8-builtins \
  flake8-class-newline \
  flake8-comprehensions \
  flake8-deprecated \
  flake8-docstrings \
  flake8-import-order \
  flake8-quotes \
  pytest-repeat \
  pytest-rerunfailures \
  pytest \
  pytest-cov \
  pytest-runner \
  numpy \
  lark \
  ifcfg \
  netifaces \
  boost

特别的,如果使用Ubuntu18.04,还需要

python3 -m pip install -U importlib-metadata importlib-resources
可选项
  • 安装SIP和PyQT5相关的包
python3 -m pip install sip
python3 -m pip install PyQt5==5.14.0   (必须5.14版本)
python3 -m pip install PyQt5-sip

# For Ubuntu 18.04
sudo mkdir /usr/share/sip
sudo ln -s ~/.local/lib/python3.6/site-packages/PyQt5 /usr/share/sip/. 
sudo ln -s ~/.local/lib/python3.6/site-packages/PyQt5/bindings/* /usr/share/sip/PyQt5/.

# For Ubuntu 20.04
sudo mkdir /usr/share/sip
sudo ln -s ~/.local/lib/python3.8/site-packages/PyQt5 /usr/share/sip/. 
sudo ln -s ~/.local/lib/python3.8/site-packages/PyQt5/bindings/* /usr/share/sip/PyQt5/.
  • 安装Point Cloud Library(PCL)
sudo apt install libx11-dev libxext-dev libxtst-dev libxrender-dev libxmu-dev libxmuu-dev
sudo apt install libgl1-mesa-dev libglu1-mesa-dev
sudo apt install freeglut3-dev libboost-all-dev libeigen3-dev libflann-dev libglew-dev
sudo apt install libpcap-dev libusb-1.0-0-dev libopenni-dev libopenni2-dev clang-format libqhull-dev

git clone git@github.com:Kitware/VTK.git
cd VTK
git checkout v8.2.0
mkdir build
cd build
cmake -DVTK_USE_SYSTEM_PNG=ON ..
make -j4
sudo make install
git clone git@github.com:PointCloudLibrary/pcl.git
cd pcl
git checkout 1.11.1
mkdir build
cd build
cmake ..
make -j4
sudo make install

4. 下载ROS 2源码

git clone https://github.com/MiRoboticsLab/cyberdog_ros2.git
mkdir -p ros_src/src
cp cyberdog_ros2/tools/ros2_fork/* ros_src
cd ros_src
vcs import src < mini.repos
# 如需要extend.repos的内容,还需执行
vcs import src < extend.repos

目前我们提供的是根据ROS社区的功能包整合的两个repos文件,分别是:

  • mini.repos : 必须项。机载最小包,包含基本的通讯和接口定义,以及关键依赖。
  • extend.repos : 可选项。适用于与mini.repos结合,在PC上使用,附带可视化和点云处理等组件

如果在大陆地区没有良好的下载速度,可以在import之前之后,输入两次下述指令。分别修改repos文件和项目内的链接。

sed -i "s/github.com/hub.fastgit.org/g" `grep github.com -rl * .*`
sed -i "s/raw.githubusercontent.com/raw.fastgit.org/g" `grep raw.githubusercontent.com -rl * .*`

5.编译ROS 2

ROS2环境预处理
export ROS_VERSION=2
编译
sudo mkdir -p /opt/ros2/galactic
sudo chown $USERNAME /opt/ros2/galactic
colcon build --merge-install --install-base /opt/ros2/galactic --parallel-workers 20

期间若发生warning,忽略即可。

若报错,可以首先尝试到社区寻找答案:

比较常见的错误,形如:

Starting >>> shape_msgs
--- stderr: libstatistics_collector
CMake Error at CMakeLists.txt:100 (rosidl_get_typesupport_target):
  Unknown CMake command "rosidl_get_typesupport_target".

通常是ros模块master分支在rolling迭代过程中导致的版本支持问题,如上述,解决方案为

cd src/ros-tooling/libstatistics_collector
git checkout galactic

6. 添加脚本

如果你使用的是zsh,则添加如下脚本到~/.zshrc,如果你使用的是bash,则添加如下脚本到~/.bashrc,如果是其他,酌情处理。

ros2_on(){
      export ROS_DOMAIN_ID=42
      export ROS_VERSION=2
      export ROS_PYTHON_VERSION=3
      export ROS_DISTRO=galactic
      source /opt/ros2/galactic/setup.zsh
 }
 # 如果你使用bash,需要替换zsh为bash

7. 使用方法

更新环境变量后,运行ros2_on即可部署ROS 2环境

English

This document introduces how to compile the source code of Galactic Geochelone and related dependencies. The platform is Debian/Ubuntu system.

If you want to install the binary package directly, please refer to Installing ROS2. Please modify the commands according to your own environment. If you use Arch Linux, please refer to ROS - Arch Linux Wiki.

1. Modify the character format (Generally you do not need to modify it.)

locale  # check for UTF-8
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

2. Update Ubuntu source

Note: Please choose your version of ubuntu.

For Ubuntu 18.04:

sudo apt install curl gnupg2 lsb-release 
sudo vim /etc/apt/sources.list

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu/ $(lsb_release -cs) main" > /etc/apt/sources.list.d/ros2-latest.list'
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
sudo apt update

For Ubuntu 20.04:

sudo apt install curl gnupg2 lsb-release 
sudo vim /etc/apt/sources.list

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu/ $(lsb_release -cs) main" > /etc/apt/sources.list.d/ros2-latest.list'
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
sudo apt update

3. Install and compile required dependencies

Required
  • Install the dependencies of ROS 2. (If you get an error, skip the library and install it manually.)
sudo apt update && sudo apt install -y \
  build-essential \
  git \
  wget \
  python3-colcon-common-extensions \
  python3-colcon-mixin \
  python3-rosdep \
  python3-vcstool \
  libopencv-dev \
  libzmq3-dev \
  libgtest-dev \
  cmake \
  libacl1-dev \
  libsqlite3-dev \
  libtinyxml2-dev \
  libbullet-dev \
  libncurses-dev \
  libbison-dev \
  libcurl4-openssl-dev \
  bison \
  liblog4cxx-dev \
  libeigen3-dev \
  libasio-dev \
  python3-dev \
  libboost-all-dev \
  libglib2.0-dev \
  libprotobuf-dev \
  libprotoc-dev \
  protobuf-compiler \
  protobuf-compiler-grpc \
  libasound2-dev \
  libconsole-bridge-dev \
  libmpg123-dev \
  libv4l-dev \
  libssl-dev

Especially, ROS2 relies on gtest static library. If you didn't compile the gtest through source code and want to install it manually, you could refer to:

sudo apt install libgtest-dev
cd /usr/src/gtest
sudo mkdir build
cd build
sudo cmake ..
sudo make
sudo *.a /usr/lib
  • Install python dependencies
python3 -m pip install -U \
  argcomplete \
  flake8 \
  flake8-blind-except \
  flake8-builtins \
  flake8-class-newline \
  flake8-comprehensions \
  flake8-deprecated \
  flake8-docstrings \
  flake8-import-order \
  flake8-quotes \
  pytest-repeat \
  pytest-rerunfailures \
  pytest \
  pytest-cov \
  pytest-runner \
  numpy \
  lark \
  ifcfg \
  netifaces \
  boost
Optional
  • Install the related packages of SIP and PyQT5
python3 -m pip install sip
python3 -m pip install PyQt5==5.14.0   (5.14 version is required)
python3 -m pip install PyQt5-sip

# For Ubuntu 18.04
sudo mkdir /usr/share/sip
sudo ln -s ~/.local/lib/python3.6/site-packages/PyQt5 /usr/share/sip/. 
sudo ln -s ~/.local/lib/python3.6/site-packages/PyQt5/bindings/* /usr/share/sip/PyQt5/.

# For Ubuntu 20.04
sudo mkdir /usr/share/sip
sudo ln -s ~/.local/lib/python3.8/site-packages/PyQt5 /usr/share/sip/. 
sudo ln -s ~/.local/lib/python3.8/site-packages/PyQt5/bindings/* /usr/share/sip/PyQt5/.
  • Install Point Cloud Library(PCL)
sudo apt install libx11-dev libxext-dev libxtst-dev libxrender-dev libxmu-dev libxmuu-dev
sudo apt install libgl1-mesa-dev libglu1-mesa-dev
sudo apt install freeglut3-dev libboost-all-dev libeigen3-dev libflann-dev libglew-dev
sudo apt install libpcap-dev libusb-1.0-0-dev libopenni-dev libopenni2-dev clang-format libqhull-dev

git clone git@github.com:Kitware/VTK.git
cd VTK
git checkout v8.2.0
mkdir build
cd build
cmake -DVTK_USE_SYSTEM_PNG=ON ..
make -j4
sudo make install
git clone git@github.com:PointCloudLibrary/pcl.git
cd pcl
git checkout 1.11.1
mkdir build
cd build
cmake ..
make -j4
sudo make install

4. Download ROS 2 source code

git clone https://github.com/MiRoboticsLab/cyberdog_ros2.git
mkdir -p ros_src/src
cp cyberdog_ros2/tools/ros2_fork/* ros_src
cd ros_src
vcs import src < mini.repos
# if you need packages in extend.repos, run below
vcs import src < extend.repos

Currently we provide two repos files integrated according to the function package of the ROS community, namely:

  • mini.repos: Required. It's the minimal packages for CyberDog. It contains basic communication and interface definitions, as well as key dependencies.
  • extend.repos: Optional. It contains components such as visualization and point cloud processing.

5. Building ROS2

Pretreatment
export ROS_VERSION=2
Building
sudo mkdir -p /opt/ros2/galactic
sudo chown $USERNAME /opt/ros2/galactic
colcon build --merge-install --install-base /opt/ros2/galactic --parallel-workers 20

If a 'warning' occurs, you can just ignore it, which means it usually needs to be run twice.

If it reports an error, you can first try to find the answer in the community:

There are some common errors, such as:

Starting >>> shape_msgs
--- stderr: libstatistics_collector
CMake Error at CMakeLists.txt:100 (rosidl_get_typesupport_target):
  Unknown CMake command "rosidl_get_typesupport_target".

It is usually a version support problem caused by the ros module master branch during the rolling iteration process as mentioned above. Below are the solutions:

cd src/ros-tooling/libstatistics_collector
git checkout galactic

6. Add script

If you are using zsh, add the following script to ~/.zshrc. If you are using bash, add the following script to ~/.bashrc. If it's other, handle it as appropriate.

ros2_on(){
      export ROS_DOMAIN_ID=42
      export ROS_VERSION=2
      export ROS_PYTHON_VERSION=3
      export ROS_DISTRO=galactic
      source /opt/ros2/galactic/setup.zsh
 }

7. Instructions

After updating the environment variables, run ros2_on to deploy the ROS 2 environment.