Compiling & Installing Linux Software From Source


26812994-word-cloud-with-source-code-related-tags

Prerequisites 

No real requirements for this post are needed other a Linux operating system such as the ones listed in step 1 of the guide. A VPS or even client machine with Linux as the operating system would work fine for this post. If you are in need of hosting Hoverdata provide afforable payment plans for shared hosting, VPS hosting, or dedicated server hosting, and are the company behind this blog and its content.

Introduction

At times it is more preferable to acquire and install software or packages through a different means than the OS package manager. This could be due to a number of reasons such as desiring up to date versions of software, no provision of the software in OS repositories, or a wish install the application with less or more custom features.

To do this in Linux/Unix systems a general process referred to as compiling from source is carried out.

Follow these steps to see how to install htop from source as an example.

Step 1 — Build/Compilation Dependencies

To complete this process our system must have the packages used for building software.

Use the relevant download method for your chosen operating system to install these tools.

Ubuntu / Debian

Fedora, CentOS, & RHEL

Arch Linux 

Installed during the base install (base-devel).


Step 2 — Program Dependencies

We are installing htop which requires:

Any unmet dependencies will be listed through output of an error message upon issuing the configure command in step 6.


Step 3 — Active Home Directory

We need somewhere in the file system that’s suitable to retrieve and build software. Your Linux user’s home directory is one of these.

Make the home directory your active directory by typing:

The ~ symbol is assigned to $HOME which in the bash shell environment is a variable containing your current Linux user’s “home” directory path.

You can confirm this worked with this command:

It will output your currently active working directory.


Step 4 — Acquire Source Code Packages

The most routine way of fetching source code files on the command line from a distributor, maintainer, or developer is with a program like “wget” or “curl”. With either of these two commands, files stored at accessible web URL’s are identified then downloaded for use onto the local file-system.

Other commonplace methods involve using a package management system such as Git, Mercurial, NPM, pip, etc to pull files from their online repositories.

Here we’ll show a wget that you can use to download the source code we’ll need for this tutorial.

If you don’t have wget installed you will need to look into installing it from your operating systems respective package manager.


Step 5 — Decompress & Extract 

Now we have the software we need it’s time to decompress it.

In our instance the files we have before us are .tar.gz files. So the command-line program we can use is tar.

In other cases the file formats can be different meaning another extraction program might have to be used in place of tar.

Bear this in mind for future extractions.


Step 6 — Configure

The newly extracted folder in our home directory contains the configure file that directs the make process in compiling the software.

change directory (cd) into the folder and run the configure script we mentioned to prep the files.

Any errors at this stage and any of these latter stages could originate from inadequate user permissions or missing dependencies the program we are building requires.

Go back and check you have all the right dependencies from before in step 1 and 2 whilst making sure your user has the needed permissions for these actions.

Also here with configure files you can pass a prefix (e.g. –disable-unicode) to notify the configure file you want to install the eventual software in a specific manner or with certain conditions.

This is one of several benefits to installing programs from source.


Step 7 — Compile

Next comes the compilation of the pre-configured software using any dependencies from earlier including a C compiler (gcc) to build the code with.


Step 8 — Install

Our final step involves installing the built code from step 7 into any system directories alongside producing the binary file you can run to initiate the program.

Prefixes of varying kinds can be applied here also like we did earlier in step 6


Conclusion 

You can now run the program in this directory with:

Or elsewhere from the command line with:

An additional step that shoud not be required but is sometimes needed is to add the binary file generated to your system’s native program directory, enabling you to execute it from any location on the local command line/file system, but note that this step is not usually necessary.

You can remove the generated files and clean up the system after installing with:

We hope this post proves useful and helps you to understand the processes involved in the installation of software from source.