# Force \`shards build\` to use musl on Ubuntu?

**URL:** <https://forum.crystal-lang.org/t/force-shards-build-to-use-musl-on-ubuntu/6894>\
**Category:** Help & Support\
**Created:** [June 4, 2024, 10:17am UTC](https://forum.crystal-lang.org/t/force-shards-build-to-use-musl-on-ubuntu/6894 "2024-06-04T10:17:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jochen](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jochen/32/3490_2.png) [@Jochen](https://forum.crystal-lang.org/u/Jochen)\
**Post date:** [June 4, 2024, 10:17am UTC](https://forum.crystal-lang.org/t/force-shards-build-to-use-musl-on-ubuntu/6894/1 "2024-06-04T10:17:23Z")

</div>

Is there a way to force `shards build` to use musl on an Ubuntu system in one go, or do I need to cross compile and link in a separate step?

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [June 4, 2024, 11:20am UTC](https://forum.crystal-lang.org/t/force-shards-build-to-use-musl-on-ubuntu/6894/2 "2024-06-04T11:20:27Z")

</div>

Neither `shards` nor `crystal` have much to do for this, actually. You can tell it to target a musl environment via the `--target` option. That’s it.  
The rest is up to the linker. It needs to be able to link to musl libc and have the required libraries available.  
Ubuntu’s default cannot do that. So you need to use one that does.  
I can’t give much advice on that. However I believe `zig cc` should be able to do that. And I’ve successfully linked a Crystal program against musl from a glib distro in an environment managed by nix. There might be other options that I’m not aware of.

Of course you could also link (or compile and link) in an environment that is based on musl libc, e.g. run AlpineLinux in a container or chroot.

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [June 4, 2024, 1:23pm UTC](https://forum.crystal-lang.org/t/force-shards-build-to-use-musl-on-ubuntu/6894/3 "2024-06-04T13:23:02Z")

</div>

Check this.

> <https://github.com/zw963/crystal-book/blob/patch-1/docs/guides/use_zig_cc_as_an_alternative_linker.md>

---

<div class="post-metadata">

**Author:** ![ysbaddaden](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ysbaddaden/32/2252_2.png) [@ysbaddaden](https://forum.crystal-lang.org/u/ysbaddaden)\
**Post date:** [June 14, 2024, 12:36pm UTC](https://forum.crystal-lang.org/t/force-shards-build-to-use-musl-on-ubuntu/6894/4 "2024-06-14T12:36:24Z")

</div>

You need a musl-libc toolchain such as [https://musl.cc/](https://musl.cc/) or `zig cc` for example, but you’ll have to compile every required library using this toolchain (bdwgc, pcre2, libevent2, …), then compile a binary with a command such as (you’ll need to specify some `-Lpath` as link flags):

```console
$ CC=/path/to/toolchain/cc crystal build \
  --target=x86_64-linux-musl \
  --link-flags="..."

```

If you’re already on Ubuntu it might be simpler to compile a static executable in a Docker image (e.g. either the official `alpine` or the `crystallang/crystal:alpine` images) directly.
