How-To: Compile Programs From Source in Linux
Reasons to compile
With the prevalence of software available for many distros, why would anyone want to compile software from source? Compiling allows you to custom-fit a program to your particular hardware configuration and CPU architecture, which is useful if a program has no binary that is compatible with your processor. However, this is seldom a problem these days, since most computers now use 32 or 64-bit x86 processors. In the past, Linux enthusiasts often compiled programs from source to wring the greatest possible performance out of their hardware. More recently, this has mostly become a non-issue due to the increases made in computing speed; while compiling may offer a slight performance increase, it is not enough to really make a difference.
Although the introduction of package management on most distros, less diversity in CPU architecture among the user base, and massive increases in hardware speed have largely reduced or eliminated the need to compile software yourself, there are still a few instances where you would have to do so. Although the various official and unofficial software repositories for Ubuntu and other distros include most of the tools that the average user would need for any given purpose, the repositories are not completely comprehensive. Old packages sometimes get dropped and updated versions are often slow to be added. It may also take a release cycle or more for brand-new programs to be included.
While Ubuntu and Debian have “backports” repositories that have fairly new packages in them, many other distros do not have such a resource. For large projects with large community support, the developer may offer nightly builds, but this is not the case for most projects. The only reliable way to get bleeding-edge software (stability issues aside) is to either find a repository that has it or download the source code from the developer and build it yourself.
If you want to be a programmer at some point, you are going to need to know how to compile applications, since that is really the only way to develop your own projects and effectively contribute your own code to open source projects that other people have started.
It is also wise to compile security-oriented software (like encryption tools) yourself. Although binaries are generally trustworthy if they came from an official repository or developer website, you can never really be 100% sure of what you're getting unless you build it yourself. (preferably after a code audit, if you have sufficient skill to do so)
Alternatively, you may find a program you are interested in, but it is not packaged for your distribution (e.g. RPM packages on a DEB-based distro) and there is no native package available for you to use. While there are tools (like alien) that can convert packages from one type to another, the program may not always work correctly after it has been converted.
In such situations, your only real option is to build from source.
Reasons not to compile
Although compiling software from source can solve some of your problems, it can also create new ones. Compiling and installing software from source effectively bypasses your package management system. This means that you must personally do the work that your package management system would otherwise do, such as keeping track of installed software, satisfying dependencies, and even preventing conflicts between different programs. This last situation is where the most can go wrong.
A decent package manager is aware of the specific version of the dependencies that a program needs to run and is able to cross-reference that data with the needs of other programs. This is especially important when updating; if a new version of a program includes dependencies that would break other installed programs, the package management tool should postpone installing the new version until the other programs that would be affected can be safely updated as well. When you compile from source, you are forcing your changes through safeguards meant to help protect your system's software integrity. This can be very dangerous if you have not taken the time to understand and estimate the repercussions this action may have.
Compiling software from source is a useful concept to understand, (or even essential, depending on your aspirations) but it should always be considered a measure of last resort. On most modern distros, there are much better ways to install software, and these should always be used first whenever possible.
This guide will tell you how to compile programs from source on Linux. We will not cover the specifics of building a kernel, but we will teach you how to build individual programs on any distro. Likewise, this guide will not address specialized tools (like emerge) that are found on source-based distros like Gentoo. This guide is intended for fairly advanced users instead of those new to Linux. Some knowledge of the terminal is required due to the way the compiling process works.
Preparation
Before you compile your first program, you must prepare your compiler toolkit. Linux has many compilers and related tools available as part of the GNU Project; these include gcc, (the GNU C compiler) g++, (the GNU C++ compiler) make, (a tool to help automate the build process) and many others. You will probably need to install them yourself (check your distro's repositories) since few distros include them out of the box.
Fortunately, many distros include most of the compiler utilities in a single package so you won't have to install each one separately. (Ubuntu's is called “build-essential”) Unless you are familiar with the language the program is written in, you may not know what compilation tools you will need until you run the configuration script.
Standard procedure
1. First, you must acquire the source code of the program you wish to compile. (this can be found at the developer's website or at an online resource like Sourceforge.) Source code usually comes in archive files called tarballs, identifiable by a tar.gz extension. You should save the tarball to a folder where you have write permission, such as your own home directory. The temp (/tmp) folder is not recommended due to reasons we will address later.
2. Open a terminal and navigate to the directory where you saved the source tarball.
3. Extract the tarball by typing “tar -xvf program.tar.gz”. (obviously, you should substitute program.tar.gz with the real filename) Most of the time, a tarball will create a new folder (this guide will refer to it as the build directory) during the extraction process and will place its contents in there.
4. Navigate to the build directory.
5. The build directory may have many files and subfolders in it, but the first thing you should look for is a file called “configure”. For a moment, you should compare the software compilation process to cooking. Before you start making dinner, you need to know what goes into a recipe, (and the cooking instructions) or the dish will turn out wrong. The configuration script is like a grocery list; it makes sure that you have everything you need before the compilation process begins.