Compiling cv_bridge for ROS Melodic with Python 3 (System and Conda Environments)
This guide details step‑by‑step instructions for setting up Ubuntu 18.04 with ROS Melodic, installing required packages, compiling cv_bridge using both the native Python 3.6 environment and a Conda‑based Python 3.x environment, troubleshooting common build errors, and verifying the installation.
Environment
Ubuntu 18.04, Python 3.6.9 / Conda Python 3.6, ROS Melodic.
1. Compile cv_bridge in native Python 3.6 environment
<code>sudo apt-get install python3-dev python3-numpy python3-yaml ros-melodic-cv-bridge python3-rospkg-modules</code>
<code>pip3 install pip --update</code>
<code>pip3 install rosdep rosinstall catkin_pkg</code>Initialize workspace:
<code>mkdir -p cv_bridge_ws/src && cd cv_bridge_ws/src</code>
<code>catkin_init_workspace</code>Clone source:
<code>git clone https://github.com/ros-perception/vision_opencv.git</code>Compile:
<code>cd ../</code>
<code>catkin_make install -DPYTHON_EXECUTABLE=/usr/bin/python3</code>Compilation error resolution
Problem 1: Incorrect Boost Python library path. Locate the correct libboost_python* file (e.g., in /usr/lib/aarch64-linux-gnu/ ) and adjust CMakeLists.txt lines 11 and 14 from python37 to python3 , then recompile.
2. Compile cv_bridge in Conda Python 3.x environment
Create Conda environment:
<code>conda create -n ros python=3.6</code>
<code>conda activate ros</code>
<code>pip install --upgrade pip</code>
<code>pip install rosdep rosinstall catkin_pkg rospkg numpy pyyaml opencv-python</code>Update ~/.bashrc to use conda activate ros and source the workspace after building.
Initialize workspace and clone source:
<code>mkdir -p ros_cv_bridge/src && cd ros_cv_bridge/src</code>
<code>catkin_init_workspace</code>
<code>git clone https://gitee.com/irvingao/vision_opencv.git</code>Compile with Conda Python interpreter:
<code>cd ../</code>
<code>export CPLUS_INCLUDE_PATH=/home/innox/anaconda3/include/python3.7m</code>
<code>catkin_make install -DCMAKE_BUILD_TYPE=Release -DSETUPTOOLS_DEB_LAYOUT=OFF -DPYTHON_EXECUTABLE=/home/innox/anaconda3/bin/python</code>Add the generated files to the environment:
<code>vim ~/.bashrc</code>
<code>source ~/ros_cv_bridge/install/setup.bash --extend</code>
<code>source ~/.bashrc</code>Testing
Open a new terminal and run:
<code>python</code>
<code>import cv_bridge</code>
<code>from cv_bridge.boost.cv_bridge_boost import getCvType</code>If the imports succeed, cv_bridge has been successfully compiled and can be used in ROS Python 3 nodes.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.