Fix rustup failed with "error: linker `cc` not found" on Alpine Linux 3.13 (Rust 1.50)

created
( modified )
@nabbisen

Summary

Today, I made a Dockerfile for Rust development on Alpine Linux. I met the error below when installing Rust with Rustup:

# apk add rustup

Then

# rustup-init -y

The output was:

(...)
error: linker `cc` not found
  |
  = note: No such file or directory (os error 2)
...
error: build failed
The command '/bin/sh -c cargo build' returned a non-zero code: 101

Environment

  • Alpine Linux 3.13
  • Rust 1.50
  • Rustup 1.23

Solution

It was fixed by adding build-base package:

# apk add rustup
# #apk add gcc       # here or
# apk add build-base # here

Then

# rustup-init -y

The output was:

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-musl
info: syncing channel updates for 'stable-x86_64-unknown-linux-musl'
info: latest update on 2021-02-11, rust version 1.50.0 (cb75ad5db 2021-02-10)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-musl'

  stable-x86_64-unknown-linux-musl installed - rustc 1.50.0 (cb75ad5db 2021-02-10)


Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done
automatically.

To configure your current shell, run:
source $HOME/.cargo/env

As to Dockerfile, adding

ENV PATH=#{user-home-abs-path}/.cargo/bin:"$PATH"

after the installation might bring a little more happiness :)

Conclusion

The minimal installation requires, of course, just gcc instead of build-base.

It is, however, not recommended because either musl-dev or libc-dev will be necessary in addition soon after.

In my case, installing Rust only was successful with gcc, but Actix Web, a Rust web framework using actors communication, required build-base.

Series

Rust on Alpine Linux
  1. Fix rustup failed with "error: linker `cc` not found" on Alpine Linux 3.13 (Rust 1.50)
  2. Fix rustup failed with "error: linker `cc` not found" on Alpine Linux 3.17 (Rust 1.66)

Comments or feedbacks are welcomed and appreciated.