Install musl-gcc on Ubuntu 24.04

Install musl-gcc on Ubuntu 24.04

The musl-gcc is a wrapper script around gcc compiler that tells it to link programs against the musl libc instead of the default glibc. It's often used to build statically linked, lightweight, and portable binaries. This tutorial explains how to install musl-gcc on Ubuntu 24.04.

Prepare environment

Make sure you have installed gcc compiler in the system:

sudo apt install -y gcc

Install musl-gcc

Update package lists to ensure you have the latest information:

sudo apt update

Install the musl-tools package, which provides musl-gcc:

sudo apt install -y musl-tools

Verify the installation:

musl-gcc --version

Testing musl-gcc

Create a main.c file:

nano main.c

Paste the following code:

#include <stdio.h>

int main() {
    printf("Hello world\n");

    return 0;
}

Compile using musl-gcc (dynamic link by default):

musl-gcc main.c -o test

While you can use musl libc for dynamic linking, the most common and practical use case is to create statically linked binaries, which are self-contained and portable across different Linux systems:

musl-gcc -static main.c -o test

Run the compiled binary:

./test

Uninstall musl-gcc

To completely remove musl-gcc and its related dependencies, use the following command:

sudo apt purge --autoremove -y musl-tools

Leave a Comment

Cancel reply

Your email address will not be published.