How to Compile Source Code and Install software in Linux

One of the main benefits of open-source software is that you can get the source code of the applications. This makes it possible to do audits of them and also to be able to compile and install software using the source code. Is it hard? Is it worth it? Let’s see.

What is the source code of an application?

The source code is a file or set of files, containing concrete instructions, written in a programming language, that later compile one or several programs.

The source code of a program is written in a specific programming language. The detail is that this type of language cannot be executed directly by the computer.

So, what is done? Well, it must be translated into another language that the computer can run without problems. For this translation, are used the so-called compilers, assemblers, interpreters, among others.

In summary, the source code is the instructions of how the program was made and this is compiled originating a binary that the system can execute.

On Linux, you can install applications by compiling their source code by yourself, this, although it slows down the process a little, has its advantages.

Is it worth compiling and installing software on Linux using source code?

The short answer is maybe. It depends on the application and the level of user that is involved.

An example of this is what happens with certain applications that are not present in the official distribution repositories. A great way to get them is to compile the source code.

Another particularly important use case is when we need the latest stable version of a package. A good way is to get the source code and compile it ourselves.

But it happens that sometimes the compile times are high, and many users prefer to skip this and just install the precompiled package.

In addition, many times it is difficult to satisfy all the dependencies of the application in our system. This makes it unfeasible.

You could say that the main advantage of compiling and installing software using the source code is that when you download, unzip and compile the source code of a program on your own, it is compiled with the specific instructions of your processor and system. This affects the performance and usability of the program.

However, there are factors to consider such as time, ease of use and necessity.

How to compile and install software on Linux using Source code

The process of installing software from source code is varied, as each application has its dependencies and processes. However, it is possible to extract generalities that will help to understand the process.

Step 1: Install all the compiling and package building tools

Each operating system has many packages that are applications that help the process of package creation and of course compilation.

These tools are straightforward to install on a modern Linux operating system and contain many packages. This is supposed to be the first step to achieve the goal.

On Debian, Ubuntu, Linux Mint, Elementary OS and family members, you will have to in a terminal run:

sudo apt install build-essential

In the case of RHEL, CentOS, Fedora, and members such as Rocky Linux or Alma Linux, just run:

sudo dnf groupinstall "Development Tools" "Development Libraries".

If you use openSUSE, you have to run

sudo zypper install -t pattern devel_basis

Finally, if you use Arch Linux, Manjaro or any derivative of these:

sudo pacman -Sy base-devel

This way you will have at least the basic system tools.

Step 2: Download the source code of the application

Before starting, you have to download the source code of the application from the application’s website, from a special repository or from sites like GitHub or GitLab where the source code is hosted.

For downloading, you can use a common web browser or do it from the terminal with commands like wget or curl, but in short it depends on where the program is distributed from.

Usually, the source code of applications is distributed in the form of compressed files. So to decompress it, you will need tools like tar, unrar or unzip. Again, the format depends on the developer

Step 3: Install the necessary dependencies for compilation

Each program or library requires a series of dependencies that you have to fulfill in order for the whole process to be successful. However, they are very diverse and depend on each application.

For example, an application developed in PHP does not require the same as one developed in C++ and so on in each language and application.

So, how to know which are the necessary dependencies? Well, you have to consult the official documentation of the application as well as the README or INSTALL files inside the source code folder, which contain instructions and information about it.

One aspect to keep in mind is that the README and INSTALL names may vary according to each application and developer, but in the documentation or on the tool’s website you should get useful information.

Step 4: Configure the compilation

The next step, after having the source code and installing the required dependencies, is to configure the compilation.

Inside the folder where the source code of the application is hosted will be an executable file called configure.

Generally, the configure file is a shell script (generated) and is used to detect certain machine configurations and configure the necessary files so that the compilation can be performed.

Then, the configure file must be executed to prepare the compilation. The most classical usage is like this:

./configure

One of the most common configure options is --prefix. Thanks to it, you will be able to define the compilation and installation directory of the application. For example:

./configure --prefix=[path]

The main advantage of this is that once the application is installed, if you want to delete it, it would be enough to delete the configured directory.

Step 5: Compile the source code

Now you can effectively compile the code. To achieve this, the make command is used.

The classic use of this command is that after configuration, you use it like this:

make

One thing to keep in mind is that current processors have multiple cores. So using the -j option you can specify the number of cores to use.

For example:

make -j8

The above command states that a compilation will be done using 8 processor cores. Obviously, you have to be careful with it and do not impair other system tasks.

Step 6: compile and install software on Linux using Source code

Now the code is compiled. You can install it without too much trouble.

To complete this, use the make command but add the install subcommand

make install

If you have chosen an installation directory whose writing and modifying requires root permissions, you will have to add sudo

sudo make install

This will install the tool on the system.

Compile and install software on Linux – Conclusion

The process of compile and install software on Linux using source code is conditioned by the complexity of the tool. However, as a general rule, it is simple to do and only requires to have the dependencies covered.

As you have noticed, the process is worth it because it installs a clean application with optimized features for the processor and system.

I hope you liked this post and help to spread it.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top