Compiling Software from Source: A Complete Step-by-Step Guide
In this article
Compiling software from source is the process of converting human-readable source code into a machine-readable binary executable that your computer can run. This is typically done to get the absolute latest version of a program, to enable specific features not included in the standard installer, or to optimize the software for your specific CPU architecture. To do this, you need a compiler (like GCC or Clang) and a set of build tools that translate the code into a working application.
What You'll Need
- The Source Code: Usually downloaded as a compressed archive (.tar.gz, .zip) or cloned from a repository like GitHub using Git.
- A Compiler: A tool that translates code. For C/C++, GCC (GNU Compiler Collection) or Clang are the standards.
- Build Automation Tools: Tools like Make, CMake, or Ninja that manage the complex sequence of compilation steps.
- Development Headers: Libraries (often ending in -dev or -devel in package managers) that provide the necessary interfaces for the software to interact with your operating system.
- A Terminal/Command Line Interface: Access to Bash, Zsh, or PowerShell to execute the build commands.
Step-by-Step
- Install Build Essentials: Before touching the source code, you must install the toolchain. On Linux distributions, this is often a single package. For example, on Debian-based systems, you would install the "build-essential" package. On Fedora or CentOS, you would look for "Development Tools". This ensures you have the compiler, the make utility, and the standard C library.
- Acquire the Source Code: Download the software. If using a web browser, download the latest stable release archive. If using Git, run
git clone [repository-url]. Once downloaded, extract the archive using a tool liketar -xzvf filename.tar.gzor unzip it. Navigate into the resulting folder using thecdcommand. - Read the Documentation: Look for a file named
README,INSTALL, orCONTRIBUTING. This is the most critical step because every project is different. The documentation will tell you exactly which dependencies are required, which build system is used (Autotools, CMake, Meson, etc.), and if there are specific flags you need to set for your hardware. - Install Dependencies: If the README lists dependencies (such as
libssl-devorzlib), install them using your system's package manager. If you attempt to compile without these, the process will fail during the configuration stage with an error stating that a specific library was not found. - Configure the Build: This step checks your system for the required tools and creates a custom "Makefile" tailored to your environment.
- If the project uses Autotools, run
./configure. You can add options here, such as./configure --prefix=/usr/localto specify where the software should be installed. - If the project uses CMake, create a build directory to keep the source clean:
mkdir build && cd build, then runcmake ...
- If the project uses Autotools, run
- Compile the Code: Now that the Makefile is generated, tell the computer to start the translation process. The standard command is
make. To speed this up on multi-core processors, use the-jflag followed by the number of cores you have (e.g.,make -j8). This allows the compiler to process multiple files simultaneously. - Run Tests: Many projects include a test suite to ensure the compilation was successful and the program is stable. Look for a command like
make checkormake test. If the tests fail, do not proceed to installation, as the software may crash or behave unpredictably. - Install the Binary: Once the compilation is complete and tests pass, move the resulting binary files into the system folders where the OS can find them. This usually requires administrative privileges. Run
sudo make install. If you used a prefix during configuration, the files will be placed in that specific directory rather than the system root.
Common Mistakes
One of the most frequent errors is skipping the dependency installation. Users often try to run ./configure or cmake immediately after downloading the code, only to be met with a wall of "Library not found" errors. Always scan the README for a list of required packages first.
Another common mistake is compiling as the root user. You should perform the configuration and compilation steps as a regular user and only use sudo for the final make install step. Compiling as root can lead to permission issues with the generated object files and poses a security risk if the build script contains malicious code.
Forgetting to create a separate build directory when using CMake is a frequent annoyance. If you run cmake . in the root source folder, it litters the source code with temporary build files, making it very difficult to clean the project or switch versions without deleting the entire folder and starting over.
Tips
To keep your system clean, consider installing software to the /opt directory or a folder in your home directory (like ~/.local) instead of /usr/bin. This prevents your compiled software from clashing with packages managed by your system's official package manager, making it much easier to delete the software later.
If you are compiling a large project that takes hours, use a tool like ccache. Ccache caches previous compilations and will significantly speed up the process if you need to recompile the software after making a small change to the code.
Always check the version of the compiler you are using. Some modern software requires a specific version of C++ (like C++17 or C++20). If your system compiler is too old, the build will fail with syntax errors. You may need to install a newer version of GCC or Clang to proceed.
Troubleshooting
If the process fails, the first thing to do is scroll up through the terminal output to find the first instance of the word "Error". The most recent error is often just a consequence of a previous failure. The first error is the root cause.
If you see a "Permission Denied" error during the make install phase, ensure you are using sudo. If you see a "Command not found" error for make or cmake, return to the "What You'll Need" section and ensure the build tools are actually installed on your system.
If the configuration fails due to a missing library, search your package manager for the library name followed by "-dev". For example, if the error says libpng not found, search for libpng-dev. Installing the runtime library is not enough; the compiler needs the header files provided by the development package to understand how to link the code.
If the software compiles but fails to run with a "shared library error" (e.g., error while loading shared libraries: libexample.so.1), you may need to update your system's library cache. Run sudo ldconfig to refresh the links to the newly installed libraries.
Frequently Asked Questions
Why compile from source instead of using a package manager?
Compiling from source allows you to use the absolute latest version of the software before it is packaged for your distribution. It also lets you toggle specific features on or off via configuration flags and optimizes the binary for your specific CPU instructions, which can improve performance.
Is it safe to compile software from the internet?
It is only as safe as the source of the code. Compiling software allows you to inspect the code before running it, but unless you are an experienced programmer, this is difficult. Only compile software from reputable developers or well-known open-source projects.
What is the difference between make and cmake?
Make is a build tool that executes a set of instructions found in a "Makefile". CMake is a build-generator; it doesn't compile the code itself, but it looks at your system and creates a Makefile (or a Visual Studio project) that Make or other tools can then use.
Do I have to compile every time I want to update?
Yes, if you are building from source, you must download the new source code and repeat the build process to update. However, if you used Git, you can simply run git pull to get the latest changes and then run make again; the build system will only recompile the files that have changed.
Can I uninstall software that I compiled from source?
If the developer included an uninstall target, you can run sudo make uninstall from the build directory. If that doesn't work, you will have to manually delete the files installed to the system, which is why installing to a specific prefix like /opt/software-name is recommended.