How to make and install programs from source in Linux

Even though most Linux distributions have extensive package repositories, you can easily install any package from there using a specific * Nix distribution package manager, which automatically handles the compilation of packages and any Required dependencies, sometimes it may still be necessary to compile and install programs from the following versions such as our own source code to fix errors, install new versions of packages that are not yet available on the package repository …

In this tutorial, we will learn how to make and install programs from source code on Linux.

Install required utilities

Before you can compile any program, you need to install the necessary utilities, such as the GCC compiler. Development tools or build necessary tools provide at least such utilities on RHEL-derived tools and Debian-derived tools, respectively. To install such tools, run the following command:

About Debian derivatives:

# apt install build-essential

On RHEL derivatives:

# yum groupinstall "Development Tools"

Extract the source code

In most cases, the software is distributed as a compressed tarball, which is a release of the source code for a specific version. The compressed file contains the source code and build scripts to compile and install the software.

The most common methods for compressing tarballs include gzip, bzip2 Either z. Therefore, the source code may be compressed into pkgname-version.tar.gz, pkgname-version.tar.bz2, Or pkgname-version.tar.xz respectively.
GNU version tar archive utility These methods are supported so that files compressed in such managers can be easily decompressed.

To decompress a gzip-compressed tarball:

# tar zxvf pkgname-version.tar.gz

To decompress a bzip2 compressed tarball:

# tar jxvf pkgname-version.tar.bz2

To decompress an xz compressed tarball:

# tar Jxvf pkgname-version.tar.xz

Alternatively, you can use a specific utility to decompress the compressed source code and then use the GNU tar utility to extract the content. E.g;

To decompress a gzip-compressed tarball:

# gunzip pkgname-version.tar.gz
# gzip -d pkgname-version.tar.gz

For bzip2 compressed tarball:

# bunzip2 pkgname-version.tar.bz2
# bzip2 -d pkgname-version.tar.bz2

For xz compressed tarball:

# unxz pkgname-version.tar.xz
# xz -d pkgname-version.tar.xz

After the extraction is complete, extract the content with tar as follows:

# tar xvf pkgname-version.tar

Building packages from source code

Utilities commonly used to generate configuration / build scripts for software source code packages include Automatic configuration with Made automatically.

Automatic configuration Generate scripts that can adapt packages to different variants of UNIX-like systems without human intervention. It creates a configuration script for a package from a template file that lists the operating system features that the package can use.

Automatic production Utilities are automatically generated Generate file Files that conform to the GNU coding standard.

To build a program from source code, you need to take the source code and unzip it. Before unzipping the package, it is wise to check the contents of the archive to verify that your own directory will be created after extraction. To list the contents of the archive, run the following command.

# tar tvf pkgname-version.tar.*


# tar tvf pkgname-1.2.3.tar.gz
---output truncated---
# tar tvf pkgname-4.5.6.tar.xz

In the first case, it can be extracted into the current directory. In the second case, you need to create a directory and unzip the archive to that directory because this will unzip the contents of the current directory.

Please note that it is important to read the documentation of the problematic package carefully. Readme with installation file.

Configure the installation environment

After extracting and reading the INSTALL and README documents, it’s time to set up an environment for compiling and installing packages. This can be done by using Configuration A script that checks the system for the software required to build the program. It will also check for optional and mandatory dependencies. If an optional dependency is missing, it will disable compilation of that dependency. If the required dependencies are missing, it will print an error and exit.

To configure the package, call Configuration The script is located in the source code directory, as shown in the following example;

# cd pkgname-1.2.3/
# ./configure

The configure script usually accepts parameters that enable you to set compile time
Options for this software. However, these options may be package-specific, so to find out more about that option, run the help command.

# ./configure -h

Compile source code

If the above steps were completed without errors, run the following command to continue compiling the software: Make The command is also in the source code directory.

# make

Any errors encountered during compilation are displayed on the console.

install software

If the compilation step is completed successfully, usually, the software compiled from the source code will be installed in / usr / local And its subdirectories. To install, run the following command;

# make install

This ensures that the binaries required by the software are placed in a standard path so that they can run anywhere on the system.

If the program does not provide the installation target to be installed, and the installer that is running make install does not install the program, you can copy the program binary to the standard path and set the appropriate ownership and permissions. E.g;

# cp pkgname /usr/local/bin/
# chmod +x /usr/local/bin/pkgname

However, you can use the following methods instead of copying the program yourself: Install The program can copy binary files and automatically adjust ownership and permissions. To use the installer, type:

# install pkgname /usr/local/bin

So far you know Basic Information on how to get, extract, configure, compile and install programs from source code.

If you need to uninstall the package, just navigate to the source directory and run Make With command Uninstall The goals are as follows.

# make uninstall

If you have deleted the source directory or uninstall targets are not supported (this is usually not the case), manually delete the program files. You may also need to run Clean up Only delete files generated during compilation without deleting the source code directory.