How to statically compile to a binary

The front page says at the “General Purpose” tab that you can “statically compile your code”.

So I have installed PackageCompiler as I described here.

I am able to create a dynamic binary as follows:

julia ~/.julia/packages/PackageCompiler/oT98U/juliac.jl -vae --compile=all hello.jl

when you examine it:

$ ldd builddir/hello (0x00007ffc27946000) => /home/certik/repos/julia_comp/builddir/ (0x0000151ddd2c7000) => /home/certik/ext/julia-1.1.0/lib/ (0x0000151ddcb57000) => /lib/x86_64-linux-gnu/ (0x0000151ddc766000) => /home/certik/ext/julia-1.1.0/lib/julia/ (0x0000151dd9a40000) => /lib/x86_64-linux-gnu/ (0x0000151dd983c000) => /lib/x86_64-linux-gnu/ (0x0000151dd9634000) => /lib/x86_64-linux-gnu/ (0x0000151dd9415000) => /home/certik/ext/julia-1.1.0/lib/julia/ (0x0000151dd9097000) => /lib/x86_64-linux-gnu/ (0x0000151dd8cf9000) => /home/certik/ext/julia-1.1.0/lib/julia/ (0x0000151dd8ae1000)
	/lib64/ (0x0000151de4c7b000)

it links to Julia and to

$ ldd builddir/ (0x00007ffc48392000) => /home/certik/ext/julia-1.1.0/lib/ (0x000014d4bc419000) => /lib/x86_64-linux-gnu/ (0x000014d4bc028000) => /home/certik/ext/julia-1.1.0/lib/julia/ (0x000014d4b9302000) => /lib/x86_64-linux-gnu/ (0x000014d4b90fe000) => /lib/x86_64-linux-gnu/ (0x000014d4b8ef6000) => /lib/x86_64-linux-gnu/ (0x000014d4b8cd7000) => /home/certik/ext/julia-1.1.0/lib/julia/ (0x000014d4b8959000) => /lib/x86_64-linux-gnu/ (0x000014d4b85bb000) => /home/certik/ext/julia-1.1.0/lib/julia/ (0x000014d4b83a3000)
	/lib64/ (0x000014d4c433a000)

Which also links to Julia and LLVM ( and

Question 1: How do I create a static binary? (Or are static binaries not supported yet?)

Question 2: Why is the library so large (131MB)? (Is there a way to make it smaller?)

$ ll -h builddir/ 
-rwxr-xr-x 1 certik certik 131M May  3 11:18 builddir/*

Question 3: Why does the compilation of this simple script take 2min 38s ? (Is there a way to make it compile faster?)

$ time julia ~/.julia/packages/PackageCompiler/oT98U/juliac.jl -vae --compile=all hello.jl
real	2m38.828s
user	2m35.881s
sys	0m6.016s

Thanks for any feedback on this.


I am also looking for a way to compile a Julia code and “bundle” everything necessary inside the executable. I know it will be probably very big (hundreds of MiB), but it will be very good for my use case.


Not the most helpful comment, but I wonder if it would make sense for not to emphasize static compilation until PackageCompiler is significantly more mature. I must confess, I’ve also found static compilation to be quite an effort in the current state. The progress has been great, but, unless I’m missing something, this still has a way to go before static compilation is a smooth process.

The last I spent any time with PackageCompiler, I found compile_incremental to be far more helpful and functional than anything else. It might be worth considering whether that approach could work for you before resorting to compiling static binaries or even using compile_package.


@Ronis_BR, @ExpandingMan thanks a lot for the feedback. Do you think it would be fair to say about Julia that “Ahead-of-time compilation to static executables is not yet as easy as with C++ or Fortran”?

1 Like

That’s definitely true. C++ and Fortran were designed specifically for compilation into static binaries, Julia’s compiler on the other hand is rather novel. I like to think of it as an ahead-of-time compiler that gets called “on demand”. This has all sorts of huge advantages for writing generic code, but the disadvantage is that it is significantly harder to determine what exactly should be incorporated in a statically compiled binary. There’s no fundamental reason why this can’t be done, and lots of great progress has been made, but it will probably take a bit of time for the process to be really robust.

(Bear in mind the disclamer that I’ve made absolutely zero contribution to PackageCompiler myself, so people who work on that package and actually know what they’re talking about may be either more optimistic or more pessimistic.)


Thanks @ExpandingMan, I appreciate your answer!