For an alternative guide see Compiling Easy How To.

The Ubuntu repositories contain thousands of packages, and with 3rd party repositories you can get even more. However, sometimes you might want to compile packages from source in the following cases.

Background

You might need to compile software depending on the language in which the program is written. Applications which require compiling are usually written in C and C++. If this is the case, you will need to install a compiler gcc which can be obtained by installing the build-essential package. Normally, you can do this by typing the following in a terminal:

sudo apt-get install build-essential

and in order to run the configure and autogen.sh files that come with many programs:

sudo apt-get install automake

Finally, you will need CheckInstall to safely insert your program in your system:

sudo apt-get install checkinstall

If the program is written in Java, you need a java run-time and compiler. You can install java by following the instructions on the Java page. Sometimes, a java based build tool ant is required. This package is available from the Ubuntu repositories.

sudo apt-get install gcj

If the program is written in an interpreted language like python or perl, you simply need the interpreter, which are installed by Ubuntu by default. You should be able to run the program directly, and in most cases you can install the additional python or perl programs required to run the package from the Ubuntu Repositories. Please see the note at the end of this article about interpreted languages.

Do you really need to compile?

If you are attempting to compile packages because you think the package is not available in the repository, please search for packages at the Ubuntu Package Search. Many authors also make their packages available as a Personal Package Archive (PPA), which can be added as an additional repository in your software sources. One place that many authors make their PPAs available is at Launchpad. You can also search by keyword for packages available to you from the command line (replace "<keyword>" with the actual term you are searching for of course!) by issuing following command:

apt-cache search <keyword>

Obtaining the Sources

Generally, the source packages will be available from the author's website in an archived format. Common archive formats are tar.gz, tar.bz2 and zip. You can unarchive them using file-roller or by using the following commands:

tar -xvf file.tar.gz
tar -xvf file.tar.bz2
unzip file.zip

Sometimes, you may also need to obtain cutting edge code using CVS. You will need to install the cvs utility by issuing a sudo apt-get install cvs. For example, to retrieve the NetworkManager sources, issue the following commands.

cvs -d :pserver:anonymous@anoncvs.gnome.org:/cvs/gnome login
cvs -d :pserver:anonymous@anoncvs.gnome.org:/cvs/gnome co NetworkManager 

If you are installing from cvs, please see the note at the end of this article.

With many (though far from all) packages, it is possible to fetch current code from upstream using Bazaar (sudo apt-get install bzr) without having to look up the revision control location by hand. For example:

bzr get lp:network-manager

Three Stages to Compiling Packages

Most programs that need to be installed from source on Linux can be installed by using the ./configure; make; make install steps. The most difficult step is usually the ./configure step, after which the remaining steps should be easy to complete.

configure

configure is a script that is used for the following things:

You can look at the various options that your specific configure script provides you with by executing:

./configure --help | less

For example, by default the configure script installs to /usr/local. If you want to change the path, you would execute the configure script as shown below:

./configure --PREFIX=/opt

If you are really compiling from scratch, always read through the README and INSTALL files. You will see which pieces of software it needs, usually these are libraries and they usually have a different name in these files than in the packaging system. Then proceed to look for the package using apt-cache tool. Another trick to use if you are compiling a package already in the repositories is to install the build dependencies of the package using the command below:

sudo apt-get build-dep <package>

This will ensure that you have all the dependencies of the package are installed, and hopefully the configure script will not complain about your system having old version of the dependencies installed, in which case you will have to compile the dependencies also.

Troubleshooting the configure

Even after following the above steps, the configure steps might fail.

If no configure file exists at all, you might want to check for the existence of a configure.ac file (although you should, first of all, read the INSTALL and README files very carefully!). If configure.ac exists, chances are that the developer has forgot to create the final configure. To create it yourself, you need the autoconf package:

sudo apt-get install autoconf

After it is installed, you can type

autoconf

and, if you're lucky, a working configure will be created.

Compiling the Package

Hopefully, the configure stage should have been completed by now and the actual compiling can begin. This can be done simply be calling:

make

Now if all goes well you should have a working copy of the program in the sources directory. You should try it out by running it:

src/program_name

If the program fails at this stage, this is probably a bug in the program. You should contact the author, and notify the author of the errors.

Installing the Package

If everything works well and you want to install the program just type:

sudo checkinstall

This creates a .deb file using CheckInstall which makes removing the package at a later stage very easy.

However, if for some reason you do not want to use CheckInstall, this is the legacy way of installing (not recommended):

sudo make install

Notes

Interpreted Languages

Usually interpreted languages will fail to run if a required module is not installed.

apt-cache search foo | grep bar
apt-cache search xml | grep parser

apt-cache search python | grep mysql

apt-cache search ruby | grep activesupport

Comments

For a newbie, you may consider CompilingEasyHowTo more user-friendly.

Also, You may find CheckInstall is helpful.


CompilingSoftware (last edited 2014-08-19 16:26:23 by 162)