April 5, 2010, 4:05 a.m.
posted by void
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:
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:
Install Compiler ToolsIf 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 SourceOnce 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: configureThe 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: compileOnce 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: installOnce 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.
Hacking the HackIn 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. |
- Comment

