Install Software from Source




Install Software from Source

When there's no repository and no package, there's still hope. Here's how to build and install standalone programs from source.

It used to be that when you wanted to install a new program under Linux, you would locate the project's home page, find a source tarball, download it, and then extract and compile the source. After a lot of text scrolled by in your terminal, your program would finally be installed. This sort of method was so common, in fact, that many distributions would ship with all of the basic compilers and libraries installed. Ahh, the good old days....

These were not the good old days. The reality was that along with the steps mentioned above, you had some additional chores:

  1. You had to revisit the project home page when you realized that there were numerous library dependencies that you needed to first track down, along with any other programs this software relied on.

  2. If the software depended on other programs, you usually needed to download and compile those first, only to discover that they had external dependencies, too, so you had to revisit their project pages to track them down as well.

  3. Finally, after all the time you spent tracking down all the dependencies and successfully installing the software, the developer would have just released a new version with more features and fewer bugs, so you would have to start again from scratch.

Nowadays, almost every distribution, including Ubuntu, not only uses packages (software that has been compiled and bundled for you), but also incorporates some sort of automatic dependency management. When you want to install software, you just tell Ubuntu which program to install and it will find it and any dependencies, and install it all for you. When you want to upgrade, Ubuntu will manage any new dependencies and new versions for you. So little software these days requires you to download and compile source that (by default) Ubuntu doesn't even install all of the software you need to compile source code. Nine times out of 10, the package you want will either be packaged for the distribution already, or at least be available in a precompiled .deb package. ("Install and Remove Standalone .deb Files" [Hack #57] describes how to install these packages.)

Having said all of this, there are still a few circumstances that crop up that require you to download and compile a program from source:

  • A brand-new open source project has been released. It is so new that no one has yet decided to package it for Ubuntu.

  • There is some special feature in the kernel or other piece of software that isn't enabled by default, or a patch you would like to add, so that you must compile the kernel from source. (However, you might find that you can customize the source .deb package [Hack #63] to get what you need.)

  • A third-party hardware manufacturer has released drivers for Linux; however, they are released only in source form (or perhaps they are released only as RPMs built for Red Hat or SUSE along with a .tar.gz file for any other distribution), and Ubuntu hasn't released some sort of prebuilt package for it.

  • You are a programmer yourself, and you would like to help out with an open source project by submitting patches, or perhaps you want to start a whole new project.

  • A new version of a program has come out, and you can't wait for it to be packaged, but you will have to build it yourself. (However, note that, depending on the program, you risk breaking other parts of the system that depend on the older version of the program. Typically, when a program has been updated that affects other programs, it and its dependencies will be held back temporarily by the package manager until they are all ready to be installed together.)

In the world of software installation under Ubuntu, compiling a program from source should be the absolute last resort. There is a good chance that you can break your system and its dependencies if you don't know what you are doing. Please try all of the other avenues at your disposal, including Ubuntu's built-in package managers mentioned in "Manage Packages from the Command Line" [Hack #54], "Manage Packages with Synaptic" [Hack #55], and "Manage Packages with Adept" [Hack #56]. If that doesn't work, try to locate a precompiled .deb package and follow the steps in "Install and Remove Standalone .deb Files" [Hack #57]. Finally, if none of those choices are available, then read this hack.


Install Compiler Tools

If you do need to compile from source, first you will need to install a compiler and all of the other packages essential to building programs from source. Ubuntu makes this easy: just open your preferred package manager, and find and install the build-essential package. This will grab and install the GNU C compiler (gcc), make, g++, and a number of other programs you will need:

$ sudo apt-get install build-essential
            

Get and Compile the Source

Once all of these programs have installed, download the tarball for your program and extract it somewhere, such as your home directory. Most tarballs extract into their own directory (often named after the program name), so the next step is to cd into that directory:

greenfly@ubuntu:~$ tar xvzf 
               
                  program.tar.gz
               
greenfly@ubuntu:~$ cd 
               
                  program/
               
            

Most programs these days follow a common three-step compile method of configuring, but before you launch into the process, you will want to read the installation instructions the developer has provided (usually in a file called INSTALL or README in the main source directory, or otherwise provided on its main project page).

Step 1: configure

The first step in the build process is usually to run the configure script that is located in the main source directory:

greenfly@ubuntu:~/program$ ./configure
               

This program will scan the system and make sure that all of the libraries that the program needs exist, as well as set any file paths or other settings. If you don't have all of the libraries the program needs, the configure script will error out and tell you what library you are missing (or what newer version you need). If you see this type of error, you will need to not only track down the package that contains that library, but also the development package that contains all of the header files for that library (in Ubuntu, most of these packages end in -dev). Once you get these libraries installed, run the configure script again to see if there are any other libraries you need.

Step 2: compile

Once the configure script exits successfully, the next step is to compile the source code. Most projects use makefiles these days along with the configure script, so the next step is to run make from the main source directory:

greenfly@ubuntu:~/program$ make
               

You should see a stream of compiler output roll by, and eventually you will end up back at the prompt. It's possible for errors to occur during this step, but these are generally more difficult to debug because they may have resulted from a syntax error in the source code or some other problem. If you do find some sort of compiler error, check the software's mailing list or other support channels (if one exists) for a possible known bug with the software; otherwise, file a bug report.

Step 3: install

Once the source code is compiled, the final step is to install the software on the system. Most programs include a function in their makefiles for installation; however, note that you will generally have to install the program as root, since it will want to install files under /usr and other directories that are writable only by root. Make sure you are still in the main source directory and type:

greenfly@ubuntu:~/program$ sudo make install
               

The program will now be installed to the system.

Some programs also provide an "uninstall" function to remove the program from the system. Just change back to the main source directory and run:

greenfly@ubuntu:~/program$ sudo make uninstall
                  


Hacking the Hack

In most cases, installing a package using this procedure will install the application in /usr/local. But if you want to be sure that the installation will stay away from the parts of the filesystem patrolled by Ubuntu, add --prefix= pathname to the ./configure command, as in:

greenfly@ubuntu:~/program$ ./conFigureprefix=
               
                  /opt
               
            

This will almost always work. However, there are some programs that don't respect the --prefix option, and some programs (such as those that include kernel modules) will insist on spreading themselves all over your filesystem.