In previous articles, we have introduced the environment for accelerating development using QEMU + Buildroot to build ARM64 (https://moon-half.info/p/4051). This approach can successfully create a small environment to test developed programs. However, after all, it is not a complete Linux distribution and lacks many things compared to general Ubuntu, such as gcc.
The reason for compiling on ARM64 is to port Python3 to the product. But one of the advantages of Python is that there are already many pre-compiled or well-organized packages, so you don’t have to recompile them one by one. I was also at a loss as to how to do these things with only a Buildroot environment. So I thought that in ARM64 Linux, using pip to install everything and then packaging it all together and placing it on the target would be fine!
And it really works!
This article uses QEMU to run Ubuntu 20.04 ARM64 and pairs it with miniconda to use python3.7 (coordinated with the target’s Python version), establish the environment, and install packages.
Install Ubuntu ARM64 with QEMU
Download ISO file
Firstly, you need to download the ARM64 ISO file of Ubuntu. You can find relevant information by searching online for “ubuntu arm64 iso download”. There may be Raspberry Pi versions, server-live versions, etc. http://old-releases.ubuntu.com/releases/ has comprehensive collection, listing all versions and files for different platforms.
However, the version downloaded from the website had the word “live-server” in it, which sounded uncomfortable to me, so I didn’t download it. The location of the legacy version is https://cdimage.ubuntu.com/ubuntu-legacy-server/releases/20.04/release/. The file I finally downloaded is ubuntu-20.04.1-legacy-server-arm64.iso.
Installing packages and generating image files
Because most QEMU ARM64 articles start with specifying the kernel directly. Actually, I have no idea how to install ARM64 environment from an ISO. So, I follows then content based on this article.
First, install the package.
Then issue the following commands to create two block devices which will act as flash. The first one (flash0.img), written in the QEMU EFI image contained in the just installed kit.
And use qemu-img to create a system disk. Here we use the raw format instead of qcow2. This makes it easy to move the created environment to Windows for use.
Finally, start QEMU and specify the location of the flash, hard drive, and ISO file to perform the installation using the following command.
The next steps are roughly the same as for a typical x86 Linux. Since it is cross-platform simulation, it is relatively slow, so please be patient and wait~
After installation, delete the 4th line to avoid booting from the ISO and entering the installation process again.
After entering the ARM64 Linux environment, you need to install the gcc package in order to compile source code when using pip.
As we mentioned before, the purpose of using Ubuntu ARM64 is to migrate binary files to target platform. Here, I use miniconda python3.7 4.9.2. For other versions, you can find them at https://repo.anaconda.com/miniconda/.
Due to the compatibility of Python versions being determined by the first two digits of the version number, it is important to choose the right version for similar actions. For example, 3.7.5 and 3.7 are compatible, but not with 3.6 or 3.8.
Install packages in Conda environment
Due to the fact that Ubuntu ARM64 is not included in the official support list, it is in a state of having bugs and may or may not be fixed. However, general operations seem to be okay. The problem occurs when using conda, such as an “illegal instruction,” which may be due to differences in instruction sets for different ARM64 CPUs or other unknown issues.
This version of miniconda’s base environment is functioning properly after installation and can be used to normally install packages with pip. However, if a new environment is created, it will crash as soon as pip is executed. I was trying other versions and having similar problems.
Collecting the packages
Taking advantage of this opportunity, I also learned about the installation behavior of .whl files, which roughly involves placing the contents of the decompressed file in $CONDA/lib/python3.x/site-packages/. If you want to force install some packages, you can do it this way. The directory $CONDA/lib/python3.x/lib-dynload/ will also contain some files that are installed during the process, but it is not clear whether they differ from those directly decompressed. This directory refers to the installation using the base environment directly, and if there is a created env operation, it will be located in $CONDA/envs/.
After creating a new environment or reinstalling conda, install the packages you want. Finally, package up the $CONDA/lib/python3.x directory and overwrite the relative directory of the Python 3.7 environment in target, and you’re done.
I installed the Streamlit package and it downloaded a lot of additional things, which would take up about 355MB. I can do some stripping and delete files like test/, but the effect is not significant. After packaging with squashfs xz, it is about 80MB.
Since some packages also need to be recompiled, when selecting an ARM64 Linux version for installation, it is best if the inside gcc version is lower than the target gcc version. This can avoid compatibility issues with libraries. As for how to know which version of gcc is inside, you still have to try installing it to find out.