Linux: Compiling software from source code
Hi, everybody!
Since last week, i’m thinking about making a post on here about running apache web server from source code on Linux (but nothing about APT, YUM, DNF, or any other package manager tool), i want to compile and integrate it to systemd (so you can do all that fancy management with systemctl). But, in this case, one of the things we will have to do is compile some programs (apr, apr-util, pcre and apache itself) from source code. So, to avoid making a big post explaining everything, I decided to make a first post about compiling programs from source code (and how to do it the correct way).
First off, the program that we will be compiling from source code is ‘htop’, which is a program that gives information about the system itself (cpu and memory usage, load average, monitoring the processes that are running on the system, and all of it in real time). For what we will be doing it really doesn’t matter what the program do, but i wanted to give some context.
Where can we get htop’s source code? We will be looking into it’s github repo (https://github.com/htop-dev/htop), and we will download it through git command (if you don’t have it, you should either download htop source code through github web page or install git, it’s up to you, but i will be using git command).
To start, download the repo and put it at /usr/src — this is a standard directory where administrators can put the source code from any program that they want to compile. Now, you can ‘cd’ into the directory that was created in /usr/src, which will be /usr/src/htop.
From there, we will be using a shell script called ‘configure’, which is a script used to prepare the program to be compiled (it will look for necessary binaries for it’s compilation as well as general information about the system itself and verify if all necessary libraries are available). What you can now realize is that /usr/src/htop/ directory DOESN’T have this ‘configure’ file (most source codes will have this file prepared right away after you have download it). As an exception, htop doesn’t have it, but you CAN generate it through another script called ‘autogen.sh’. So, you know what you have to do: run the script /usr/src/htop/autogen.sh, and after that you will have the ‘configure’ file prepared for you to be used.
From now on, it’s pretty simple, you should run the /usr/src/htop/configure script, but in case you want to easily remove it in the future, you should tell this script WHERE you want it to install the binaries and possible configuration and man page files, and you tell this to the configure script using –prefix flag. You should tell the script to install in /usr/local/htop (in case you want to remove, you basically remove this directory)
So, you will run configure file like this: /usr/src/htop/configure –prefix=/usr/local/htop.
After this, you will have almost everything set it up. Now, it’s time to compile it, and for that you just run the command “make” with no flags or parameters (you should do this inside the /usr/src/htop directory).
Get Hanlly S.’s stories in your inbox
Join Medium for free to get updates from this writer.
Now, everything is compiled and you should just install it in the right place (/usr/local/htop or the place you specified with –prefix FLAG). To do this, you should run the command “make install”. This last part is pretty fast, now you have installed htop in you system, pretty cool, right?
To test it, run the command “htop” in your shell. It didn’t work, did it? Well, it’s pretty simple, the reason it didn’t work it because the PATH variable doesn’t include the path where the htop binary is found, which is /usr/local/htop/bin. We will include it editing the PATH variable.
PATH=”${PATH}:/usr/local/htop/bin”
running the ‘htop’ command, IT WORKED. By the way, so you don’t have to edit the PATH variable every time you log into the computer, you should add this PATH variable edited in the /etc/profile and /etc/bash.bashrc.
In case you want to know more about htop command, you most likely will use
man htop
Probably, you ended up with no results, that’s is because man command tries to find man pages in /usr/share/man/{man1,man2,man3,…,man8}, and you didn’t add any man pages there for htop… YET.
To finish it up, that’s what we will be doing, if you look into /usr/local/htop/share/man, you will see a single directory called “man1”, where you will find htop’s man page type 1. We will copy it to the /usr/share/man/man1/ directory.
cp /usr/local/htop/share/man/man1/* /usr/share/man/man1
now… running “man htop”, you can read more about the htop command.
That’s it. I hopy everyone liked it, and see you guys next time (I’m going to try to make posts more frequently, but no promises).




