Compiling from Source

Sometimes the software you need isn't packaged. Or you need a specific version. Or you want custom compile options. Time to build from source.

When to Compile from Source

  • Software not in any repository
  • Need latest version (packages lag behind)
  • Need specific compile-time options
  • Learning how software is built
  • Contributing to open source

Prefer Packages When Possible

Compiling means no automatic updates, harder uninstalls, and potential dependency conflicts. Only compile when you have a good reason.

Install Build Tools

Terminal
$sudo apt install build-essential
Installing: gcc g++ make dpkg-dev...

build-essential includes:

  • gcc - C compiler
  • g++ - C++ compiler
  • make - Build automation
  • Development libraries

The Classic Pattern

Most source code follows this pattern:

Terminal
$./configure
(checks your system, sets options)
$make
(compiles the code)
$sudo make install
(installs to /usr/local)

Real Example: htop from Source

Terminal
$# Get the source
$git clone https://github.com/htop-dev/htop.git
Cloning into 'htop'...
$cd htop
$
$# Install dependencies
$sudo apt install libncursesw5-dev autotools-dev autoconf automake
$
$# Generate configure script
$./autogen.sh
$
$# Configure
$./configure
checking for gcc... gcc checking for ncurses... yes ... config.status: creating Makefile
$
$# Compile
$make
gcc -c -o htop.o htop.c ...
$
$# Install
$sudo make install
install -m 755 htop /usr/local/bin/htop

Understanding ./configure

configure checks your system and creates a Makefile. Common options:

Terminal
$./configure --help
(shows all options)
$
$# Common options:
$./configure --prefix=/opt/myapp
(install somewhere other than /usr/local)
$./configure --enable-feature
(turn on optional features)
$./configure --disable-feature
(turn off features)
$./configure --with-library=/path
(specify library location)

Missing Dependencies

Configure fails? Usually missing a library:

Terminal
$./configure
checking for libssl... no configure: error: OpenSSL library not found
$
$# Install the dev package
$sudo apt install libssl-dev
$
$# Try again
$./configure
checking for libssl... yes

Dev Packages

Libraries have two packages: libfoo (runtime) and libfoo-dev (headers for compiling). You need the -dev package.

Uninstalling

If you kept the source directory:

Terminal
$cd htop
$sudo make uninstall
(removes installed files)

If you didn't keep it... manually delete from /usr/local/.

Alternative: checkinstall

checkinstall creates a .deb package from your compiled software:

Terminal
$sudo apt install checkinstall
$
$# Instead of 'make install':
$sudo checkinstall
Creating package... Done! Installing package... Done!
$
$# Now you can uninstall properly:
$sudo apt remove htop
Knowledge Check

What command checks your system and creates a Makefile before compiling?

Quick Reference

StepCommandPurpose
1./configureCheck system, create Makefile
2makeCompile source code
3sudo make installInstall to system
4sudo make uninstallRemove (if available)

Key Takeaways

  • ./configure && make && sudo make install is the classic pattern
  • Install build-essential for compilers
  • -dev packages provide headers for compiling
  • Configure fails? Install missing dev packages
  • Use checkinstall for cleaner uninstalls
  • Prefer packages when available

Congratulations! You've completed Chapter 10: Package Management.

Next chapter: Text Editors - editing files from the command line.