Compile and Install Programs from Source Code in Linux

|
Last Updated:
|
|
Compile and Install Programs from Source Code in Linux

In this tutorial, we are going to learn how to compile and install programs from source code in Linux. Even though most Linux distributions have extensive package repositories from where package can be installed so easily using a specific OS distribution package manager that handles the package compilation and any required dependency automatically, sometimes there may arise a need to compile and install a program from a source code ourselves for instance to fix a bug, install a new version of a package that is not available yet on the package repositories…

Install Programs from Source Code in Linux

Install Required Utilities/Build tools

Before any program can be compiled, there are necessary utilities/packages/tools that need to be installed such as the GCC compilers. Such utilities are provided by the Development Tools or build-essentials at least on RHEL derivatives and Debian derivatives respectively.

To install such tools, run the following commands:

On Debian Derivatives:

apt install build-essential

On RHEL Derivatives:

yum groupinstall "Development Tools"

There might be other additions libraries/packages or tools required by specific packages for compilation. Consult the application documentation on the same.

Unpacking Source Code Archive

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

The most common methods used for compressing the tarballs include;

  • gzip
  • bzip2
  • xz

So the source code may come compressed as pkgname-version.tar.gz, pkgname-version.tar.bz2, or pkgname-version.tar.xz e.t.c

The GNU version of the tar archiving utility supports these methods and thus make it easy to unpack files compressed in such a manager.

To unpack a gzip compressed tarball:

tar zxvf pkgname-version.tar.gz

To unpack a bzip2 compressed tarball:

tar jxvf pkgname-version.tar.bz2

To unpack an xz compressed tarball:

tar Jxvf pkgname-version.tar.xz

Alternatively, the compressed source code can be decompressed using specific utilities after which the contents are extracted using the GNU tar utility.

For example;

To decompress a gzip compressed tarball:

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

For a bzip2 compressed tarball:

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

For a xz compressed tarball:

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

Once decompression is done, extract the contents with tar as shown below:

tar xvf pkgname-version.tar

Building package from the source

The utilities that are commonly used to generate the configuration/build scripts for software source code packages include autoconf and automake.

  • Autoconf generates scripts that can adapt the packages to different variants of UNIX-like systems without manual user intervention. It creates a configuration script for a package from a template file that lists the operating system features that the package can use.
  • Automake utility automatically generates Makefile.in files that are compliant with the GNU Coding Standards.

To build a program from a source code, you need to obtain the source code and unpack it. Before you can unpack the package, it is wise to check the contents of archive so as to verify whether they will or will not create their own directories once extracted. To list the contents of the archive, run the command below.

tar tvf pkgname-version.tar.*

For example

tar tvf pkgname-1.2.3.tar.gz
---output truncated---
pkgname-1.2.3/configure.ac
pkgname-1.2.3/Makefile.in
pkgname-1.2.3/m4/
tar tvf pkgname-4.5.6.tar.xz
configure.ac
Makefile.in
m4/
  • In the first case, it is fine to extract in the current directory.
  • In the second case, you need to create a directory and extract the archive to that directory as this unpacks the contents on the current directory.

Note that is important to go through the Documentation of the package in question which is simply described in README and INSTALL files.

Configure installation environment

Once you have extracted and gone through INSTALL and README documentation, it is time to setup the environment for compiling and installing the package. This can be achieved using the configure script that checks the system for the required software needed to build the program. It will also will check for both optional and mandatory dependencies. If an optional dependency is missing, it will disable compilation to that dependency. In the case of missing required dependencies, it will print the error and exit.

To configure a software package, call the configure script located in the source code directory as shown in the example below;

cd pkgname-1.2.3/
./configure

The configure script often accepts parameters that enable you to set compile-time options for the software. However, the options may be package specific and thus to find out more about the options, run the help command;

./configure -h

Compiling the Source Code

If the above step completes with no errors, proceed to compile the software by running the make command, again within the source code directory.

make

Any errors encountered during compilation are shown on the console.

Install Programs from Source Code in Linux

If the compilation step completes successfully, proceed to install it. Usually, the software compiled from source code will be installed at /usr/local and its subdirectories. To install, run the command below;

make install

This ensures that the necessary binaries for the software are put under the standard paths so they can be run anywhere in the system.

In case the program doesn’t provide an install target to make, i.e running make install doesn’t install the program, you can copy the program binary to standard path and set the proper ownership and permissions. For example;

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

However, instead of copying the program yourself, you can use install program which can copy the binary and automatically adjust ownership and permissions. To use install program, type:

install pkgname /usr/local/bin

And that is it! to that far, you have learnt the basics on how to obtain, extract, configure, compile and install a program from the source code.

Uninstall a Package Build from Source Code

If you need to uninstall the package, just navigate to the source code directory and run make command with uninstall target as shown below.

make uninstall

If you already removed the source code directory or the uninstall target is not supported (not usually the case), delete the program files manually. You may also need to run the make clean to remove files generated during the compilation process – only if the source code directory has not been removed.

That concludes our guide on how to install programs from source code in Linux.

Other Tutorials

Upgrade a Single Package on CentOS/Fedora

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

Leave a Comment