Executable wont run on computer without libraries installed

This is what I run:

crystal build src/test.cr --release --no-debug;

When I open it on my machine, it runs fine, but when I open the file on another mac, it says:

dyld: Library not loaded: @rpath/libui.A.dylib
Referenced from: /Users/computername/Desktop/test
Reason: image not found
Abort trap: 6

I am using this shard (https://github.com/hedron-crystal/hedron), and it is basically a way to utilize libui (https://github.com/andlabs/libui) with crystal. I have zero experience with C and static/dynamic libraries. I always assumed when the crystal program was compiled, it also compiled the C library in with it. Is there any way I can get crystal to build the C library in my /usr/local/lib file in with the compiled crystal executable?

You can build statically linked programs in Crystal but now it only works on Alpine Linux.

You can install Docker for Mac and use https://hub.docker.com/r/jrei/crystal-alpine for building statically linked apps.

Basically in addition to Crystal itself you install all required libraries in Docker https://github.com/j8r/dockerfiles/blob/master/crystal-alpine/Dockerfile#L6-L22

Then use crystal build --static ... to build statically linked application. After that you can copy your single binary to another Docker container with Alpine Linux but without Crystal and any mentioned libraries installed and it will work.

And you’ll also need this workaround https://github.com/j8r/dockerfiles/tree/master/crystal-alpine#usage


This ain’t work for macOS, unfortunately.

1 Like

Can you build “libui” static only and link against that instead?

1 Like

So does this mean I can install the statically compiled program on any computer even without docker?

You can do it on any computer with Alpine Linux.

A statically linked Linux binary should run on any Linux system, not limited to Alpine.