20 Aug 2019
To kick off my new blog, I thought that I’d tackle an issue that’s been bugging me for a few months now: how the hell do I
get python3 support in ROS Melodic! In this article, I will go through the steps required to build ROS Melodic from source
with full python 3 support. Future posts will discuss how to integrate this new build with pycharm, as well as, how a simple
First things first, we need to setup our build environment properly. As mentioned above, we will be using Ubuntu 18.04 LTS,
This step is optional, but I recommend it to have a clean build. Any other system packages that are removed can be
reinstalled later by following the instructions here. So, to remove all python2 packages, execute the following on the
$ sudo apt-get remove python- *
It’s probably a good idea to remove any previous versions of ROS. So, for example, to remove a default melodic install,
$ sudo apt-get remove ros- *
$ sudo apt-get remove ros-melodic- *
$ sudo apt-get autoremove
Since we will be building ROS Melodic from source to support python3, we need to install several dependencies.
$ sudo apt update
$ sudo apt install -y python3 python3-dev python3-pip build-essential
$ sudo -H pip3 install rosdep rospkg rosinstall_generator rosinstall wstool vcstools catkin_tools catkin_pkg
$ sudo rosdep init
$ rosdep update
and create the catkin workspace that we will use to build ROS. Please be sure of the location of the workspace, since the
entire build will be tied to that location. If the workspace folder is deleted or moves, it will corrupt the installation.
$ cd ~
$ mkdir ros_catkin_ws
$ cd ros_catkin_ws
Next, we have to initialize the catkin workspace. If you’re unfamiliar with the catkin build tools, you can check out the docs
here. Note: This initialization will build the release version of ROS Melodic and install it to a directory called “install” that is
$ catkin config --init -DCMAKE_BUILD_TYPE = Release --blacklist rqt_rviz rviz_plugin_tutorials librviz_tutorial
--install
ROS has many different flavors of installations: desktop, desktop-full, ros_core, robot, etc. For development purposes, I
would recommend installing desktop-full, but feel free to install the flavor that meets your needs. Simply replace desktop-full
with the installation flavor of your choice, e.g. ros_base. You can read more about the various options here.
$ rosinstall_generator desktop_full --rosdistro melodic --deps --tar > melodic-desktop-full.rosinstall
$ wstool init -j8 src melodic-desktop-full.rosinstall
$ wstool update -j4 -t src
Here is where things start to diverge a bit from the default build from source procedure. First, we need to have an
$ export ROS_PYTHON_VERSION = 3
$ pip3 install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-18.04 wxPython
#!/bin/bash
#Check whether root
if [ $(whoami) != root ] ; then
echo You must be root or use sudo to install packages.
return
fi
#Call apt-get for each package
for pkg in "$@"
do
echo "Installing $pkg"
sudo apt-get -my install $pkg >> install.log
done
$ chmod +x install_skip
We need to install all of the dependencies for the ROS source, but for python3 instead of python3. That means that we
need to see what the ROS packages need and install the python3 version instead of the python2 version. We can do that
$ sudo ./install_skip `rosdep check --from-paths src --ignore-src | grep python | sed -e "s/^apt\t//g" | sed
-z "s/\n/ /g" | sed -e "s/python/python3/g"`
We’ll now install all of the remaining dependencies using rosdep and skip the python2 based dependencies:
$ rosdep install --from-paths src --ignore-src -y --skip-keys = "`rosdep check --from-paths src --ignore-src |
grep python | sed -e "s/^apt\t//g" | sed -z "s/\n/ /g"`"
$ find. -type f -exec sed -i 's/\/usr\/bin\/env[ ]*python/\/usr\/bin\/env python3/g' {} +
Be sure to only run this once. If you run this twice by mistake, you might end up with shebangs that have python33 instead
$ catkin build
And that’s it! Once the build finishes, you will have a ROS Melodic build that supports python3. The last step is to export the
PYTHON_PATH environment variable to let ROS know where all of the system python3 packages are installed.
$ export PYTHONPATH = /usr/lib/python3/dist-packages
$ source ~/ros_catkin_ws/install/setup.bash
In summary, assuming that you are familiar with Ubuntu, the command line, and the ROS build system, I showed you how
to build ROS Melodic with python3 support. Next post, I’ll be discussing how to setup pycharm with our python3 ROS
20 Aug 2019