Building ROS Melodic with Python3 support
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 demo example to test the python3 integration. So, without further delay, here we go!
This tutorial assumes a few things:
- You are familiar with ROS
- You would like to run ROS Melodic in Ubuntu Linux 18.04 with Python3 support
- You are familiar with the ROS build system
- Familiar with the Linux command line, a shell like bash, and an editor like vim
First things first, we need to setup our build environment properly. As mentioned above, we will be using Ubuntu 18.04 LTS, so, we will simply refer to the OS as Ubuntu.
Remove all things python2 (Optional)
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 command line:
Remove any previous installations of ROS
It’s probably a good idea to remove any previous versions of ROS. So, for example, to remove a default melodic install, execute the following in a shell:
Setup Python3 dependencies
Since we will be building ROS Melodic from source to support python3, we need to install several dependencies.
We’ll also need to install ROS specific packages using pip3:
Initialize catkin build environment
As with any standard ROS installation, we need to initialize rosdep:
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.
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 located within the ros_catkin_ws directory.
Setup ROS 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.
If a download fails, just re-run the following command until all packages download properly:
Setup environment and install dependencies
Here is where things start to diverge a bit from the default build from source procedure. First, we need to have an environment variable called ROS_PYTHON_VERSION set to 3:
We need to install wxPython for the build to complete.
We will also need to create a shell script called install_skip with the following:
and make the script executable:
And now for a little SED magic!
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 with the following:
We’ll now install all of the remaining dependencies using rosdep and skip the python2 based dependencies:
The final step before building is replacing all references to python2 in the shebangs to python3:
Be sure to only run this once. If you run this twice by mistake, you might end up with shebangs that have python33 instead of python3.
Finally, build ROS!
With all the setup out of the way, we’re ready to build ROS Melodic with python3 support:
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.
You should do this prior to sourcing the setup in the devel directory:
to be able to access ROS via the command line (assuming that you are running 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 Melodic build, along with a simple example to test the installation. Until next time!
I’ve added the code snippets to a gist here to make bug tracking a bit easier.