Most of the time, installing software in Linux is a breeze. Package management utilities like Apt, Portage, and Yum have made software installation in Linux even easier than it is in Windows (in my opinion at least). If you know what you want, you simply tell your package manager that you want it, and it’ll find, download, install, and configure your new package for you.
Sometimes, however, the package doesn’t exist in your distribution’s repositories. Often, in cases like that, your only option is to download a tarball (usually .tar.gz, .tar.bz, or .tgz) which contains the source code for the program that you have to compile yourself. While it may be a little intimidating at first, compiling from source is normally a quick and easy process. Today, we’ll learn how.
First off, I should note that not all tarballs are the same. This guide will be assuming that the program you’re trying to install is a normal GNU-style source code collection. Most require all the steps noted below, but many skip one step or another. For the purposes of the tutorial I’ll be compiling the source code package of Python 3.0.1 from the Python homepage.
Step 1: Extract the tarball
For those new to Linux, tarball is a term commonly used to refer to a file which contains other files. It’s a lot like a ZIP or RAR file in Windows, except that the tar program, on its own, does not compress the files. Tar works with a compression program like gzip to actually compress the files, which is why you commonly see two extensions (.tar and .gz). This is sometimes abbreviated to just .tgz.
Fortunately, we don’t need to run two separate programs to extract the files, we just tell tar to run the files through gzip to decompress. You can use a graphical utility to extract those files by simply double clicking the tarball from your file manager, or you can do it from the command line with:
tar -zxvf mytarball.tar.gz
The options we gave tar are as follows:
- -z to tell tar to run this file through gzip to decompress (use -j for bzip files)
- -x to extract the files
- -v for “verbose”, so we can see a list of the files it’s extracting
- -f to tell tar that we’re working with a file
For easier unzipping, see the Tips section at the bottom of this page
Once the files are extracted, open a command terminal and go to the directory where the files have been unzipped. Before we can compile, we need to run the configure script. The job of the configure script is to check your system for all the software necessary to compile the program from source code into a usable binary program. It looks for things like gcc version and other tools needed to build the software. So once you’re in the directory with all the files that were unpacked from the tarball, type in
If all goes well it’ll go through a check of various parts of your system, then drop you back to the command line like below:
The most common cause of errors in this step is a missing dependency. Look closely at any errors you may get to determine what package is missing.
This is the real meat of the process – where we compile the source code into a runnable program. This is normally the easiest step, only requiring a single command. If the configure step completed without errors, simply type in
On a large program, this step might take a few minutes. Once done, you’ll be dropped back to the shell prompt as shown here.
Technically, your program is now ready to use. Under most circumstances, however, you’ll want to run one more step so that she program can be fully installed into the correct locations for it to be run from anywhere.
All this really does is copy the now-compiled program into the system directories like /usr/bin so that it can be run from any directory without having to specify a path to the files. Since it’s copying to a directory outside your home, you’ll probably need root privileges. If the make step completed without errors, simply run
sudo make install
to copy the files. At this point, you’re all done! Your new program can be used like any other.
Chances are, you’ll be compiling from source more than once in your life. In fact, for those who like to use the latest and greatest software, this can be very common. To make it a little easier, open your .bashrc file from your home directory, and add the following aliases to the end:
alias ungz="tar -zxvf" alias unbz="tar -jxvf" alias cmi="./configure && make && sudo make install"
Our latest tutorials delivered straight to your inbox