{"id":627,"date":"2018-09-06T15:34:35","date_gmt":"2018-09-06T12:34:35","guid":{"rendered":"http:\/\/kifarunix.com\/?p=627"},"modified":"2024-03-11T19:44:42","modified_gmt":"2024-03-11T16:44:42","slug":"compile-and-install-programs-from-source-code-in-linux","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/compile-and-install-programs-from-source-code-in-linux\/","title":{"rendered":"Compile and Install Programs from Source Code in Linux"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1062\" height=\"596\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/05\/compile-n-install-program-from-source-code.png\" alt=\"Compile and Install Programs from Source Code in Linux\" class=\"wp-image-16661\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/05\/compile-n-install-program-from-source-code.png?v=1684073737 1062w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/05\/compile-n-install-program-from-source-code-768x431.png?v=1684073737 768w\" sizes=\"(max-width: 1062px) 100vw, 1062px\" \/><\/figure>\n\n\n\n<p>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&#8230;<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#install-programs-from-source-code-in-linux\">Install Programs from Source Code in Linux<\/a><ul><li><a href=\"#install-required-utilities-build-tools\">Install Required Utilities\/Build tools<\/a><\/li><li><a href=\"#unpacking-source-code-archive\">Unpacking Source Code Archive<\/a><\/li><li><a href=\"#building-package-from-the-source\">Building package from the source<\/a><\/li><li><a href=\"#configure-installation-environment\">Configure installation environment<\/a><\/li><li><a href=\"#compiling-the-source-code\">Compiling the Source Code<\/a><\/li><li><a href=\"#install-programs-from-source-code-in-linux-1\">Install Programs from Source Code in Linux<\/a><\/li><li><a href=\"#uninstall-a-package-build-from-source-code\">Uninstall a Package Build from Source Code<\/a><\/li><li><a href=\"#other-tutorials\">Other Tutorials<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"install-programs-from-source-code-in-linux\">Install Programs from Source Code in Linux<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"install-required-utilities-build-tools\">Install Required Utilities\/Build tools<\/h3>\n\n\n\n<p>Before any program can be compiled, there are necessary utilities\/packages\/tools that need to be installed such as the <a href=\"https:\/\/gcc.gnu.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">GCC<\/a> compilers. Such utilities are provided by the Development Tools or build-essentials at least on RHEL derivatives and Debian derivatives respectively.<\/p>\n\n\n\n<p>To install such tools, run the following commands:<\/p>\n\n\n\n<p>On Debian Derivatives:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install build-essential<\/code><\/pre>\n\n\n\n<p>On RHEL Derivatives:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>yum groupinstall \"Development Tools\"<\/code><\/pre>\n\n\n\n<p>There might be other additions libraries\/packages or tools required by specific packages for compilation. Consult the application documentation on the same.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"unpacking-source-code-archive\">Unpacking Source Code Archive<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>The most common methods used for compressing the tarballs include;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>gzip<\/strong><\/li>\n\n\n\n<li><strong>bzip2<\/strong> <\/li>\n\n\n\n<li><strong>xz<\/strong><\/li>\n<\/ul>\n\n\n\n<p>So the source code may come compressed as <strong>pkgname-version.tar.gz<\/strong>, <strong>pkgname-version.tar.bz2<\/strong>, or <strong>pkgname-version.tar.xz <\/strong>e.t.c<\/p>\n\n\n\n<p>The GNU version of the <strong>tar archiving utility<\/strong> supports these methods and thus make it easy to unpack files compressed in such a manager.<\/p>\n\n\n\n<p>To unpack a gzip compressed tarball:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tar zxvf <strong>pkgname-version.tar.gz<\/strong><\/code><\/pre>\n\n\n\n<p>To unpack a bzip2 compressed tarball:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tar jxvf <strong>pkgname-version.tar.bz2<\/strong><\/code><\/pre>\n\n\n\n<p>To unpack an xz compressed tarball:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tar Jxvf <strong>pkgname-version.tar.xz<\/strong><\/code><\/pre>\n\n\n\n<p>Alternatively, the compressed source code can be decompressed using specific utilities after which the contents are extracted using the GNU tar utility.<\/p>\n\n\n\n<p>For example;<\/p>\n\n\n\n<p>To decompress a gzip compressed tarball:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>gunzip <strong>pkgname-version.tar.gz<\/strong>\nOr\ngzip -d <strong>pkgname-version.tar.gz<\/strong><\/code><\/pre>\n\n\n\n<p>For a bzip2 compressed tarball:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>bunzip2 <strong>pkgname-version.tar.bz2\n<\/strong>Or\nbzip2 -d <strong>pkgname-version.tar.bz2<\/strong><\/code><\/pre>\n\n\n\n<p>For a xz compressed tarball:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>unxz <strong>pkgname-version.tar.xz<\/strong>\nOr\nxz -d <strong>pkgname-version.tar.xz<\/strong><\/code><\/pre>\n\n\n\n<p>Once decompression is done, extract the contents with tar as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tar xvf <strong>pkgname-version.tar<\/strong><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"building-package-from-the-source\">Building package from the source<\/h3>\n\n\n\n<p>The utilities that are commonly used to generate the configuration\/build scripts for software source code packages include <strong>autoconf<\/strong> and <strong>automake.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Autoconf<\/strong> 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.<\/li>\n\n\n\n<li><strong>Automake<\/strong> utility automatically generates <strong>Makefile.in<\/strong> files that are compliant with the GNU Coding Standards.<\/li>\n<\/ul>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tar tvf pkgname-version.tar.*<\/code><\/pre>\n\n\n\n<p>For example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tar tvf pkgname-1.2.3.tar.gz<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>---output truncated---\npkgname-1.2.3\/configure.ac\npkgname-1.2.3\/Makefile.in\npkgname-1.2.3\/m4\/<\/code><\/pre>\n\n\n\n<pre id=\"block-ff0df80b-3da3-4c7b-90ea-fc4f8b4a27bb\" class=\"wp-block-preformatted\">tar tvf pkgname-4.5.6.tar.xz<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>configure.ac\nMakefile.in\nm4\/<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the first case, it is fine to extract in the current directory.<\/li>\n\n\n\n<li>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.<\/li>\n<\/ul>\n\n\n\n<p>Note that is important to go through the Documentation of the package in question which is simply described in <strong>README<\/strong> and <strong>INSTALL<\/strong> files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"configure-installation-environment\">Configure installation environment<\/h3>\n\n\n\n<p>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 <strong>configure<\/strong> 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.<\/p>\n\n\n\n<p>To configure a software package, call the <strong>configure <\/strong>script located in the source code directory as shown in the example below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd pkgname-1.2.3\/\n.\/configure<\/code><\/pre>\n\n\n\n<p>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;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>.\/configure -h<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"compiling-the-source-code\">Compiling the Source Code<\/h3>\n\n\n\n<p>If the above step completes with no errors, proceed to compile the software by running the <strong>make<\/strong> command, again within the source code directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>make<\/code><\/pre>\n\n\n\n<p>Any errors encountered during compilation are shown on the console.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"install-programs-from-source-code-in-linux-1\">Install Programs from Source Code in Linux<\/h3>\n\n\n\n<p>If the compilation step completes successfully, proceed to install it. Usually, the software compiled from source code will be installed at <strong>\/usr\/local&nbsp;<\/strong>and its subdirectories. To install, run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>make install<\/code><\/pre>\n\n\n\n<p>This ensures that the necessary binaries for the software are put under the standard paths so they can be run anywhere in the system.<\/p>\n\n\n\n<p>In case the program doesn&#8217;t provide an install target to make, i.e running make install doesn&#8217;t install the program, you can copy the program binary to standard path and set the proper ownership and permissions. For example;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp pkgname \/usr\/local\/bin\/\nchmod +x \/usr\/local\/bin\/pkgname<\/code><\/pre>\n\n\n\n<p>However, instead of copying the program yourself, you can use <strong>install <\/strong>program which can copy the binary and automatically adjust ownership and permissions. To use install program, type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>install pkgname \/usr\/local\/bin<\/code><\/pre>\n\n\n\n<p>And that is it! to that far, you have learnt the <strong>basics<\/strong> on how to obtain, extract, configure, compile and install a program from the source code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"uninstall-a-package-build-from-source-code\">Uninstall a Package Build from Source Code<\/h3>\n\n\n\n<p>If you need to uninstall the package, just navigate to the<strong> source code directory<\/strong> and run <strong>make<\/strong> command with <strong>uninstall<\/strong> target as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>make uninstall<\/code><\/pre>\n\n\n\n<p>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 <strong>make clean<\/strong> to remove files generated during the compilation process &#8211; only if the source code directory has not been removed.<\/p>\n\n\n\n<p>That concludes our guide on how to install programs from source code in Linux.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"other-tutorials\">Other Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/upgrade-a-single-package-on-centos-fedora\/\" target=\"_blank\" rel=\"noreferrer noopener\">Upgrade a Single Package on CentOS\/Fedora<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<\/p>\n","protected":false},"author":1,"featured_media":16661,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121],"tags":[6694,6695,124,122,123],"class_list":["post-627","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-compile-and-install-programs-from-source-code-in-linux","tag-install-package-from-source-linux","tag-make","tag-package-installation","tag-source-code","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/627"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=627"}],"version-history":[{"count":9,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/627\/revisions"}],"predecessor-version":[{"id":20994,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/627\/revisions\/20994"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/16661"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}