# How to statically compile to a binary

**URL:** <https://discourse.julialang.org/t/how-to-statically-compile-to-a-binary/23825>\
**Category:** New to Julia\
**Created:** [May 3, 2019, 5:50pm UTC](https://discourse.julialang.org/t/how-to-statically-compile-to-a-binary/23825 "2019-05-03T17:50:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![certik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/certik/32/2651_2.png) [@certik](https://discourse.julialang.org/u/certik)\
**Post date:** [May 3, 2019, 5:50pm UTC](https://discourse.julialang.org/t/how-to-statically-compile-to-a-binary/23825/1 "2019-05-03T17:50:16Z")

</div>

The front page [https://julialang.org](https://julialang.org) says at the “General Purpose” tab that you can “[statically compile](https://github.com/JuliaLang/PackageCompiler.jl) your code”.

So I have installed `PackageCompiler` as I described [here](https://discourse.julialang.org/t/how-to-install-packagecompiler/23824).

I am able to create a _dynamic_ binary as follows:

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

```

when you examine it:

```julia
$ ldd builddir/hello
	linux-vdso.so.1 (0x00007ffc27946000)
	hello.so => /home/certik/repos/julia_comp/builddir/hello.so (0x0000151ddd2c7000)
	libjulia.so.1 => /home/certik/ext/julia-1.1.0/lib/libjulia.so.1 (0x0000151ddcb57000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x0000151ddc766000)
	libLLVM-6.0.so => /home/certik/ext/julia-1.1.0/lib/julia/libLLVM-6.0.so (0x0000151dd9a40000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x0000151dd983c000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x0000151dd9634000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x0000151dd9415000)
	libstdc++.so.6 => /home/certik/ext/julia-1.1.0/lib/julia/libstdc++.so.6 (0x0000151dd9097000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x0000151dd8cf9000)
	libgcc_s.so.1 => /home/certik/ext/julia-1.1.0/lib/julia/libgcc_s.so.1 (0x0000151dd8ae1000)
	/lib64/ld-linux-x86-64.so.2 (0x0000151de4c7b000)

```

it links to Julia and to `hello.so`:

```julia
$ ldd builddir/hello.so 
	linux-vdso.so.1 (0x00007ffc48392000)
	libjulia.so.1 => /home/certik/ext/julia-1.1.0/lib/libjulia.so.1 (0x000014d4bc419000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x000014d4bc028000)
	libLLVM-6.0.so => /home/certik/ext/julia-1.1.0/lib/julia/libLLVM-6.0.so (0x000014d4b9302000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x000014d4b90fe000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x000014d4b8ef6000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x000014d4b8cd7000)
	libstdc++.so.6 => /home/certik/ext/julia-1.1.0/lib/julia/libstdc++.so.6 (0x000014d4b8959000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x000014d4b85bb000)
	libgcc_s.so.1 => /home/certik/ext/julia-1.1.0/lib/julia/libgcc_s.so.1 (0x000014d4b83a3000)
	/lib64/ld-linux-x86-64.so.2 (0x000014d4c433a000)

```

Which also links to Julia and LLVM (`libjulia.so.1` and `libLLVM-6.0.so`).

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

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

```julia
$ ll -h builddir/hello.so 
-rwxr-xr-x 1 certik certik 131M May 3 11:18 builddir/hello.so*

```

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

```julia
$ 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.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [May 3, 2019, 6:10pm UTC](https://discourse.julialang.org/t/how-to-statically-compile-to-a-binary/23825/2 "2019-05-03T18:10:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 3, 2019, 6:22pm UTC](https://discourse.julialang.org/t/how-to-statically-compile-to-a-binary/23825/3 "2019-05-03T18:22:24Z")

</div>

Not the most helpful comment, but I wonder if it would make sense for [julialang.org](http://julialang.org) 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`.

---

<div class="post-metadata">

**Author:** ![certik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/certik/32/2651_2.png) [@certik](https://discourse.julialang.org/u/certik)\
**Post date:** [May 3, 2019, 7:42pm UTC](https://discourse.julialang.org/t/how-to-statically-compile-to-a-binary/23825/4 "2019-05-03T19:42:00Z")

</div>

@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”?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 3, 2019, 8:05pm UTC](https://discourse.julialang.org/t/how-to-statically-compile-to-a-binary/23825/5 "2019-05-03T20:05:33Z")

</div>

> [@certik](#):
>
> 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”?

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.)

---

<div class="post-metadata">

**Author:** ![certik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/certik/32/2651_2.png) [@certik](https://discourse.julialang.org/u/certik)\
**Post date:** [May 3, 2019, 8:12pm UTC](https://discourse.julialang.org/t/how-to-statically-compile-to-a-binary/23825/6 "2019-05-03T20:12:44Z")

</div>

Thanks @ExpandingMan, I appreciate your answer!
