By using the Raspbian distro on the Raspberry PI (or any debian / main stream distro for that matter on any embedded device), most of the software packages that you would want to install are available within the package manager of the distribution. Sometimes there some small / medium sized programs missing, or you want to use some specific sourcecode. At those times you just download the sourcecode and compile it on the Raspberry Pi. The CPU and Memory of the device is not great, but with some patience it just takes a cup of coffee (or two, or three). For instance for our DiY Personal A.I. Assistant project we compile the Mycroft sourcecode directly on the Raspberry Pi. Building Mimic2 a part of the Mycroft software stack, takes around ~2-3 hours. Doable if you ask me.
However if you need to compile sourcecode bigger than that it becomes a pain in the …… Waiting a few hours, OK. Waiting countless hours or like with WPE Webkit which I am currently looking at, it is more like waiting days !!! Not acceptable of course. Then you are better of, (cross)compiling the software for the Raspberry Pi on a big fancy powerfull machine (or any other linux machine, which is a faster then the Raspberry Pi). How to set that up is what this article is about.
There are two ways to do this.
- By bootstrapping an ARM filesystem on our build machine and chroot into it.
- By removing the SD card from the RPi, mounting it on our build machine and chroot into it.
The first has my preference as you can do two things at the same time as your RPi is still running. The second has some benefits as you do not have to transfer your compiled binary files, however at the cost of read/write cycles to the SD card possibly wearing it out (faster).
So let’s get started. Below steps are done on my Debian 9 server I have running in my network. If you use another Debian based distribution, most likely you can use the same steps. Or at least use the steps with some minor tweaks here and there.
NOTE: Below steps installing a Debian stretch system. If you would like to build another version like jessie, please change stretch -> jessie (or whatever version you would like).
Bootstrapping a Raspbian (ARM) chroot.
First we need to install some dependencies / packages to get started.
[bash]sudo apt-get install qemu-user-static debootstrap
[/bash]Then we create a directory where in we would like to create our chroot filesystem. I created a directory called “raspbian” within my Home folder. Please change accordingly.
[bash]mkdir /home/peter/raspbian
[/bash]Now we are ready to start the first step of bootstrapping a full Raspbian filesystem onto our building machine. You can do that with the following command. Please note that only “root” can run the chroot command, so from here on we do all command as root by using “sudo” in front (like with installing packages).
[bash]sudo debootstrap –no-check-gpg –foreign –arch=armhf stretch /home/peter/raspbian http://archive.raspbian.org/raspbian
[/bash]When this first step is done, we have to copy over the right qemu-static binary which we installed at the first command above by;
[bash]sudo cp /usr/bin/qemu-arm-static /home/peter/raspbian/usr/bin
[/bash]And then we can initiate the second stage of our bootstrap process. This one take a bit longer as the first stage though.
[bash]sudo chroot /home/peter/raspbian /debootstrap/debootstrap –second-stage
[/bash]Almost there. Open up the sources.list within our chroot filesystem with a text editor.
[bash]nano /home/peter/raspbian/etc/apt/sources.list
[/bash]And change the content to;
[bash]deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi
deb http://archive.raspberrypi.org/debian/ stretch main
Now for good practice we will update all packages.
[bash]sudo chroot /home/peter/raspbian apt-get update
sudo chroot /home/peter/raspbian apt-get upgrade
That’s It! Now just change your CLI environment to the raspbian filesystem by;
[bash]sudo chroot /home/peter/raspbian
[/bash]You will notice that your CLI becomes a root CLI because of the $ changing to #. To check if you indeed running the right GCC to start compiling for ARM you can run the following command.
[bash]gcc -v
[/bash]If everything went as it should, you should see; “Target: arm-linux-gnueabi”. You can now do whatever you would have liked to do on the Raspberry Pi. (./configure, make, etc etc.)
Using the Raspberry Pi SD card instead of bootstrap
If you would rather using the second option by using the SD card as this chroot environment. Remove teh SD card from your RPi and insert it into your building machine. Depending on the card, the reader and other thing it will be picked up differently. You can check the /dev/ entry for your card by;
[bash]sudo fdisk -l
[/bash]You will get output like;
[bash]Disk /dev/sdc: 2013 MB, 2013265920 bytes
4 heads, 32 sectors/track, 30720 cylinders, total 3932160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ee283
Device Boot Start End Blocks Id System
/dev/sdc1 2048 155647 76800 c W95 FAT32 (LBA)
/dev/sdc2 157696 3414015 1628160 83 Linux
/dev/sdc3 3416064 3807231 195584 82 Linux swap / Solaris
The first partition is the /boot partition. /dev/sdc2 is the linux partition we are after. Now instead of bootstrapping a raspbian filesystem, we are just going to mount our SD card instead.
[bash]sudo mount /dev/sdc2 /home/peter/raspbian
[/bash]We then still need to copy over our qemu-static binary as above;
[bash]sudo cp /usr/bin/qemu-arm-static /home/peter/raspbian/usr/bin
[/bash]That is it, you can now use the same chroot command to change into the filesysten as user root as described above.
Do you like what you just read? Keep me going by donating on Paypal or become a Patron.
Thank you, this is exactly what I was looking for! I didn’t know about qemu-user-static… Very cool.
The directory containing the chroot should belong to the root. The best would be to create it as root, e.g. “sudo mkdir /home/peter/raspbian” instead of simply “mkdir /home/peter/raspbian”.
Otherwise, the second staged of debootstrap fails with some mysterious error message.