Most Linux packages are free and open-source, giving you the freedom to customize or modify any piece of software to your own liking. Additionally, you are also free to look at the source code of Linux packages to learn good architecture practices and coding patterns from other software projects.
Let’s explore how you can compile and install a package from source on Linux.
Step 1: Installing the Required Tools
Linux provides you with all the necessary tools required to compile, build, and install software from the source code.
Most Linux software is written in the C or C++ programming languages, therefore, you’ll need a C or C++ compiler. For example, the GNU Compiler Collection (GCC) and CMake for building your package.
Besides that, you’ll need other packages such as curl and gettext. Depending on your Linux distro, you can install the required tools in a single command as follows.
On Debian-based distros such as Ubuntu:
On Arch Linux and its derivatives:
On RPM-based distros such as Fedora, RHEL, etc:
Learn More: How to Manage Software Packages With DNF
Step 2: Downloading the Package Source Code
For this guide, we’ll be installing the Git package from the source. We’ve chosen Git because it is widely used among software engineers and developers.
Most packages you can compile can be found on the official website of the package in question. You can download the source code files using the curl command-line tool. Alternatively, you can use wget or the GUI.
Download the source code into the Downloads folder on your PC, then, switch to the Downloads directory using the cd command.
Once you’re in the Downloads folder, you can download the Git source code using curl as follows. In this guide, we’ll download the Git version 2.26.2 but feel free to choose any version.
The curl command specifies that it should place the source code in a zipped file named git.tar.gz.
Download: Git Source Code for Linux
In most cases, the source code will be packaged in a compressed folder to make downloading easier and for better organization of the source code files.
To extract the content of the zipped file, you can use the tar command.
Step 3: Compiling the Source Code
Next, go to the newly extracted folder. In this case, the name will be “git-2.26.2,” of course, the folder name will be different if you have downloaded a different version of Git.
It is always a good idea to take a look at the README.md or INSTALL files because they contain valuable information on how to compile and install the package. These files are usually located in the root folder of the source code.
Another important file is the configure script. It checks for software dependencies for the package you want to compile, and you’ll see an error message if the script finds missing dependencies.
Configure and prepare your source code by executing the script. The command will create make files and configurations for the software that you are about to compile and install.
Step 4: Building the Software Package
Now that the source code is configured and compiled, you can build the software as follows:
The make command uses the Makefile, which contains necessary instructions on how to build the software package.
The compilation process will take some time depending on your computer’s processing power, and the size of the package.
Step 5: Installing the Software Package
If you’ve come this far, congratulations, you’ve successfully compiled and built Linux software from source code.
In this last step, you’ll install the Git software package you’ve just built from source code. This command installs the newly compiled package by copying the build files to the correct locations on your PC.
Check the version of Git you just installed with the command:
The output should be similar to the one below. The version number may vary depending on the package that you downloaded.
Alternative Methods of Installing Software on Linux
This guide has looked at how to compile and build software from source on Linux using Git as a study case. Installing software from source code gives you so much freedom to customize the software to your liking which is an amazing thing.
Most Linux distros provide you with many options when installing software. For example, on Arch Linux, you can use Pacman and Yay package managers.