Linux Basics for Hackers, the Make Command: Compiling and Installing Software from Source in Linux

Cybersecurity Linux

Welcome back, aspiring Linux enthusiasts!

 

In our journey through the Linux ecosystem, we often encounter situations where the software we need isn’t available through standard package managers like apt or yum. Or perhaps we need a bleeding-edge version with features not yet available in the repositories. When this happens, knowing how to compile and install software from source becomes an invaluable skill in your Linux toolkit.

 

Make is a build automation tool that builds executable programs and libraries from source code by reading files called Makefiles. These files specify how to derive the target program from its dependencies. Once you understand this process, you’ll have the freedom to install any open-source software on your Linux system.

 

Some of the key features of the make command include:

 
  1. Dependency Tracking: Make only rebuilds what needs to be rebuilt, saving time during compilation.

  2. Parallel Execution: Modern versions can compile multiple source files simultaneously, utilizing multi-core processors.

  3. Cross-Platform: The make utility works across almost all Unix-like operating systems, making your skills transferable.

  4. Customization: The compilation process can be customized to your specific needs and system configuration.

 

Let’s download and compile a package from source!

 

Step #1 Preparing Your System for Compilation

 

Before we can compile any software, we need to ensure our system has the necessary tools. The compilation process requires a collection of development tools including compilers, linkers, and build utilities.

 

In Debian-based systems like Kali, you can install these tools with:

 

kali > sudo apt-get install build-essential

 
 

These packages include the gcc/g++ compilers and libraries required to compile software from source. Without these tools, you won’t be able to proceed with compilation.

 

Once you have the development tools installed, you’ll need to check if the software you’re trying to compile has any dependencies. When using package managers, dependencies are handled automatically, but when compiling from source, you need to ensure all required libraries are available on your system.

 

For our example, we’ll be compiling curl, a command-line tool for transferring data with URLs. While most systems come with curl pre-installed, compiling it ourselves will demonstrate the process that applies to most software packages.

 

Step #2 Downloading and Extracting the Source Code

 

Let’s download the curl source code:

 
 
 
This command downloads the curl source code and saves it as curl.tar.gz in your current directory.
 

Next, we need to extract the contents of this file:

 

kali > tar -xvzf curl.tar.gz

 
 

The tar command with these options (-xvzf) extracts (-x) the contents of the archive, verbosely (-v) showing the files being extracted, from a gzipped (-z) file (-f).

 

After extraction, you’ll have a new directory containing the source code. Let’s navigate into it:

 

kali > cd curl-8.13.0

 
 

Inside this directory, you’ll find numerous files and subdirectories containing the source code and build instructions for curl.

 

Step #3 Configuring the Build Environment

 

Before compiling, we need to configure the build environment. This is done using the configure script, which is designed to help programs run on a wide variety of different computer systems.

 

The configure script checks your system for required dependencies, sets up the build environment, and creates a Makefile tailored to your specific system. This is a critical step that ensures the software will compile correctly on your particular Linux distribution.

 

Let’s run the configure script:

 

kali > ./configure

 
 

This command runs the configure script in the current directory. The script will perform numerous checks on your system, looking for required libraries and tools, and setting up the build environment accordingly.

 

As you can see, we received an SSL error. We can choose any option from the list, I will use OpenSSL:

 

kali> ./configure –with-openssl

 
 

The configure script essentially matches the libraries required by the program with the ones installed on your system. By doing this, the compiler knows where to look for the libraries required by curl. When it’s done, it generates a file called Makefile with all the necessary information for the compilation process.

 

During this process, you may see an error about a missing library, to continue you just need to install it through apt and repeat the script.

 

The configure script often accepts various options that allow you to customize the build. For example, you might want to install the software in a different location or enable/disable certain features (just like we did with SSL). You can usually see these options by running:

 

kali > ./configure –help

 
 

Step #4 Compiling the Source Code with Make

 

Now that we have configured the build environment and generated the Makefile, we can compile the source code using the make command:

 

kali > make

 
 
The make command reads the instructions in the Makefile and executes the commands necessary to compile the software. This process can take anywhere from a few seconds to several hours, depending on the size and complexity of the software.
 

During compilation, you’ll see a lot of output as the compiler processes each source file. Don’t worry if you don’t understand all of this output – it’s primarily useful for debugging if something goes wrong.

 

The make command is incredibly powerful because it only rebuilds what needs to be rebuilt. If you make changes to the source code and run make again, it will only recompile the files that have changed, saving considerable time during development.

 

Step #5 Installing the Compiled Software

 

After successfully compiling the software, the final step is to install it on your system. This is done using the make install command, which typically needs to be run with root privileges:

 

kali > sudo make install

 
 

This command copies the compiled binaries, libraries, and documentation to their appropriate locations on your system, as specified in the Makefile. The installation locations are typically determined during the configure step.

 

After installation, you should be able to run the software. For curl, you can verify the installation by checking its version:

 

kali > curl -V

 
 

This should display the version of curl you just compiled and installed, along with information about the supported features and protocols.

 

Summary

 

Remember, while package managers offer convenience, knowing how to compile from source gives you flexibility and control that package managers can’t match. It’s an essential skill for any serious Linux user, administrator or hacker.

 

To learn more about Linux check out Linux Basics for Hackers training.