Compiling Programs from Source

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search

Posted by: jeremy on 07-10-2003.

Sooner or later when using Linux you will hear the term "compile from source". This can be intimidating the first time you hear it, but once you understand what it is and how it works you will find it is actually quite simple. You may ask yourself why you would want to use source when there are package management systems like rpm and apt. While these tool do make things easy, there are a few drawbacks to using them. First you have to depend on someone to actually do the packaging. This means when a new version comes out you may not be able to install it until the package is updated. You can always go grab the source. Packages are also compiled to the lowest common denominator. Options that many people may need are enabled and all others are disabled. This means that if the feature you want isn't compiled into the package or you want things installed to fit your system exactly source is the only option.

To compile a program from source you do need a few things. Some distros call this the development package. Minimally you will need make, a compiler (usually gcc), the appropriate libraries and a few other basic programs. If you encounter any problems the error message should tell you what you are missing. After correcting the problem you may need to delete the file called config.cache before configure will recognize your changes.

We are now ready to install a basic program. The first thing you need to do is download the source. For this article the program we are installing will be called LinuxQuestions. You will want to download the source to the following directory: /usr/local/src (in this case the filename is linuxquestions.tar.gz). Once downloaded you want to do the following:

code:

tar zxf linuxquestions.tar.gz
cd linuxquestions
./configure
make
make test (optional)
make install


The first command will unzip and untar the downloaded file. You will then need to change into the appropriate directory. Most standard packages will be installed using the ./configure && make && make install process. You should always read the packages instructions which will usually be found in either README or INSTALL. By running ./configure you are doing two basic things: 1) letting the program detect if you have everything it needs installed and where it is 2) telling the program what options you would like it compiled with. The options in questions can range from just a few to literally hundreds. Running

code:

./configure --help | less

will allow you to see the available options. Once configure runs through and you have verified that there are no errors you can continue with make. This step does the actual compiling of the program. Depending on what program you are compiling and the machine you are on this can take anywhere from a few seconds to a few hours. The make test step is optional, but it is a good practice to get into. It will verify that everything is ok and the program operates properly. It should be noted that all steps up to now should be performed by a normal non-root user. The last step however needs to be executed with root permissions. It needs to be run as root as this step will actually copy the compiled programs to your system. If you made it this far you should be able to run the program just as any other program installed on your system. If you ever need to recompile a program you should do a make clean (or make distclean) before doing so. This will delete files created during the previous compile and give you a clean tree.

One notable exception to the previous instructions is perl programs. To install perl programs from source (you can also use CPAN) you need to do the following:

code:

tar zxf linuxquestions-perl.tar.gz
cd linuxquestions-perl
perl Makefile.PL
make
make test (optional)
make install


You will notice that many of the steps are the same with the notable exception of ./configure vs. perl Makefile.PL.

If you ever encounter problems compiling from source the error should clearly state what you are missing or why it is failing. If you need to post to linuxquestions.org remember to post the complete error message you are getting and what steps you took.