Hi buddies,
As a newbie in linux I am very-much found of compiling software from source, but still its a critical task for me. So here I log the process for further reference.
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.
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>
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.
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.