The Crystal Programming Language Forum

Crystal on Void Linux with Musl

I have just posted something similar on the Void forum and they suggested I try over here.
I am using Void with musl C lib and would like to know how I can get Crystal running in this environment. I notice that Apline linux (also musl based) has a package to install. Any suggestions would be much appreciated.

I think the crystal binary that is distributed is statically linked.

So, in theory, you should be able to just download the debian package unpack it and put the files where your distribution expects them. (/usr/bin, /usr/lib/crystal, etc)

Right?

There’s also an xpbs package available for crystal. In principle you should just need to install that. Whether you’re using musl or gnu libc doesn’t matter, the compiler detects which one to link against automatically according to the output of ldd --version.

PS When posting the same question on multiple platforms, it’s very much appreciated to put links between them.

Thanks guys - busy yesterday but looking at this again. Void does have a crystal package, but only for the glibc version of x86_64 and not the musl one. The glibc version is dynamically linked so I am trying to compile a statically linked crystal version using this in a glibc chroot. I had to do similar for Java.
I will link in the future (when I figure out how) but it just says “musl and crystal” are there own thing - go check there. I don’t agree with this as I see it as a packaging issue. Anyway I will update progress over the next few days as I try to follow the Alpine references I came across. Tks.

Oh, I see. I suppose the crystal package template for glibc should essentially just work with musl. At least from Crystal’s perspective it doesn’t make a difference. I’m not sure about any particularities with xbps, though.

Alternatively, you can just download the Crystal tarball from the releases page and install the required libraries manually. gc-devel pcre-devel libevent-devel gcc pkg-config should do it.

That is sorta what I have been doing. Early days, but the error is “bin/ld: cannot find -lxml2”. I have libxml2 and libxml2-devel installed so maybe it is a PATH issue.

What are you trying to compile that binds libxml?
Does a basic puts "Hello World" work?

Not sure what is trying to bind. I did this in a bit of a hurry and think I need to redo with more care. I will post how this goes - couple of other things need my attention for now.

Looks like libxml2 is a make-depends for Crystal and I only have libxml2.so and not the static .a version. So I need to rethink my strategy as the other dependencies will create errors as well. I will try to reach out the Void developer for the glibc version of Crystal to see if I can decide on the best approach. The idea for the static glibc version of Crystal ( in the chroot isolation ) was to be able to compile the musl version. Does this sound like a viable way to go?

Building Crystal doesn’t depend on libxml2. It’s only necessary for the stdlib specs.
You don’t necessarily need to build Crystal, the generic Linux binary should just work.
Building std_spec should also work with a shard library since you probably don’t need a static binary for that.

Thanks. I was wondering why libxml2 was needed. I am also looking at Alpine linux and Docker - I wanted to learn about Docker anyway. I understand to build crystal under musl I need to have a crystal compiler that will work there. I have a dynamic crystal package installed in the gligc chroot, but when I try to run this outside the glibc-chroot I get the library not found errors. You can probably guess this is a learning process for me and I appreciate your help in pointing me in the right direction. I’ll update when I play with the crystal-alpine docker image.

Not necessarily. You can also cross-compile from a glibc system using the compiler flags --cross-compile --target x86_64-linux-musl. This compiles the object files and output a linker command which you need to run on a musl system.

Alternatively, as mentioned before the linux binary from the official release is statically compiled and runs on both glibc and musl systems because it doesn’t need any runtime libraries.

Thank you for being so patient with me. What was throwing me off was the Void glibc crystal is dynamically linked. Anyway I followed your advice and used linux-x86_64.tar.gz to give me the static crystal compiler. This gave me some new errors about undefined references in libgc.a. I had installed libgc in Void, but the search path was finding the one in the above archive. Once I realized this and started to use the Void version, I could run the “helloworld.cr” example OK - yea. I will try my project files now to see how they turn out. Also after RTFM I came across the Wiki article on Static Linking which explained it all. Again many thanks.

1 Like