The Crystal Programming Language Forum

Could crystal static build to single binary?

is crystal can build to single exe or binary like golang ? ever not need glibc.

Yes, see docs https://crystal-lang.org/reference/using_the_compiler/#creating-a-statically-linked-executable

And this answer

1 Like

thanks, buddy. but why static only work on alphine ? could you share to me ? @vlazar

The main issue is with linking glibc statically (it has some features working against that), but musl works fine. Alpine is built on musl and generally favours static linking with patches to other troublesome libraries like openssl and libz.
That’s why we recommend linking static Linux libraries on Alpine. They should run on any Linux system with the same architecture.
There are probably other options, but this approach has proven to be the easiest one (for now).

1 Like

so rust use the musl too. it is the same challenge for rust developer ?

Yes, these static linking issues are not specific to Crystal.

thanks, buddy. :grin:

Could we link against musl in other systems to achieve static linking?

You mean for example having glibc as the system default and musl additionally installed? I doubt: better to have each static library compiled with the same libc.

Yes, that’s definitely doable. However, the issues with linking statically are not only with libc but also other libraries and as I understand it, Alpine helps by providing libraries that can be statically linked.

But you can simply install musl on say a Ubuntu machine and use musl-gcc for linking statically. In most cases, it should probably work as good as on alpine.

Could we link against musl in other systems to achieve static linking?

It’s nice to have a techical possibility to do something, but the result may be of less utility than anticipated. In other words, why would you do that? My understanding is that release builds should always involve a clean chroot of a target distro (probably docker). In case of a static build there is no target distro, so Alpine or otherwise should not matter at all, but if it’s easy to make a non-obvious mistake, that’s a sign of something that should be at least marked as problematic, even better made possible, but inconvenient on purpose. And by that I mean that other, generally more safe and elegant ways to achieve the desired result should come to mind first.

1 Like

FWIW, building a statically linked crystal program is not bound to Alpine Linux, works great on WSL (Ubuntu)

@girng The point, as I’ve mentioned above, is that it might as well work for some Crystal programs (maybe even for most of them) to use musl-gcc on Ubuntu etc. to link a fully static executable. But chances are it won’t work at some point because some of the libraries are not prepared for that.

Oh, yeah. I just meant in general. I was reading it on the docs page and just wanted to point it out :O

@straight-shoota, Hi buddy, when using the musl compile, have any .so lib be required in somewhere up to now? Cloud you give some scenario ? i make a account of it. :grinning: .

Just tested it compiling a simple program in my Macbook with Linux Alphine via Docker, then uploading and running it on Ubuntu VPS.

Worked like a charm. :partying_face: