Skip to content

Linux Foundations / Manage a Linux host ยท Step 21 of 50

Lesson: Compile Source CodeSwitch
โ† Linux Foundations

System administration

Compile Source Code

Build and install a program from source with configure, make and make install.

Sometimes there is no package, or you need a newer version or a different build option. Then you can compile: turn the source code into a program yourself.

Get the tools

You need a compiler and make. Install the usual set with:

$ sudo apt install build-essential        # Debian, Ubuntu
$ sudo dnf install gcc make               # Fedora, RHEL

Projects often need libraries too, including their header files. Those packages usually end in -dev on Debian and -devel on Fedora.

Unpack and read

Get the source from the project's official site, and check the published checksum or signature if there is one.

$ tar -xf hello-2.12.tar.gz
$ cd hello-2.12
$ less README

Read README or INSTALL first. Not every project uses the steps below. Some use CMake, Meson or other build tools.

configure

Many projects ship a script named configure:

$ ./configure
checking for gcc... gcc
checking whether the C compiler works... yes
config.status: creating Makefile

It inspects your system, checks for requirements and writes a Makefile. If it stops with an error, a tool or library is usually missing. The ./ runs the script in the current directory.

To choose where the program will be installed:

$ ./configure --prefix="$HOME/.local"

make and make install

$ make
$ sudo make install

make reads the Makefile and builds the program inside the source directory. make install copies the results into place. The default for configure scripts of this kind is under /usr/local, which is why it needs sudo. With a prefix in your home directory, run make install without sudo.

Do not run ./configure or make as root. Building runs code from the project.

The cost

Your package manager does not know about files installed this way. It will not update them, so security fixes are your job. Some projects support:

$ sudo make uninstall

Keep the configured source directory in case you need it. Tools such as checkinstall can wrap an install in a package, but support varies.

Try it

Build a tiny program of your own. Save this as hi.c:

c
#include <stdio.h>
int main(void) { puts("hi"); return 0; }

Then run:

$ make hi
cc     hi.c   -o hi
$ ./hi
hi

make has a built-in rule for single C files, so no Makefile is needed here. For the packaged side of software, see the packages, disk and FHS lab.

Check your understanding

Compile Source Code

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.