Compiling htop on Ubuntu 24.04


Why compile from source?

  • Access latest features or bugfixes
  • Test development branches
  • Customize build options
  • Avoid distro packaging delays

Compiling from source can be a daunting task for users and administrators, but it doesn’t have to be. It’s funny, for all the problems Ubuntu has with its package management, compiling from source is not that bad. I will show you a general process for compiling packages for Ubuntu which is straightforward. You don’t need to be a developer to compile code!

TL;DR

To compile htop on Ubuntu 24.04:

sudo sed -i 's/^Types: deb$/Types: deb deb-src/' /etc/apt/sources.list.d/ubuntu.sources
sudo apt update
sudo apt build-dep htop
mkdir -p ~/src/htop && cd ~/src/htop
curl -LO https://github.com/htop-dev/htop/releases/download/3.4.1/htop-3.4.1.tar.xz
tar xvf htop-3.4.1.tar.xz && cd htop-3.4.1
./configure --prefix=/usr/local # or --prefix=$HOME/.local
make -j $(nproc)
sudo make install
which htop # verify installation
sudo make uninstall # to uninstall

The Build Pipeline

The general build pipeline is as follows

autogen.sh           # if no "configure" script present
configure            # configure the build
make -j $(nproc)     # compile source code
sudo make install    # install resulting binary
sudo make uninstall  # if supported, uninstall 

⚠️ Warning: Never run configure or make as root. make install may require using sudo, unless using prefix e.g. ./configure --prefix=$HOME/.local

autogen.sh

Only run this if there is no configure script present. For example, if we git clone the repo, we see there is no configure script.

builduser@ubuntu-s-2vcpu-2gb-nyc1-01:~$ git clone https://github.com/htop-dev/htop.git
Cloning into 'htop'...
remote: Enumerating objects: 20188, done.
remote: Counting objects: 100% (20/20), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 20188 (delta 11), reused 7 (delta 6), pack-reused 20168 (from 2)
Receiving objects: 100% (20188/20188), 6.43 MiB | 14.93 MiB/s, done.
Resolving deltas: 100% (15640/15640), done.
builduser@ubuntu-s-2vcpu-2gb-nyc1-01:~$ cd htop/
builduser@ubuntu-s-2vcpu-2gb-nyc1-01:~/htop$ ls configure
ls: cannot access 'configure': No such file or directory

But there is a script named autogen.sh, which we must first run. This runs autotools which generates the configure script using Autotools components like aclocal, autoconf, and automake.

builduser@ubuntu-s-2vcpu-2gb-nyc1-01:~/htop$ ./autogen.sh 
autoreconf: export WARNINGS=all
autoreconf: Entering directory '.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force 
autoreconf: configure.ac: tracing
autoreconf: configure.ac: creating directory build-aux
autoreconf: configure.ac: not using Libtool
autoreconf: configure.ac: not using Intltool
autoreconf: configure.ac: not using Gtkdoc
autoreconf: running: /usr/bin/autoconf --force
autoreconf: running: /usr/bin/autoheader --force
autoreconf: running: automake --add-missing --copy --force-missing
configure.ac:69: installing 'build-aux/compile'
configure.ac:23: installing 'build-aux/config.guess'
configure.ac:23: installing 'build-aux/config.sub'
configure.ac:24: installing 'build-aux/install-sh'
configure.ac:24: installing 'build-aux/missing'
Makefile.am: installing './INSTALL'
Makefile.am: installing 'build-aux/depcomp'
autoreconf: Leaving directory '.'
builduser@ubuntu-s-2vcpu-2gb-nyc1-01:~/htop$ ls configure
configure

For release versions, such as those found on the htop release page, the release tarball usually includes a pre-generated configure script. So we don’t need to run autogen.sh if building a release tarball. Only if we clone the raw git repo.

configure

The system we compile on is called the Build System. The system where the compiled binary will run is the Host System. In most cases (like compiling htop for your own system), they are the same. However, the two don’t need to be the same OS, or even the same architecture. configure is a tool provided by autoconf used to gather various information about the build and target systems to feed into the Makefile.

The --prefix option sets where the compiled tool will be installed to. If we only want our user to use the tool, we can set --prefix=$HOME/.local. If we want to make the tool available to everyone on the system, we can set --prefix=/usr/local. This makes it so we don’t accidentally overwrite system binaries in /usr

If ./configure fails, inspect config.log in the same directory to troubleshoot missing dependencies or environment issues.

make

This command reads the contents of Makefile and executes the specified compiler (usually GCC) to compile the source code. The -j $(nproc) option tells make to run one job per CPU core in parallel, speeding up the build process.

make install will install the resulting binary in the directory specified by --prefix earlier.

Some tools support uninstallation via make uninstall.

If you want to start over and reconfigure/rebuild, you can clean out all the resulting files and configs from the build process.

make clean      # removes compiled objects
make distclean  # removes configure and Makefile if generated by autogen.sh

Compiling htop

htop is a great package to practice compiling with. It is a useful utility, relatively small, and safe to compile since it doesn’t risk destabilizing the system.

(Optional) run apt show htop to get packaged htop info. This is the package we would get by running apt install htop. This can provide useful information, but is not necessary. Example output:

builduser@ubuntu-s-2vcpu-2gb-nyc1-01:~$ apt show htop
Package: htop
Version: 3.3.0-4build1
Priority: optional
Section: utils
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]m>
Original-Maintainer: Daniel Lange <[email protected]g>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 434 kB
Depends: libc6 (>= 2.38), libncursesw6 (>= 6), libnl-3-200 (>= 3.2.7), libnl-genl-3-200 (>= 3.2.7), libtinfo6 (>= 6)
Suggests: lm-sensors, lsof, strace
Homepage: https://htop.dev/
Task: cloud-image, cloud-image, server, ubuntu-server-raspi, lubuntu-desktop, ubuntu-mate-core, ubuntu-mate-desktop, ubuntu-budgie-desktop-minimal, ubuntu-budgie-desktop, ubuntu-budgie-desktop-raspi
Download-Size: 171 kB
APT-Manual-Installed: no
APT-Sources: http://mirrors.digitalocean.com/ubuntu noble/main amd64 Packages
Description: interactive processes viewer
 Htop is an ncursed-based process viewer similar to top, but it
 allows one to scroll the list vertically and horizontally to see
 all processes and their full command lines.
 .
 Tasks related to processes (killing, renicing) can be done without
 entering their PIDs.

Create Build User

useradd -m -G sudo -s /bin/bash builduser
passwd builduser
su - builduser

From this point on, you should run as this user.

Install Build Dependencies

Enable Ubuntu to read from source code repositories.

sudo sed -i 's/^Types: deb$/Types: deb deb-src/' /etc/apt/sources.list.d/ubuntu.sources
sudo apt update

Install build dependencies. This installs the build dependencies for the packaged version of htop (e.g., 3.3.0), but newer releases (e.g., 3.4.1) may require additional or updated libraries not listed in the packaging metadata.

sudo apt build-dep htop

Download release source code. As of this writing, the latest release was 3.4.1. We should compile inside ~/src/htop/ to avoid overwriting any system files by mistake.

mkdir -p ~/src/htop
cd ~/src/htop
curl -LO https://github.com/htop-dev/htop/releases/download/3.4.1/htop-3.4.1.tar.xz
tar xvf htop-3.4.1.tar.xz
cd htop-3.4.1

Build Options

See all available build options

./configure --help

What options should we use? You can get the exact build options, as well as any dependencies by looking at the build logs (although you may need to do some tweaking).

Go to the launchpad build page for htop.

Find the version you want to build. For this example 3.4.1. Note: it is still under active development, which means it is not suitable for production systems!

htop launchpad build page

Find the CPU architecture of the target system.

💡 Note: amd64 and x86_64 both refer to the same CPU architecture. You can find your CPU architecture on linux by running uname -a

Note that the build status was successful. This is really important! Finally, click on the build logs.

htop launchpad build status

You should see build logs that look like this.

Here, we can get details about the build system that the package maintainers used when they compiled htop. This is vital information, since we can compare to our own environment. Not only can we see the build dependencies they used, but also the options they enabled when running configure.

CTRL+F and search for ”./configure” to find the build options. You should see something like this. Note, I’ve changed --prefix=/usr/local (which is usually the default directory). This is where htop will be installed to. I’ve done this to avoid overwriting or clobbering system files.

./configure \
	--build=x86_64-linux-gnu \
	--prefix=/usr/local \
	--includedir=\${prefix}/include \
	--mandir=\${prefix}/share/man \
	--infodir=\${prefix}/share/info \
	--sysconfdir=\${prefix}/etc \
	--localstatedir=\${prefix}/var \
	--disable-option-checking \
	--disable-silent-rules \
	--libdir=\${prefix}/lib/x86_64-linux-gnu \
	--runstatedir=/run \
	--disable-maintainer-mode \
	--disable-dependency-tracking \
	--enable-openvz \
	--enable-vserver \
	--enable-unicode \
	--enable-affinity \
	--enable-delayacct \
	--enable-sensors

Compiling and Installing

make -j $(nproc)

At this point, we could simply run from inside the current directory

builduser@ubuntu-s-2vcpu-2gb-nyc1-01:~/src/htop/htop-3.4.1$ ./htop --version
htop 3.4.1-3.4.1

If we want to install to the system, so other users can use it, just run

sudo make install

Verify we are using the compiled htop

builduser@ubuntu-s-2vcpu-2gb-nyc1-01:~/src/htop/htop-3.4.1$ which htop
/usr/local/bin/htop

Uninstalling

Some tools support uninstalling using make uninstall. For example, if we want to uninstall our compiled htop we can do so like this

builduser@ubuntu-s-2vcpu-2gb-nyc1-01:~/src/htop/htop-3.4.1$ sudo make uninstall
 ( cd '/usr/local/share/icons/hicolor/scalable/apps' && rm -f htop.svg )
 ( cd '/usr/local/share/applications' && rm -f htop.desktop )
 ( cd '/usr/local/bin' && rm -f htop )
 ( cd '/usr/local/share/man/man1' && rm -f htop.1 )
 ( cd '/usr/local/share/pixmaps' && rm -f htop.png )
builduser@ubuntu-s-2vcpu-2gb-nyc1-01:~/src/htop/htop-3.4.1$ which htop
/usr/bin/htop

We are now using the default packaged htop.

Further reading:

GNU Autotools Manual

Using checkinstall to safely manage compiled software

GNU Stow for managing local installations