Developing aarm64 Native ML tools on a Raspberry-Pi4 / CM4

As an innovator, I always enjoy pushing the limits. Raspberry Pi4 and CM4 already enable significant extensions with 8GB RAM, 32GB eMMC easy add-ons and 2.00 GHz stable performance. The CM4-DUAL-ETH BASE supports the CM4 modules and provides three USB2-Gen1, Dual Gigabit Ethernet with Dual band Wifi and Bluetooth5.0 BLE to communicate, an EMC2301 Fan controller and RTC clock and a very small footprint.

The Waveshare CM4-DUAL-ETH-BASE chosen for this project was the fully enabled but elusive and hard-to-find CM4108032. A Noctua NF-A4x10 5V PWM Premium Quiet Fan insured adequate active and regulated cooling, onced interfaced properly on the Waveshare board.

To be prepared for large amount of compilation and tools, a Samsung USB3.1 Flash Drive FIT Plus 256GB was initially loaded with Ubuntu Mate 21.10 (Impish Indri) 64-bit image prepared with Balena Etcher.

All the following was thus performed on this fresh install Ubuntu 21.10 aarm64 platform, after preparing the CM4 to Boot from USB and formatting but laving the eMMC CM4 module available for further memory extension. Once the OS was installed, an 8GB Swap file was enabled on the eMMC module, as evidenced in the next figure.
Fig 1. - htop provides core, memory, swap and process status

Fig 1. - htop provides core, memory, swap and process status

Amongst the main initial tools, in light of expected long build sessions and loads on the quad CPU, it is certainly recommended to get the hardware properly cooled, and instructions were followed to accomplish this from the Waveshare Wiki.

An OpenSCAD design was then completed and produced on a Prusa-MK3S printer for a multi-part housing to contain this hardware as well as optional USBC portable SSD should the need to extend data storage surfaces down the road.
Fig 2. - OpenSCAD multi-part CM4 Custom Box design

Fig 2. - OpenSCAD multi-part CM4 Custom Box design

From there, simply importing the STL produced into PrusaSlicer provides the path to the part needed.
Fig 3. - PrusaSlicer STL multi-part CM4 Custom Box ready for slicing

Fig 3. - PrusaSlicer STL multi-part CM4 Custom Box ready for slicing

The final assembly produced in operation is shown next, powered, with dual RPI cameras, wifi antenna and connected to a 7ā€ tactile screen. It is powered by a regulated 12V 5A module. along with the finished application i action.
Fig 4. - Final assembly of the multi-part CM4 Custom Box in operation

Fig 4. - Final assembly of the multi-part CM4 Custom Box in operation

On this next screen capture, the finished aarm64 RStudio Desktop IDE is captured and the markdown document you are reading was produced on it.
Fig 5. - aarm64 RStudio Desktop IDE

Fig 5. - aarm64 RStudio Desktop IDE

Note that once the application was fully operational, a migration to the recently available Ubuntu 22.04 LTS was performed via a system upgrade, and only minor adjustments were required to retain full RStudio IDE functionality in the new operating system.
Fig 6. - Neofetch in Final Ubuntu 22.04 LTS

Fig 6. - Neofetch in Final Ubuntu 22.04 LTS

Building Natively an aarm64 RStudio Desktop IDE from source in Ubuntu Mate 21.10

Rstudio currently provides only x86_64 and amd64 prepackaged tarballs for Debian 10+. So the aarm64 version must be build from the source provided. Some references can be found for cross-compilation, but we decided to pursue a native build on the aarm64 platform to further demonstrate this alternative capability. You will need a great deal of perseverance and patience to get it working, but, as illustrated above, the task can be successfully accomplished. Start with retrieving the source.

cd ~
mkdir src
cd src
git config --global --add safe.directory /home/pi/src/rstudio
wget https://github.com/rstudio/rstudio/archive/refs/tags/v2022.02.2+485.tar.gz

Once the source downloaded, open the README.md and INSTALL files to get familiar with the pre-requirements and building process, and follow particularly since you won’t find a Ubuntu 21.10 platform target.

To manage the numerous libraries, aptitude was used frequently along the way. As you no doubt will require compiling, linking, and other library/package installation, consider using the LLVM toolchain where an automatic installation script will conveniently automate the process. Including the toolchain in the PATH will smooth further building.

bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
sudo ./llvm.sh all
sudo apt update
sudo apt upgrade
export LLVM_INSTALL_DIR=/usr/lib/llvm-11/bin
ENV PATH=/usr/lib/llvm-11/bin:$PATH

Following this toolchain, you can likely use the install-dependencies-focal as basis for the libraries you need to install prior to conduct more intensive building. Alternatively, follow the install sequence given in Dockerfile.debian10-x86-64 located in the rstudio/docker/Jenkins directory of the source…In particular, make sure to exactly locate for these dependencies so that RStudio Desktop build will be successful. This means creating a few sub-directories for /opt/rstudio-tools:

mkdir -p /opt/rstudio-tools/dependencies/common
sudo cp dependencies/common/ /opt/rstudio-tools/dependencies/common/
mkdir -p /opt/rstudio-tools/panmirror
sudo cp src/gwt/panmirror/src/editor/yarn.lock /opt/rstudio-tools/panmirror/
sudo cp src/gwt/panmirror/src/editor/package.json /opt/rstudio-tools/panmirror/
cd /opt/rstudio-tools/dependencies/common && /bin/bash ./install-common debian10

Note we will build without crashpad as it is not currently supported on aarch64.

sudo apt-get update

sudo apt-get -y install ant build-essential clang cmake curl debsigs dpkg-sig expect fakeroot git   gnupg1 libacl1-dev libattr1-dev libbz2-dev libcap-dev libclang-6.0-dev libclang-dev   libcurl4-openssl-dev libegl1-mesa libfuse2 libgl1-mesa-dev libgtk-3-0 libpam-dev libpango1.0-dev   libpq-dev libsqlite3-dev libssl-dev libuser1-dev libxslt1-dev lsof ninja-build p7zip-full patchelf   pkg-config python python3 python3-venv rrdtool software-properties-common unzip uuid-dev wget   zlib1g-dev

Building an aarm64 R4.20 Natively from source

The primary requirement for the RStudio Desktop is R itself. Again, the version distributed on CRAN targets amd64 platformss, and consequently, you will need build R for the aarm64 platform. On the plus side, once completed, this task will provide R4.2.0 on your platform, much more recent than the minimum required version 3.3.0 listed in the linux dependencies.

cd ~/src
wget https://cran.r-project.org/src/base/R-4/R-4.2.0.tar.gz
tar -xzvf R-4.2.0.tar.gz

We’ll update our toolchain and obtain few dependencies for building a decent version of R. These include notably gfortran, libatlas-base-dev, libopenblas-dev, libicu-dev and a few others… The LLVM-14 toolchain is used for this build.

cd
sudo ./llvm.sh
cd src
cd R-4.2.0
export LLVM_INSTALL_DIR=/usr/lib/llvm-14/bin
sudo apt-get install libatlas-base-dev
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install -y gfortran-10 gfortran libbz2-dev libcurl4-openssl-dev texlive texlive-fonts-extra libopenblas-dev libicu-dev icu-devtools

In addition, we will provide a few JAVA alternatives…

sudo add-apt-repository ppa:linuxuprising/java
sudo apt update
sudo apt install oracle-java15-installer
sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt update

sudo apt-get install -y openjdk-15-jre openjdk-15-jdk openjdk-11-jdk
sudo update-alternatives --set java /usr/lib/jvm/java-11-openjdk-${arch}/bin/java
sudo update-java-alternatives --list

With these pre-requisite libraries installed, the configuration and make of R can be executed.

./configure     --prefix=/opt/R/${R_VERSION}     --enable-memory-profiling     --enable-R-shlib     --with-blas     --with-lapack --with-readline=no --with-openblas --with-icu

make
sudo make install
sudo make install-info
sudo make install-pdf

We then verify the installed version of R and make links in /usr/local/bin as this is the location that will be target for execution and where RStudio will default search.

/opt/R/${R_VERSION}/bin/R --version
sudo ln -s /opt/R/${R_VERSION}/bin/R /usr/local/bin/R
sudo ln -s /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript
R

You will also need to download and install boost 1.79 and a recent version of CMake. I used version 3.22.1 which is more than the required minimum 3.4.3, and Patchelf 0.9.

Building an aarm64 QT 5.10.8 Natively from source

The major hurdle will be to download, build and install a release version of QT 5.12.8 specifically into /opt/Qt5.12.8 location for further use by RStudio. You can count of at least 17 hours of compilation and linking for this stage, and, in addition, a few restarts as there are further patches necessary to cope with the current toolchain requirement as we’ll see soon… It is best at this stage to carefully study the Qt5 building requirements and process as detailed in the Building Qt5 from Git wiki. Follow diligently all library requirements and install with either apt or aptitude. Once the required development libraries installed, you can proceed with building a release version of Qt5.12.8 and target /opt/Qt5.12.8 for installation.

cd ~
mkdir -p /opt/Qt5.12.8
git clone https://code.qt.io/qt/qt5.git
cd qt5
git checkout 5.12.8
perl init-repository
mkdir qt5-build
cd qt5-build
../qt5/configure -release -opengl desktop -opensource -confirm-license -qt-pcre -eglfs -make libs -no-pch -ssl -evdev -fontconfig -qt-freetype -system-freetype -qt-zlib -qt-libjpeg -qt-libpng -pkg-config -v -prefix /opt/Qt5.12.8

This configuration alone may take some 12 minutes and is likely to even to stop due to stringer compiler requirements, and the now-required include statements in some header files. Just run

gmake

I noted at least 10 occurrences of such omissions which each required minor patching of the header file, mostly. Rather than pinpointing the specific files, the remedies are indicated here.

#include <limits>
#include <cstdint>
...std::max<size_t>(16384...)

The last patch involved a bit more changes in qt5/qtwebengine module for a file mis-behaving:/chromium/third_party/blink/renderer/build/scripts/rule_bison.py. Once the build completed, install it with

sudo gmake install

and insure you have built the qtwebengine libraries. There are a few more items that will be required to make RStudio Desktop behave. One of them are the icu libraries, specifically libcudata, libicui18n and libicuuc which all require version 56.2 to work with RStudio Desktop IDE. Since the corresponding libraries distributed via apt are currently version 71, we must again target a build from source. With a bit of research, the necessary archives can be located.

cd src
wget https://github.com/unicode-org/icu/archive/refs/tags/release-56-2.tar.gz
tar xzvf release-56-2.tar.gz
chmod +x runConfigureICU configure install-sh
cd icu-release-56-2/icu4c/source
make
sudo make install

Copy the 3 libraries into the /opt/Qt5.12.8/lib directory. To build rstudio, a few more dependencies are required, and the provided scripts was followed. Likely, some of these are already installed, but a few may still be added in the process.

cd ../rstudio

sudo apt-get -y install ant build-essential clang cmake curl debsigs dpkg-sig expect fakeroot git  gnupg1 libacl1-dev libattr1-dev libbz2-dev libcap-dev libclang-6.0-dev libclang-dev libcurl4-openssl-dev libegl1-mesa libfuse2 libgl1-mesa-dev libgtk-3-0 libpam-dev libpango1.0-dev libpq-dev libsqlite3-dev   libssl-dev libuser1-dev libxslt1-dev lsof ninja-build p7zip-full patchelf pkg-config python python3   python3-venv rrdtool software-properties-common unzip uuid-dev wget zlib1g-dev

cd dependencies/common
./install-common
./install-cef
./install-yarn-linux-aarch64
./install-yarn
./install-node
cd ../linux
./install-qt-sdk --QT_VERSION=5.12.8
sudo apt install patchelf

We have finally met all dependencies and can proceed with the RStudio Desktop IDE build. We also need to patch the CMakeGlobals.txt and modify the section ~line 125 and set the RSTUDIO_DESKTOP flag to TRUE to enable building for aarm64.

# set desktop and server build flags
if(NOT DEFINED RSTUDIO_SERVER)
   if(NOT WIN32 AND (RSTUDIO_TARGET STREQUAL "Development" OR RSTUDIO_TARGET STREQUAL "Server"))
      set(RSTUDIO_SERVER TRUE)
   endif()
endif()

if(NOT DEFINED RSTUDIO_DESKTOP)
   if(RSTUDIO_TARGET STREQUAL "Electron")
      set(RSTUDIO_ELECTRON TRUE)
   elseif(LINUX AND UNAME_M STREQUAL aarch64)
      message(STATUS "Desktop builds attempted on aarch64; enabling RSTUDIO_DESKTOP")
      set(RSTUDIO_DESKTOP TRUE)
   elseif(APPLE AND UNAME_M STREQUAL arm64)
      message(STATUS "Desktop builds not currently supported on arm64; disabling RSTUDIO_DESKTOP")
      set(RSTUDIO_DESKTOP FALSE)
   elseif(RSTUDIO_TARGET STREQUAL "Development" OR RSTUDIO_TARGET STREQUAL "Desktop")
      set(RSTUDIO_DESKTOP TRUE)
   endif()
endif()

Note also that quarto is not currently supported on aarm64 and so it is bypassed, ~ line 232 in the same CMakeGlobals.txt file.

# quarto support
if(LINUX AND UNAME_M STREQUAL aarch64)
   # disabled on linux aarch64
   message(STATUS "quarto does not yet support aarch64 builds of Linux; disabling quarto")
   set(QUARTO_ENABLED FALSE CACHE INTERNAL "")
else()
   # enable by default
   set(QUARTO_ENABLED TRUE CACHE INTERNAL "")
   add_definitions(-DQUARTO_ENABLED)
endif()

In addition, the build must be performed with sufficient permission, so sudo make is required for the Java compiler to build successfully.

cd ~/src/rstudio
mkdir build
cd ../build
cmake .. -DRSTUDIO_TARGET=Desktop -DRSTUDIO_PACKAGE_BUILD=1 -DCMAKE_BUILD_TYPE=Release -DQT_QMAKE_EXECUTABLE=/opt/Qt5.12.8/bin/qmake
sudo make
sudo make install
sudo make package
At that point, you should have rstudio-2022.05.999-dev-999-arm64.sh and 2 tarballs: rstudio-2022.05.999-dev-999-arm64.tar.gz and rstudio-2022.05.999-dev-999-arm64.tar.Z. You’ll need a few more tweaks to get this working but you are very close now!
Fig 7. - rstudio/build directory at completion of the building process

Fig 7. - rstudio/build directory at completion of the building process

Enabling the aarm64 RStudio build functionality

When rstudio is launched as built, the window opens with all the icons, pull-down menu-box and check-boxes, but no font is rendered! The easy fix is to copy the fonts folder under /usr/local.
Fig 8. - usr/local/lib directory with a copy of fonts folder

Fig 8. - usr/local/lib directory with a copy of fonts folder

Another requirement is to launch RStudio Desktop with the –no-sandbox flag. This is easily amended in the menu link as well.
Fig 9. - Customized rstudio menu includes --no-sandbox option

Fig 9. - Customized rstudio menu includes –no-sandbox option

At that point, you’ll have a Ubuntu 21.10 version working, and that is an achievement! The very last hurdle to face, if upgrading Ubuntu to 22.04 LTS, is that the new distribution comes with version 7 of libwebp, while you built all with libwebp version 6, as you can observe in /usr/local/lib. The easiest fix for this, rather than to reiterate the whole building sequence, is to rename a copy of libwep.so.7 link as libwep.so.6 and keep the RStudio application happy!

cd /usr/local/lib
sudo cp libwebp.so.7 libwebp.so.6
and voila! No more complaints upon launching the application!
Fig 10. - A Dual Screen capture with RStudio Desktop IDE windowed version

Fig 10. - A Dual Screen capture with RStudio Desktop IDE windowed version

The application natively built on the CM4 aarm64 platform is behaving with all the windowing / resizing and functionality we observe on the other 64bit platforms. You now have an essential data science tool performing on a reduced footprint and performing nonetheless! In fact, the markdown you are reading was generated with the very same version just built!

Other tools: Python 3.10 Jupyter Notebooks and Visual Studio Code

As Ubuntu 22.04 LTS comes with Python 3.10, it is straight-forward to implement the Python environment and Jupyter notebook on the aarm64 platform. Porting substantial size notebooks from a desktop to the aarm64 platform is demonstrated here.
Fig 11. - IPython notebook runs natively on aarm64

Fig 11. - IPython notebook runs natively on aarm64

Alternatively, it is equally possible to select the Visual Studio Code aarm64 application and perform development and/or studies for Data Science on the Pi4 / CM4!
Fig 12. - VS Code Statistical Analysis runs natively on aarm64

Fig 12. - VS Code Statistical Analysis runs natively on aarm64

Where to go from there…

After this extensive building exercise, it has now clearly demonstrated that multiple modern Data Science tools have their place on the aarm64 platform. The choice is yours to select the programming tool best suited for your task and you have lifted some platform restrictions. Further, it has been clearly demonstrated that with existing RAM and USB3.2 data storage size being essentially lifted, native builds are really possible. I can only hope that Data Science learners and established scientists will benefit from these broadening horizons and extend even further the aarm64 platform and Data Science applications reach. Enjoy and Use this new options!