Crystal & Alpine on the Raspberry Pi

Bringing together two interesting technologies:

  • Alpine Linux – a small-size and security oriented Linux distribution
  • Crystal – a compiled language with a nice syntax (similar to Ruby)
  • on the Raspberry Pi (armhf / ARMv6, which would also support the Pi Zero W)

is not possible currently, as of 21.12.2018.

There is no (at least no easy) way to cross-compile Crystal applications for the Raspberry Pi and Alpine Linux, from, for instance, x86 platforms, as the necessary target arm-linux-musleabihf is not supported yet.

Misc background information

Unfortunately it’s not possible to compile crystal on Alpine/ARM/Raspberry using gcc or the like, as “to compile Crystal you need Crystal” – the compilation will fail:

pitest:/usr/local/crystal/crystal# make
Using /usr/bin/llvm-config [version=5.0.1]
g++ -c  -o src/llvm/ext/llvm_ext.o src/llvm/ext/llvm_ext.cc -I/usr/lib/llvm5/include -Os -founused-parameter
-Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-fdata-sections -Os
-fomit-frame-pointer -Os -DNDEBUG  -fno-exceptions -DLLVM_BUILD_GLOBAL_ISD__STDC_LIMIT_MACROS
cc -fPIC    -c -o src/ext/sigfault.o src/ext/sigfault.c
ar -rcs src/ext/libcrystal.a src/ext/sigfault.o
CRYSTAL_CONFIG_PATH=”/usr/local/crystal/crystal/src” CRYSTAL_CONFIG_BUILD_COMMIT=”74c65ce6b”                                                                                               lib

You need to have a crystal executable in your path!

make: *** [Makefile:123: .build/crystal] Error 1

The idea is that you crosscompile crystal on another architecture, and then can use it to natively compile it.

Alpine Linux uses musl, an alternative libc.

To find out the name of the target (LLVM target triple), you can execute llvm-config on the target system, like this:

llvm-config –host-target

To be able to run this command on Alpine, you install two packages:

apk add llvm

apk add llvm-dev

When trying a target which Crystal does not support yet for crosscompilation, you will get this error message:

in /usr/share/crystal/src/callstack.cr:3: while requiring “c/dlfcn”: can’t find file ‘c/dlfcn’

These libraries are located here:

/usr/share/crystal/src/lib_c

Crystal is supported on Alpine, on the aarch64 (ARM 64bit) and x86_64 platforms. (and x86 via portalier.com)

In general, crosscompiling with Crystal works like this:

crystal build –release –cross-compile –target “aarch64-linux-musl” src/armtest.cr

it will give you a command line to execute on the target machine:

cc ‘armtest.o’ -o ‘armtest’  -rdynamic  -lpcre -lgc -lpthread /usr/share/crystal/src/ext/libcrystal.a -levent -lrt -L/usr/lib -L/usr/local/lib

See these references: