<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Abhishek Pareek</title>
    <description>The latest articles on Forem by Abhishek Pareek (@abhishekpareek).</description>
    <link>https://forem.com/abhishekpareek</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1280078%2Fb99d34b1-fda4-4781-bb81-7997b575b1df.jpg</url>
      <title>Forem: Abhishek Pareek</title>
      <link>https://forem.com/abhishekpareek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/abhishekpareek"/>
    <language>en</language>
    <item>
      <title>Build statically linked Rust binary with musl (and avoid a common pitfall)</title>
      <dc:creator>Abhishek Pareek</dc:creator>
      <pubDate>Tue, 13 Feb 2024 19:25:17 +0000</pubDate>
      <link>https://forem.com/abhishekpareek/build-statically-linked-rust-binary-with-musl-and-avoid-a-common-pitfall-ahc</link>
      <guid>https://forem.com/abhishekpareek/build-statically-linked-rust-binary-with-musl-and-avoid-a-common-pitfall-ahc</guid>
      <description>&lt;h4&gt;
  
  
  Introduction
&lt;/h4&gt;

&lt;p&gt;I was required to produce a x86-64 statically linked Rust binary for a containerized workload at work. This guide briefly outlines the process of doing so using &lt;strong&gt;musl&lt;/strong&gt;, a lightweight C library. Unlike traditional compilation with &lt;strong&gt;glibc&lt;/strong&gt;, musl eliminates dynamic dependencies, resulting in a single, external dependency-free executable.&lt;/p&gt;

&lt;h4&gt;
  
  
  A quick word on why &lt;strong&gt;musl&lt;/strong&gt; is preferred over &lt;strong&gt;glibc&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Even if a program is statically linked with glibc, it still requires to dynamically load the c library in order to satisfy loading of some NSS and iconv modules. &lt;a href="https://stackoverflow.com/a/57478728" class="ltag_cta ltag_cta--branded"&gt;ref&lt;/a&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Steps
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Create a new cargo project
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  $ cargo new --bin static-rust-binary
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Build the default binary
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  $ cd static-rust-binary
  $ cargo build 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Check it is dynamically linked
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  $ file ./target/debug/static-rust-binary 
./target/debug/static-rust-binary: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=774a033de72094a3ea8a7ac99261b57a86603173, for GNU/Linux 3.2.0, with debug_info, not stripped
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Change build target to musl and re-compile
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  $ cargo build --release --target=x86_64-unknown-linux-musl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Check if the binary is now statically linked
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  $ file ./target/x86_64-unknown-linux-musl/release/static-rust-binary 
./target/x86_64-unknown-linux-musl/release/static-rust-binary: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), static-pie linked, BuildID[sha1]=e3880597fbf7adcb623d246b69c52d9aee6fe867, with debug_info, not stripped
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🎩✨ Voilà! ✨🎩 &lt;br&gt;
The statically linked rust binary is ready which can now be put in a scratch container to be run on #docker #kubernetes etc.&lt;/p&gt;


&lt;h4&gt;
  
  
  ‼️ Attention! ‼️
&lt;/h4&gt;

&lt;p&gt;In my experience I've run into trouble generating a statically linked binary if the project dependencies have implicitly or explicitly included &lt;code&gt;openssl-sys&lt;/code&gt; crate. For example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo build --release --target=x86_64-unknown-linux-musl
   ...
   Compiling openssl v0.10.63
   ...
   Compiling openssl-sys v0.9.99
error: failed to run custom build command for `openssl-sys v0.9.99`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case the simplest way I've been able to fix the problem is to explicitly add the openssl crate (latest version) with the features = &lt;code&gt;vendored&lt;/code&gt; conditional dependency to the &lt;code&gt;Cargo.toml&lt;/code&gt; (Note: as of today "v0.10.63" is the latest version of openssl crate).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[dependencies]
...
openssl = { version = "0.10.63", features = ["vendored"] }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Re-running the build compiles successfully.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo build --release --target=x86_64-unknown-linux-musl
   Compiling openssl v0.10.63
   ...
   Compiling openssl-sys v0.9.99
   ...
   Compiling static-rust-binary v0.1.0 (/home/abhishek.pareek/src/rust/simple-static-rust-binary)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🤞 Hopeful this will help someone get unstuck in the future! 🌟 And don't hesitate to comment if this approach worked or not! 🚀&lt;/p&gt;

</description>
      <category>rust</category>
      <category>musl</category>
      <category>linux</category>
      <category>openssl</category>
    </item>
  </channel>
</rss>
