The mold is a high-performance, drop-in replacement for traditional Unix linkers like GNU ld and LLVM lld. It achieves significant speed improvements in the linking phase of software builds by optimizing parallelism and memory usage, while maintaining full binary compatibility. Mold integrates seamlessly into existing build systems, requiring no modifications to linker scripts or build configurations. This tutorial explains how to install mold linker on Ubuntu 24.04.
Prepare environment
Make sure you have installed gcc or g++ compiler in the system:
sudo apt install -y gcc
sudo apt install -y g++
Install mold
First, fetch the latest mold release version from GitHub:
MOLD_VERSION=$(curl -s "https://api.github.com/repos/rui314/mold/releases/latest" | grep -Po '"tag_name": "v\K[0-9.]+')
Download the archive with precompiled binaries:
wget -qO mold.tar.gz https://github.com/rui314/mold/releases/latest/download/mold-${MOLD_VERSION}-x86_64-linux.tar.gz
Unpack mold to /opt/mold
directory:
sudo mkdir /opt/mold
sudo tar xf mold.tar.gz --strip-components=1 -C /opt/mold
To use mold system-wide, add /opt/mold
to the PATH environment variable:
echo 'export PATH=$PATH:/opt/mold/bin' | sudo tee -a /etc/profile.d/mold.sh
To apply the changes, either log out and back in, or run the following command to activate them immediately:
source /etc/profile
Verify that mold is installed correctly by checking its version:
mold --version
Remove the downloaded archive:
rm -rf mold.tar.gz
Testing mold
Let's confirm that mold is working correctly by compiling and inspecting a basic C program. Create a new C source file named main.c
:
nano main.c
Paste the following code into the file:
#include <stdio.h>
int main() {
printf("Hello world\n");
return 0;
}
Compile the program using gcc with mold as the linker using -fuse-ld=mold
option:
gcc -fuse-ld=mold main.c -o test
Inspect the resulting binary to verify that mold linker was used:
readelf -p .comment test
You should see a line indicating that the mold was used as the linker, confirming a successful setup:
String dump of section '.comment':
[ 0] GCC: (Ubuntu 13.2.0-23ubuntu4) 13.2.0
[ 27] mold 2.40.2 (e61093dfd61ef00f8bbdd6c997edbd4b1a2bde55; compatible with GNU ld)
Uninstall mold
To completely remove the mold linker, simply delete its installation directory:
sudo rm -rf /opt/mold
Remove the mold.sh
file that was used to configure the environment variable:
sudo rm -rf /etc/profile.d/mold.sh
Leave a Comment
Cancel reply