A beginners howto to compiling from source on Linux systems
When dealing with Linux/Unix systems, you will, sooner or later have to deal with source compilation. Some call it jokingly a rite of passage, others call it necessary knowledge. Whatever way you look at it, it's good-to-have. We will presume that you have little to no knowledge of how to use the command line or what a compiler is. So before we begin we will help you by listing a handful of tools you will use when living with the command line, plus the specific tools necessary when compiling source code. This tutorial is intended to be as practical as possible, so if you want to read about compiler theory or the steps necessary to get from the source code to a binary, use the documentation proposed below. If you still have some questions after reading this article please try our new LinuxCareer Forum.
1. Preliminary concepts and necessary tools
We will start from the supposition that this is your first time you work at the command line. We will outline the basics here, but a Linux beginner's book or online resource will help you a lot. If you use GNOME, start gnome-terminal by issuing Alt+F2 and typing the name, or if you use KDE, Alt+F2 and 'konsole'. There are plenty of terminal emulators out there, so just choose one : for the moment it isn't important what you use. You'll see a prompt with a blinking line waiting for you to type something. Linux/Unix commands are short, as in made of just a few characters, and this is for a historical reason. In the days of old, Unix terminals had such a poor speed in terms of data transfer that any character typed was carefully thought out. Of course, those days are gone, but the commands remain. We recommend you to make a habit out of reading the manual pages associated with the commands. For example, 'ls' is the command for LiSting a directory's contents and 'man ls' tells you about the options available. As we said, the commands are usually short, but intuitive. You have the base commands already installed on any Linux/Unix system, since they're pretty essential. Now, type
$ ls -a
in your terminal. Usually the options given to programs are also intuitive and smart, so the above command will LiSt All files in the current folder, including dot files (files that have a name starting with a dot, usually configuration files). Speaking of which, you want to see what directory you're in? Simple, type 'pwd', as in Print Working Directory. To return to your home folder (that is, your user's folder), simply type 'cd', which stands for Change Directory. If you use cd with a location argument, it will take you to the path you provided as an argument, as in 'cd /usr/'. Usually these basic applications reside in the /bin/ folder, so with the commands explained above, see what they are and read about each one to see what they do. Don't be afraid to play around and try out things : we know from experience that it's the best way to learn something.
Provided you took the time to get accustomed a bit with the basic Linux utilities, let's see how things are with permissions. It's a very important issue, and since you want to install software from source, you will need the proper permissions to do so, if installing system-wide. That is because a regular user (we very much hope you are reading this logged in as a regular user, not as root!) can't make system-wide changes. Unix systems, and Linux after them, take pride in a very security-conscious design, and that's how it should be. On the more practical side, if you are on an Ubuntu system, running
$ sudo su -
in a terminal takes you to a root prompt, which you will recognize as it starts with a hash (#). On other systems, running 'su-' will also take you to a root prompt, but it will ask you for the root password instead of YOUR password, as it happens in Ubuntu. We recommend great caution, though. The root user is the $god of your system : it can do anything, including damaging the system beyond repair. Take advice given to you on IRC or forums with a grain of salt and, if in doubt, try to get another opinion on the matter. If you're not the administrator of the system, ask him/her about site policy regarding users and privileges. The Arch Linux wiki has a nice introduction on users and groups. The basic idea is that you need to be root in order to install to a global location, because your user does not have write permissions to the usual folders where software is installed on your system.
2. Compiling from source
After this short introduction, which is by no means exhaustive, we have to install development tools in order to compile what we desire. This part is distribution-dependent, so we will outline the installation procedure on few popular distros. If you are on Debian/Mint/Ubuntu or other Debian-based distro, you will need to type
# apt-get install build-essential
to install the necessary tools. In Red Hat or Fedora, you must type
# yum groupinstall "Development Tools"
to achieve the same result. OpenSUSE's zypper takes care of package and group installation, so install the devel_basis and devel_C_C++ patterns to get what you need. We cannot cover all the distributions and their package managers, so if you are using something different, a short Google search of the form '$distribution install development tools' will surely help. Test if you have what you need by typing
$ gcc -v && make -v
and if you see something like what appears on my Debian testing system (output may vary), you're set :
gcc -v && make -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.1-4' --with-bugurl=file enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 multiarch-defaults=x86_64-linux-gnu --enable-linker-build-id --with-system-zlib included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib/x86_64-linux-gnu
--enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.6.1 (Debian 4.6.1-4) GNU Make 3.81 Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This program built for x86_64-pc-linux-gnu
Now, let's say you want to install tcsh from source, because your distribution doesn't have it in its' repositories or simply because you want to. Tcsh stands for Tenex C Shell and we chose because we like it and because it's small and doesn't have lots of dependencies. The shell is a command interpreter : it takes the commands you give to it and takes them to the operating system, so you may think of it as a middle man. Now, all of tcsh's dependencies are probably already installed on your system, so you needn't worry. If you get errors when compiling from source like 'ERROR : $file not found' or something similar, just copy that line and paste it to a search engine : you will find out in a minute what went wrong and how to fix it, that is, what you need to install. So, we set up a folder in our home directory with
cd; mkdir source
(you of course know that mkdir stands for MaKe DIRectory, right? :) and cd to it. Then we point our browser here to get the latest tcsh and choose to download it to our newly created source directory. When we go back to the terminal and ls inside the source folder, we will see a file named something like tcsh-6.17.00.tar.gz . What we understand from this is that it's a gzip archive so we must unpack it to use the source.
$ tar xzvf tcsh-6.17.00.tar.gz
does that, but let's stop a little to explain about archives. First, please peruse the tar man page. In the meantime, we will explain what the above command does. You see that the 'xzvf' string, which should represent the tar options (and it actually does), does not begin with a dash, as in 'ls -a' below. There are applications that accept such options, that is, grouped and dash-less. This grouped option is equivalent to '-x -z -v -f', but we'll let you decide which one is easier to type. These options, also known as flags, stand for "eXtract a gnuZip archive Verbosely. it's a File." .
Now we cd into the unpacked directory and start the work. Nowadays, compilation has been made very simple, especially in comparison to the ancient days of yore. We have a script, called configure, which checks that your system is compatible with the build requests and you have everything you need installed, which then generates a makefile. Makefiles are used by the 'make' program to automate the build, to check if the source was modified since the last compile and all sorts of things that make your life (and developers') more comfortable. If make finds a file in the current directory called GNUMakefile or makefile or Makefile, in that order, it proceeds with building the source. Afterwards, typing 'make install' as root installs the binaries in the folders chosen as arguments to 'configure'.
Yes, it's that simple. Three commands and, if all is good, you're set. So, let's see what options we have before compiling tcsh.
$ ./configure --help
Read carefully what's written there, since every option is thoroughly explained, but if you do not need to change an option, don't, at least for now. Using what's called a pager you can get some more out of long outputs : './configure --help | less' pipes the configure script to the less pager, which doesn't send you a message on your beeper, it lets you read long output page by page. Use space or PgDn to scroll, and 'q' to Quit.
Now, we assume that you need not change the default options for tcsh, so we type simply
and, as we can see, after checking our system for necessary files and configuration variables, the last lines look like
checking for sysconf... yes checking for wcwidth... yes checking whether getpgrp requires zero arguments... yes checking whether mbrtowc and mbstate_t are properly declared... yes checking whether setpgrp takes no argument... yes checking for working strcoll... yes configure: creating ./config.status config.status: creating Makefile config.status: creating config.h config.status: executing ./atconfig commands
so the Makefile has been created, as underlined above. Now we can bravely type 'make' and wait for the compilation to take place. Since tcsh is a small piece of software, you won't have to wait long, so now you can finally install it. As you've seen, the default installation place is /usr/local, which is a good choice since we want to isolate hand-compiled packages from the ones our distribution provides, which usually go to /usr. So...
# make install
and there you have it! Your first hand-compiled package is ready to be used!
As an ending note, you are warned : do not download and compile source from dubious sources ("Hey, I'm testing this new software! It's way cool! Wanna try it?"). If you compile large packages and disk space is a concern, type
$ make clean
in the source directory after you're done in order to remove unneeded files that result from compilation. As stated, try your hand with smaller packages first, to get your feet wet and then move on to larger ones. Most of the time, developers state on their page something like "You will need x and y and z to compile this piece of software", so listen to their advice, as it will spare you of Googling for missing files.
That said, good luck and, as we promised, here are some links to help you further :
3. Further reading
- The GCC page
- About installing from source; older, but accurate
- Collection of links about Linux basics
Maybe you feel like we repeat ourselves, but it's the best advice you had today : one learns Linux/Unix when one uses Linux/Unix. Simple as that. Enjoy.