Compiling & Installing Software from Source on Linux: An A-Z Guide

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

Introduction: When Do You Need to “Build” Software Yourself?

On Linux, the most common way to install software is through package managers like apt (Debian/Ubuntu) or dnf (Fedora/RHEL). They are incredibly convenient, fast, and operate smoothly. However, there will be times when you need the absolute latest software version, which hasn’t yet been updated in the package repositories, or you want to customize special features that pre-built versions don’t offer. In such cases, compiling software from source code is the solution.

Compiling from source code not only gives you the exact software version you want but also offers a great opportunity to deeply understand how applications work. You’ll see how they are ‘built’ from millions of lines of code.

Core Concepts: The Foundation of Software “Building”

What is Source Code?

Simply put, source code is the ‘blueprint’ or ‘recipe’ for a piece of software. It’s a collection of text files containing commands (written in C, C++, Python, etc.) created by programmers. Note that computers cannot directly run this source code.

Compiler – Your “Architect”

For the computer to “understand” and execute source code, we need a special tool: a compiler. A compiler’s job is to read the source code, then translate it into machine language (or binary code) that the CPU can directly process. On Linux, GCC (GNU Compiler Collection) is the most powerful and common “architect,” almost the gold standard.

Build System – The Project “Foreman”

Imagine a massive software project with hundreds, or even thousands, of source code files. If you had to compile each file individually, the process would be extremely time-consuming and prone to errors. That’s when we need a professional “foreman” – a Build System. It automates the entire compilation process, connecting disparate components to create a complete application.

  • Make and Makefile: Make is an extremely popular tool. It reads the Makefile (which contains compilation rules) to perform the necessary steps.
  • Autotools (Autoconf, Automake): A collection of tools that help generate Makefiles suitable for various Linux environments. You often encounter them through scripts like configure.
  • CMake: A more modern build system, often generating Makefiles or project files for various IDEs.

Dependencies – Indispensable “Materials”

In the software world, few applications are “standalone.” Most require support from other libraries or tools to function – this is what we call dependencies. For example, an image editing software might need libraries to read and write formats like JPEG, PNG. A web application, in turn, depends on database connection libraries. You will frequently encounter compilation errors if any dependency is missing.

Detailed Practice: Getting Started with Compilation

Step 1: Prepare the Environment – Install Your “Toolbox”

Before you start “building,” ensure your system is equipped with all the necessary “tools” for the compilation process. Linux distributions often provide meta-packages containing compilers, make utilities, and many other tools:

On Debian/Ubuntu:

sudo apt update
sudo apt install build-essential

The build-essential package will install GCC, G++, make, and other fundamental development tools.

On Fedora/RHEL/CentOS:

sudo dnf groupinstall "Development Tools"

This command will install a group of essential development tools.

Step 2: Find and Download the Source Code

Source code is typically found on the project’s official website, platforms like GitHub, GitLab, or other open-source repositories. It’s usually packaged as .tar.gz, .tar.bz2, .zip files, or you can download it directly by cloning from a Git repository.

# Download a compressed file (e.g., htop)
wget https://github.com/htop-dev/htop/releases/download/3.2.2/htop-3.2.2.tar.gz
# Extract the archive
tar -xvzf htop-3.2.2.tar.gz
# Move into the source code directory
cd htop-3.2.2

Or with Git:

git clone https://github.com/someproject/someproject.git
cd someproject

After extraction, it is extremely important to take the time to carefully read files like README, INSTALL, or COPYING. These are the “guiding lights” containing all essential information about compilation, system requirements, and license terms.

Step 3: Resolve Dependencies – “Laying the Foundation”

This can be considered the most “challenging” step for many. When you run the configure script (which we’ll explore in more detail later), it performs a “system health check,” verifying if you have all the necessary libraries and tools. If anything is missing, it will immediately report an error, and you’ll need to install the additions.

Personal experience: When I first started as a sysadmin, I once spent an entire afternoon debugging this issue simply because I didn’t carefully read the log output from the configure command. It clearly stated which library was missing, but I was fumbling around looking for errors elsewhere. The lesson learned is: always carefully read error messages, especially from the configure script!

Essentially, dependencies consist of two types: runtime libraries and development libraries. When compiling, you only need the development packages, which are often easily identified by the -dev suffix (on Debian/Ubuntu) or -devel (on Fedora/RHEL).

For example, if configure reports a missing libssl, you might need to install:

# On Debian/Ubuntu
sudo apt install libssl-dev

# On Fedora/RHEL
sudo dnf install openssl-devel

This process may need to be repeated several times until all dependencies are satisfied.

Step 4: Configure and Compile

With your “toolbox” and “materials” ready, it’s now time to start “building” the application.

Run the configure script

This script (often named configure) is the crucial “preparer.” It automatically checks the environment, locates necessary libraries and tools, and then generates a Makefile tailored to your system. You can also add parameters to customize the compilation process as desired.

# Run basic configure
./configure

# With advanced options, e.g., install to /usr/local
./configure --prefix=/usr/local --enable-feature-x --disable-feature-y

The --prefix=/usr/local parameter is one of the most common options. It specifies the directory where the software will be installed after compilation. By default, applications are often installed in /usr/local or /usr. To discover other options, don’t forget to type ./configure --help.

Compile with make

If configure succeeds and generates a Makefile, you simply issue the make command:

make

This process will “transform” the source code into executable files. For small projects, it only takes a few minutes; but for massive software, it can last tens of minutes, or even several hours, depending on your machine’s CPU and RAM configuration. If you have multiple CPU cores, you can significantly speed up the process by using -j:

make -j$(nproc) # Use all available cores

Step 5: Install

After compilation is complete and error-free, the final step is to install the software onto your system.

sudo make install

This command will copy the executable files, libraries, configuration files, and documentation into the directories specified by --prefix (or the default). You need sudo privileges because this process usually installs into system directories like /usr/local/bin, /usr/local/lib.

Real-World Example: Installing htop from Source

htop is an incredibly useful process monitoring tool that every sysadmin loves. Suppose you want the latest version that isn’t yet in the package repositories:

# Step 1: Prepare the environment (already done above)
# sudo apt install build-essential libncursesw5-dev

# Step 2: Download and extract the source code
wget https://github.com/htop-dev/htop/releases/download/3.2.2/htop-3.2.2.tar.gz
tar -xvzf htop-3.2.2.tar.gz
cd htop-3.2.2

# Step 3: Check and install Dependencies
# htop needs libncursesw5-dev (or ncurses-devel). First, run configure to see what errors it reports.
./configure

# If ncurses is reported missing, install as follows:
# sudo apt install libncursesw5-dev # Debian/Ubuntu
# sudo dnf install ncurses-devel # Fedora/RHEL

# After installing enough dependencies, re-run configure with the desired prefix
./configure --prefix=/usr/local

# Step 4: Compile
make -j$(nproc)

# Step 5: Install
sudo make install

# Verify the newly installed htop version to ensure success
/usr/local/bin/htop --version

Excellent! Now you can run htop from anywhere in your terminal!

Uninstalling Software Compiled from Source

Uninstalling software installed from source is much more complex than using a package manager. If you still have the source code directory and Makefile, try this method:

cd htop-3.2.2 # Go back to the source code directory
sudo make uninstall

However, not all projects perfectly support make uninstall. In cases where it’s not supported, you will have to manually find and delete the installed files. That’s why using --prefix=/usr/local or another custom directory is important. It helps you more easily control and manage these files later on.

Conclusion

Compiling and installing software from source on Linux might seem “daunting” at first glance, but once you master the basic steps and understand dependencies, you’ll find it less intimidating. This skill not only opens the door to the latest software versions and high customization capabilities but also deepens your understanding of how the Linux system operates. Don’t hesitate to give it a try, and remember, carefully reading error logs will save you a lot of time and effort!

Share: