A Beginner's Guide to Compiling on Linux
Having used Linux as my primary operating system for nearly a year now, I – like many Linux users – have found that the various package management systems and interfaces have allowed for my computing experience to be the much simpler. Be it the newer Ubuntu Software Center, the classic Synaptic Package Manager, or the apt-get system, installing software on Linux truly isn’t all that difficult of a task. For me, being able to install an application just as easily as I could walk into a restaurant and order a slice of pie made my migration process to Linux very simple.
However, the repository-based package installation system has a number of downsides as well. For example, installing a package or application on a computer without an Internet connection can be – while possible via means of a workaround – quite a task. The other major downside to package managers is that the repositories they connect to often do not have the most up-to-date version of an application available, leaving users who want the latest and greatest out in the cold. The solution to this problem is to download and compile the source-code for an open-source application manually. While this may seem to be somewhat of a daunting task, the fact of the matter is that downloading and compiling source code actually isn’t all that difficult.
First off, you’re going to need to open up your terminal, as this is how we are going to download and install the source code. If you haven’t compiled code before, you will likely need to install a compiler and other necessary tools. Most users will be able to suffice by installing the “build-essential” package for their distribution. If you are not a sudo/root user, you will need to have your system administrator install this for you. As I am using Ubuntu, I will use the following command to install “build-essential”.
Note: the “command” is the part located after the dollar sign (“$”). “michael@michael-laptop” simply indicates the user and computer that I am working on, and the part after the colon (“:”) is the directory that we are working in. In this case, we are working in my “home” directory, which is expressed as “~”.
michael@micheal-laptop:~$ sudo apt-get install build-essential
After entering my password (I have sudo access), the build tools are downloaded and installed for me to use.
In this example, I will be downloading and installing the IRSSI terminal-based IRC client. To do this, we will first want to get the “source” package, which like most open-source projects is readily available on their website. Once we have found the location of the source package (typically a .tar.gz file), you will want to issue the “wget” command to download it onto your machine.
michael@michael-laptop:~$ wget http://irssi.org/files/irssi-0.8.15.tar.gz
Obviously you will want to replace the actual file with the package you intend to download and install. You will know that your download was successful when you see the progress bar reach 100% and the terminal says that your file was saved.
Once we have downloading the source package, we need to un-tar (decompress) it in order to access and install its contents. Because this is a .tar.gz file, I will be using the following command. If all goes well, you should see a list of files appear in your console as the archive is de-compressed. It’s important to realize that this can take a bit of time if you are decompressing a larger file.
michael@michael-laptop:~$ tar -xvf irssi-0.8.15.tar.gz
Now, we will want to navigate to the directory where the archive was extracted to. Typically the folder name will be the same as the archive name, less the “.tar.gz” suffix.
michael@michael-laptop:~$ cd irssi-0.8.15/
If you do not know the name of the directory that was extracted, you can use the directory search command to look at the sub-directories and files that exist in the directory you started off in.
michael@michael-laptop:~$ dir -s
In my case, the folder I’m looking for is simply “irssi-0.8.15”, however you will (again) obviously be looking for the folder for the project/application you are working to compile.
Once you make your way into the extracted directory, there should be a file called “configure”. This file is what prepares the source code to be compiled. While in many cases you can simply execute the “configure” file by executing it (“./configure”), it is important to realize that different applications and projects have a number of options which can (and sometimes need) to be passed to the configuration script. To see a list of these options, you can typically use the command below.
michael@michael-laptop:~/irssi-0.8.15$ ./configure --help
In theory, you will see a list of the various options that you can pass to the configuration script in order to customize the application installation to your needs. Sometimes this takes a bit of research and/or trial and error in order to figure out what options are necessary for you.
One of the most common configuration options is “–prefix”, which allows you to install the project to a directory that you specify. If you do not have root/sudo/su/admin access on a particular Linux installation, using “–prefix” will allow you to install the project to a directory that you have access to – typically your home directory.
Once you know what options need to be based to the configuration script, enter the following command, replacing your options as needed. In this example, I am using the common “–prefix” option to demonstrate the concept of passing an option, and because it is one of the options that people may often use.
michael@michael-laptop:~/irssi-0.8.15$ ./configure --prefix=/home/michael/irssi
With any luck, the configuration script will check to make sure that you have all of the components needed to compile and run the application.
If you find that you are lacking dependencies, you can download and compile them (and their dependencies, in a domino-like fashion) first. This can, however, be a somewhat tedious process. If you run across this issue, you should consult the documentation or read-me files for the project that you are attempting to compile, as they often-times contain a list of dependencies that you will need prior to compiling. From there, you can either compile them yourself or download/install them from your repository.
Additionally, Internet-connected sudo/root users of the apt-get system may be able to download the dependencies using apt-get’s “build-dep” option. To do this, use the following command, replacing “irssi” with the name of the package you are trying to compile.
michael@michael-laptop:~/irssi-0.8.15$ sudo apt-get build-dep irssi
Once all of the dependencies are installed and the configuration tool completes successfully, the bulk of the work has been done. From here, you should only need two commands to compile and install the application.
michael@michael-laptop:~/irssi-0.8.15$ make
michael@michael-laptop:~/irssi-0.8.15$ make install
These two commands will, respectively, compile the project and move it to its proper directory on the system. While these commands typically don’t give the user any problem, there are definitely going to be times when the project does not compile because of an error with the system or package configuration. In this case, the tools will typically inform you of a log file that has been generated. My advice would be to copy the contents of this log file to a community such as the BestTechie forums, where someone who is familiar with Linux (and maybe even your particular issue) should be able to resolve the problem.
One of the great things about Linux is the large community behind it. Chances are that someone has already seen your problem – if not having dealt with it on their own – and will be able to at very least point you in the right direction.
When all is said and done, you should have successfully installed your application. If you installed it to the default system directory (this is typical if you didn’t specify a directory on your own), you should be able to type the package name (e.g. “irssi”) at the command-line in order to launch it. If not, you will need to call it using the full path where you installed it (e.g. “/home/michael/irssi/bin/irssi”). From there, you should be able to create launchers and shortcuts on your system to open your compiled project in no time.
Good luck and happy compiling!