Source compilation and the Make process
Source compilation of a software package is sometimes offered with server management services. Here is an overview of the process.
Contents |
The need for source compilation
Why compile a software package from source when it can be done easily via available RPMs? The answer is that it gives you much more flexibility. Also, it is quite possible that the RPMs that you are going to use are outdated by a few weeks. Moreover, source compilation is a fairly straightforward process.
Requirements
- A system with the required hardware
- Basic knowledge of how to work with the particular operating system
- The package containing the source code
- A compiler
- Utilities such as Make, tar, gunzip, etc.
We will use Apache for our example compilation.
Step 1: Extraction
The package is usually in a compressed tar archive (also known as tarball) with extensions like tar.gz or tar.bz2. These can be unpacked by using the following commands.
For tar.gz:
$ tar xvzf abc.tar.gz
For tar.bz2:
$ tar xvjf abc.tar.bz2
This would usually create a directory with the name of the package (abc). Please read any instruction files like README or INSTALL in this directory.
Step 2: Configure
The source code usually contains a configure script which performs some checks and prepares the software for compilation.
Assuming that you are inside the directory, the configure script can be run by:
$ ./configure
This script will not compile anything. It checks the system to see which compiler is available and to find out whether the required libraries are present. Using this script, we can also decide the location where Apache will be installed and which optional modules need to be compiled. The script assigns values to the system-dependent variables, which in turn are used to generate a Makefile. The Makefile generates the actual binary.
The list of options can be viewed by using:
$ ./configure –help
Step 3: Make
The utility Make is used to build the binary. It uses the Makefile (generated in Step 2) located in the source directory. The Makefile has detailed, step-by-step instructions on how to compile the software. A Makefile has a set of rules in a typical format of target, dependency, and the command. The advantage of using Make is that if some of the source files are changed when the software is recompiled at some point, then only the files which are actually changed or the files which depend on the changed files will be recompiled.
For example, if a file has changed, each source file that includes the file must be recompiled to be safe. Each compilation produces an object file corresponding to the source file. Finally, if any source file has been recompiled, all the object files, whether newly made or saved from previous compilations, must be linked together to produce the new executable program.
Moving ahead, issue the following command:
$ make
This will start the compilation of Apache. The process time will depend upon your system capabilities.
Step 4: Install
As root, run the following command:
$ make install
This will install Apache in the location defined by the configure script. You can ignore the weird messages as long as you do not stumble upon an error. Once the installation is complete, you can save some disk space by cleaning up the junk using:
$ make clean
Make sure that you keep the Makefile for future purposes.
That is it! You can start Apache now.
Step 5: Uninstall
If for some reason, you do not like the software, you can use:
$ make uninstall
If you come across any errors, then you will have to manually remove the files. In case you are unaware of their location, reading the Makefile will give you the answer.
Web Hosting Wiki article text shared under a Creative Commons License.
