How to Compile and Install Software from Source Code on Linux

by Himanshu Arora on June 22, 2012

While yum, apt-get, rpm are very handy to install a package that is already compiled, you still might encounter some situations where you have to install a software from source code.

This article explains on a very high level how to compile and install a software from source code.

Download the Source Code Package and Unpack it

The source code for software on Linux comes in the form of compressed tar files, which typically have either .tar.gz or .tar.bz2 extensions. The tools that are used for packing the source code into these tar balls are ‘tar’ (used for combining multiple files into one), ‘gzip’ or bzip2 (used for compression). To fetch the source code tarball for a particular software you need to know the URL to the tarball.

Once you have the download link, use ‘wget’ to fetch the tarball from command line.

$ wget <link to the tarball>

The above command will download the tarball into the current directory. wget command is very flexible and has lot of options. To learn more about wget, refer to the 15 wget examples.

Next you needs to unpack the tarball in order to get access to the source code and other files. Depending on the extension, use one of the following commands:

$ tar -xvfz <name of tarball with .tar.gz extension>
(or)
$ tar -xvfj <name of tarball with tar.bz2 extension>

tar command is very flexible and has lot of options. To learn more about tar, refer to the 10 tar examples.

Read Install Documentation

Once the software source code is downloaded and extracted, the very first thing that one should do is to go through the documentation. This may sound boring to most of us but this is a very important step as doing this step thoroughly would save you from most of the future problems. The documentation provides information about the software, changes since last version, links to more documentation, information regrading the author of the software, steps for compilation and installation of software etc. So we can see that lots of valuable information is present in the documentation.

This whole information is broadly divided into two files : ‘Readme’ and ‘Install’. While ‘Install’ covers all the information required for compilation and installation, all the other information is covered in the ‘Readme’ file. Please note that the name of file and it case may vary.

Configuration

Once the above step is over then we can assume that we have sufficient theoretical knowledge about this software and now we can move forward and configure the environment for compiling and installing the software on our system. Most of the packages come along with a configuration script that can be used for configuring the environment. The file name for configuration file is mostly ‘configure’. This script usually accepts parameters that can be used to control some features of this software. Also this script makes sure that all the tools required for compilation are present in the system.

To learn about the options provided by a specific configuration file, run the following command:

$ configure --help

To start configuring the build environment, execute the following command :

$ ./configure

The above command will check and/or create the build environment and if everything goes fine then it produces a file called ‘makefile’. The file ‘makefile’ is used in the compilation of the software.

Compilation

Once the makefile is generated, then in the same directory just run the following command:

$ make

The above command will compile all the source code related to the software. If compilation encounters some problem then error is thrown on the console.

Installation

Once the compilation is done successfully then all the required binaries are created. Now is the time to install these binaries in the standard paths so that they can be invoked from anywhere in the system. To do this run the following command :

$ make install

Note that some times installing the software may require root privileges, so one may gain the rights and then proceed with the above command.

The above 5 steps show how to fetch, unpack, configure, compile and install the software from source. Additionally one could do some cleanup by removing the directory created while unpacking the software tarball.

The following articles are few examples on how to install a software from source code.

While compiling and installing open source software from source, there could be some issues/errors that may come up. Lets look at a few of those here:

  • Missing shared library: Sometimes when you run the program you just installed, you get an error related to some .so that your program is not able to find. Firstly, .so are synonymous to the DLLs we have in windows. These are shared libraries that are required by the program. Secondly, these type of errors erupt when your program is installed in some non-standard path or the shared library is actually not present in your system. For the first case, you need to tell the shell environment the path at which these new shared libraries are installed. This can be done by using the ‘ldconfig’ command or by modifying the LD_LIBRARY_PATH variable.
  • Broken source code: No matter how much pain you take by going through all the documentation and covering all the steps building the software but if the source code gives some compilation error then it very much means that the software has broken source code. Nothing much can be done in this case except referring this problem back to the author of this software. Meanwhile, if you think you can you may debug the errors and see if these are trivial errors that can be fixed (like syntactical errors).
  • No configure script: Though rare, but sometimes you’ll find that there is no configuration script present in the source code directory. If this happens that does not mean that you are stuck. In this case all you need is to go through the documentation in detail and there you will definitely find some information regarding configuration of environment for compiling and installation of software.

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 14 comments… read them below or add one }

1 Denis June 22, 2012 at 7:08 am

Actually, it’s more useful to use ‘checkinstall’ instead of ‘make install’.

This command will create a package for your system out of the sources and install it via native methods. It’ll make package update and removal easier.

2 Pierre B. June 22, 2012 at 8:34 am

Thank you for this handy quick guide TGS !

3 Dan June 22, 2012 at 9:13 am

Thank you.
Due to memory loss (strokes) issues I need to have many “cheat sheets.”
This is a valuable article for me to add to my collection.

4 Jalal Hajigholamali June 22, 2012 at 9:36 am

Hi,
Thanks a lot for very nice article…

5 Rajesh Kumar V June 22, 2012 at 10:13 am

Thanks a lot.

6 Shankar Nandy June 23, 2012 at 2:21 am

Thanks for the details article..

7 Athul June 24, 2012 at 1:27 am

Sometime for some packages configure script may not be present , we might need to generate them using autoreconf.

http://en.wikipedia.org/wiki/GNU_build_system

Maybe this can be a new topic.

8 Nuz June 25, 2012 at 5:34 am

Tips:

You can do that to minus time for compiling :
# make -jX

Where ‘X’ is the number of thread.

9 David June 30, 2012 at 12:30 am

Thanks man. But I’ve failed 99.99% of all the times I’ve tried to install software from source. It’s a nightmare to me and my worst experience with Linux.

10 leon June 30, 2012 at 1:56 am

What are the uninstall options with make install?
Thanks for the article.

11 Tapan Kumer Das October 2, 2013 at 10:36 pm

Hi,
Thanks, I am new to linux and learning the command. This is so informative about install from source. Thanks again :)

12 Jesin October 11, 2013 at 5:20 am

How does one update a software installed via source?

13 Joe H March 4, 2014 at 10:17 am

I used your tutorial to install Joe Editor on a Amazon Linux AMI.

I have to use Sudo on these AMIs. How do I make the compiled program run under sudo?

14 HickHack November 30, 2014 at 12:19 am

in step one, you said using the following command:
$ tar -xvfz

I tried it, and got this error:
tar: z: Cannot open: No such file or directory

I asked and searched around, seems the filename must follow f argument immediately like this:
$ tar -xvzf

hope you check it. and thanks for this very good tutorial!

Leave a Comment

Previous post:

Next post: