sodcalculator

10 Tech How-To

How to Compile Software Packages on Linux

Guide 10-28035 · Updated · Step by step

Compiling software from source on Linux gives you the latest features, custom optimizations, and full control over your installation. This guide walks you through the entire process, from installing build tools to running the final install command, so you can confidently build packages on any major distribution.

What You’ll Need

  • A Linux system with an internet connection
  • A terminal (Ctrl+Alt+T on most desktops)
  • Build tools: gcc, make, and build-essential (Debian/Ubuntu) or base-devel (Arch) or Development Tools group (Fedora/RHEL)
  • The source code archive (usually .tar.gz or .tar.xz) of the software you want to compile
  • Dependencies: libraries and headers required by the software (often listed in the README or INSTALL file)

Step-by-Step

  1. Install build tools. For Debian/Ubuntu, run sudo apt update && sudo apt install build-essential. For Fedora, use sudo dnf groupinstall "Development Tools". For Arch, run sudo pacman -S base-devel. These provide the compiler, linker, and make utility.
  2. Download the source code. Go to the official website of the software or its repository (e.g., GitHub) and download the source tarball. You can also use wget or curl directly in the terminal.
  3. Extract the archive. Use tar -xzf package.tar.gz for gzip archives or tar -xJf package.tar.xz for xz archives. This creates a new directory with the source files.
  4. Enter the source directory. Run cd package-directory to move into the extracted folder. Always check the contents first with ls to see what files are present.
  5. Read the documentation. Look for README, INSTALL, or BUILD files. These contain critical instructions specific to that software, such as additional dependencies or configuration options.
  6. Run ./configure. This script checks your system for required libraries, compilers, and settings. It creates a Makefile tailored to your system. You can pass options like --prefix=/usr/local to set the installation directory. If it fails, read the error message—it usually tells you which dependency is missing.
  7. Run make. This command compiles the source code into binary executables. The process can take a few minutes to an hour, depending on the software and your hardware. Use make -j$(nproc) to speed it up by using all CPU cores.
  8. Run make test (optional but recommended). Many packages include a test suite that verifies the build is correct. Run make test or make check to execute these tests.
  9. Install the software. Run sudo make install. This copies the compiled binaries, libraries, and documentation to the system directories (e.g., /usr/local/bin). This step requires root privileges.
  10. Update the library cache (if needed). If the software installs shared libraries, run sudo ldconfig to refresh the dynamic linker cache.
  11. Verify the installation. Run the command name in the terminal (e.g., package --version) to confirm it works. If you installed to a custom prefix, you may need to add that directory to your PATH.

Common Mistakes

  • Missing dependencies: The most common failure is a missing library or header file. The configure script will usually say what is missing. Install it with your package manager (e.g., sudo apt install libfoo-dev).
  • Using sudo for configure/make: Running ./configure or make as root is unnecessary and can cause permission issues. Only make install needs root.
  • Ignoring the prefix: Installing to the default prefix (/usr/local) is fine, but if you choose a custom prefix, remember to adjust your PATH and library paths.
  • Not running make clean after a failed build: If you change configuration options, run make clean before recompiling to remove old object files.
  • Forgetting to install -dev packages: On Debian-based systems, you need the -dev versions of libraries (e.g., libssl-dev) to compile against them.

Tips

  • Use checkinstall (Debian/Ubuntu) to create a .deb package from your compiled source, making it easier to uninstall later.
  • Keep a log of the configure options you used, so you can reproduce the build later.
  • For large projects, use make -j$(nproc) to significantly speed up compilation.
  • If the software has a Git repository, you can clone it and build the latest development version, but be aware it may be less stable.
  • Consider using a package manager that supports building from source, like makepkg on Arch, which automates the process.

Troubleshooting

Configure fails with “No such file or directory” – This usually means the configure script is not present or not executable. Check if the file exists with ls -l configure. If not, you may need to run autoreconf -i to generate it (requires autoconf).

Make fails with “command not found” – You are missing a compiler or utility. Install build-essential (Debian) or the equivalent on your distro.

Make fails with “undefined reference” errors – This indicates a missing library. The error message will name the symbol and often the library. Install the corresponding -dev package.

Configure says “C compiler cannot create executables” – Your compiler is broken or missing. Reinstall gcc and libc6-dev.

Install fails with “Permission denied” – You forgot sudo before make install.

Binary runs but says “cannot find shared libraries” – The library path is not set. Run sudo ldconfig or set LD_LIBRARY_PATH to include the install directory.

Frequently Asked Questions

Why compile from source instead of using the package manager?

Compiling gives you the latest version, custom compile-time options, and optimizations for your CPU. Package managers often provide older, generic builds.

How do I uninstall software installed from source?

If you kept the source directory, run sudo make uninstall from it. If not, you may need to manually delete files listed in install_manifest.txt (if generated) or use checkinstall to create a package.

Do I need to compile as root?

No. Only make install requires root. Compiling as root can cause permission issues and is a security risk.

What is the difference between make and make install?

make compiles the code into binaries in the source directory. make install copies those binaries and other files to system directories like /usr/local/bin.

How long does compilation take?

It varies widely: small utilities take seconds, while a full web browser or LibreOffice can take over an hour. Using -j$(nproc) helps.

Can I compile on any Linux distribution?

Yes, the process is the same, but the package names for build tools differ. Use your distribution’s package manager to install the equivalent of build-essential.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →