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.
- Package is not available in the repositories
- Package in the repositories may be too old
- Package in the repositories has a feature in program disabled due to some reasons
- Package in the repositories may have a bug which has been fixed by the author of the package.
- You want to test a patch to help a developer fixing a bug
- You want to try your hand at compiling programs from scratch.
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:
- It checks that your computer fulfils all the necessary requirements to build the package
- It can help you change the default paths it looks for its requirements
- It is used to enable/disable various options in the compiled program
- It can change the path that the program will be installed in
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.
You always need the -dev package when compiling.
For compiling GNOME applications, the package gnome-devel is usually required; for KDE applications, use kde-devel
- The names of C and C++ libraries always start with lib, so if ./configure complains about missing library foo, you need the libfoo-dev package.
- If a configure script asks for "X includes", you should probably install the following packages: xlibs-dev, xlibs-static-dev,x-window-system-dev Note: Sometimes just installing xorg-dev and x-dev do the trick. -Racecar56 Under Jaunty, one would install xorg-dev and libx11-dev instead.
- Another technique to determine which package to use is using the auto-apt tool or the apt-file tool (auto-apt might be faster).
sudo apt-get install auto-apt sudo auto-apt update auto-apt search missing-file.h
Using apt-file is identical to the above, if you substitute every occurrence of "auto-apt" with "apt-file". Try compiling the software disabling the feature which is failing using the ./configure --disable-FEATURE option.
- If you are unable to troubleshoot on your own, you can always ask for help in #ubuntu
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
If the program is a kernel module you will also need gcc-3.4 (on breezy) and the kernel-headers package. They latter can be installed with sudo apt-get install gcc-3.4 linux-headers-$(uname -r). Please note that kernel modules have to be recompiled after each kernel upgrade (and new headers have to be downloaded too). For compiling a complete kernel, look at the KernelCompile.
If you are trying to recompile an existing Debian package, use dpkg-buildpackage -rfakeroot and if any packages are missing, the build script will tell you the name of the package.
If you are compiling from revision control (CVS, Bazaar, etc.), you will generally need to obtain the sources from revision control, and run another additional step of running the autogen.sh script to generate the configure script for you.
Interpreted Languages
Usually interpreted languages will fail to run if a required module is not installed.
- Perl modules are usually referenced as foo::bar (for example xml::parser). However, the package is called libfoo-bar-perl (libxml-parser-perl). Searching via apt-cache can help if this naming is a bit different, for example:
apt-cache search foo | grep bar apt-cache search xml | grep parser
- Python modules are named python-module, so the python mysql module would be python-mysqldb. The mapping is less consistent here and apt-cache will help a lot. Doing a search like
apt-cache search python | grep mysql
- usually helps in getting the required python module.
Ruby modules are named similar to the perl ones, for activesupport it is libactivesuport-ruby[1.8|1.9] (if no version is specified 1.8 is default). Also you may need to install the ruby package management platform rubygems, however, Debian/Ubuntu turns off some updating functionality to make rubygems work with apt. If you need/want up to date ruby software you should see RubyOnRails.
apt-cache search ruby | grep activesupport
Comments
For a newbie, you may consider CompilingEasyHowTo more user-friendly.
Also, You may find CheckInstall is helpful.
Shouldn't checkinstall be the default method described in this documentation? Is there any reason why someone new to compiling would not want to use it?
- I was so prudent to change the page to reflect that. I hope everyone can agree.
- Also, can't file-roller be used to unarchive the files? No one should have to memorize "-jxvf". Please recommend simpler easier-to-use tools whenever possible. Advanced users can find the command line shortcuts on a million other documents on the web. *This* document should just provide a clear method to get people started with an emphasis on Ubuntu and getting that obscure software package running easily.
Actually "tar xf file" is sufficient. GNU tar has (for some time) autodetected compression and worked around it. Possibly "tar xvf file" if the user wants verbose output.
- Also adding a gui step when the rest of compilation is done in a shell complicates matters.
- Is this guide for developers or users?