In this lesson,
you're going to learn how to build software
from its source code.
This is a critical skill
for any Linux system administrator to learn.
One of the great things about most Linux distributions
is the fact that they have a package manager
that allows you to download software
from a repository very easily.
With these package managers,
you have the ability to download
and install new software on your computer
in a completely automated manner.
But despite all their efforts,
the package maintainers simply can't handle
each and every use case,
nor can they carry every single package
for every piece of software that might be out there.
And so there are some situations
where you're going to need to compile
and install new software by yourself.
Sometimes this is because you want to run
a specific version of a piece of software,
or because you want to modify the source code
and use your own version that you're creating.
And in both of these cases,
you're going to have to download the original source code,
make those modifications you want,
and then compile it yourself.
Now compiling software
means you're taking the text based source code
and compiling it down into binary code
that your computer can understand and actually run.
Now because of this,
you're going to be able to modify that software
when it's in a text based format
but once you compile it into a binary,
it no longer can have any changes made to it
until you compile a new version.
Now, compiling software manually
does not use a management tool
like the package software does.
Now as you know, computers love binary.
They like ones and zeros,
but we as humans, we don't like that as much.
We would rather communicate with words.
So when I'm creating a program,
I'm going to use a programming language.
Now a programming language
will allow me to type in things
in a human readable format
and then later on, when I'm ready to,
I can compile that down into the ones and zeros
that my computer can understand.
This is the process of compiling,
going from source code written by a programmer
into this final binary software that the computer can run.
Now, this is all done
by using compilation software known as a compiler.
Compilers are able to translate the source code
that's written in that programming language,
such as C or C++, into a machine readable binary.
Now on Linux, there is a compiler
built in by default known as GCC,
which is the new compiler collection.
This is the GCC utility
and it has the ability to support different libraries
for you to be able to compile software
in different languages.
Typically, software developers are going to provide a list
of all the necessary libraries
or dependencies for that application source code.
And depending on the language you're using,
this is either going to be inside of a header file
known as a .h file, or a library file
which has a .a file extension.
Now program libraries are going to be chunked together
with all these different pieces of compiled code
that can then be used in the programs
to accomplish specific tasks.
These shared libraries enable more modular programming
and allows us to reduce time when we're writing programs
and when we're compiling them.
Now, these are typically going to be included
with most Linux distributions
and they're placed in the /usr/lib/
or /user/lib directory.
This means anyone in the system has access to it
and can call those libraries.
There's also some other libraries
in the /lib directory or /library directory
and these are used by essential binaries
such as the kernel and pieces of the operating system.
Now, if you're trying to determine
what libraries are being used by a given application,
you can use the ldd command.
The ldd command enables the user
to view the shared library dependencies for any application.
And this is useful when you're troubleshooting
or gathering information
about different system requirements
for an application.
To run the ldd command,
simply type in ldd options,
and the program binary that you want to check.
Now, as a system administrator,
you're going to download your software
but now you have to build that software
and this is what we call software compilation.
To do this, you're going to download the file,
you're going to decompress it,
and now you're going to have that code ready to go.
So when you download the code,
you're going to download it usually as a tar.gzip file.
Once you have that,
you're going to decompress the gzip part of the file,
and then you're going to untar the tar ball
so you have access to all the underlying files.
Now in this archive of files,
you're going to find a file called the make file.
The make file contains system information
that is going to be able to tell the system
how to compile this binary
for your particular system and your options.
To be able to configure this file properly,
you should run ./configure as a command
to gather information about your system
that may be needed for that application.
Now, inside of this make file,
you can make the modifications
to optimize the software for your particular needs
or your particular systems' needs.
Now, once you've done that,
you can then use the make command
to compile the application
using the information stored in that make file
and then you're going to type in make install
and hit enter to install the resulting binary.
So as you can see, this is a three step process.
./configure gets the information about your system.
Make is going to make that thing
and compile the application and get it ready
and then make install is going to install
that resulting binary onto your system.
Now, in most cases, once you have that make file created,
you can simply issue the make
and then the make install command
without any special arguments to install the application.
This is because the make command
and the make install sub command,
automatically look for the make file
in the current directory that you're in.
However, you can't issue various options if you need to.
This make file is a file that contains instructions
that are used by your compiler
to build the program from the source code.
These instructions inside the make file
define the resources that the program is going to depend on
in order to function properly,
as well as any additional directives
as defined by the developer.
For example,
let's say I'm creating an executable program called Dion
and it relies on two object files,
namely dionmain.o and dionfunction.o.
Now what I would need to do first
is be able to create those object files
and I create those
from the C programming language from my source code.
So I would first compile those objects
to their binary format
by entering gcc -c dionmain.c and hitting enter.
This will create the Dion main object file.
Then I would enter gcc -c dionfunction.c
to compile that into the binary file of dionfunction.o.
Notice both of these end with a .o extension
because they're compiled binary object files now.
Now, if I want to link those back to my program,
I can do that using gcc -o dion,
which is the name of my program,
and then the object files I want to link,
dionmain.o and dionfunction.o.
This allows Dion to run
by relying on those two linked objects.
Now, why would we use this approach
instead of creating everything
in a single C file and compiling it?
Well, we do this because it allows us to make changes
to each individual object or library
without affecting the overall program dramatically.
This also makes it easier for us to update things
or troubleshoot things when things go wrong.
And so we want to be able to do that
by going through and compiling these things individually.
Now, if I make a change
to one of these underlying object files,
I would then just need to use gcc -c
and the name of that code, such as dionfunction.c,
to be able to recompile that back to an object file.
Now, whenever you go to install a program
and you use the make command,
it's going to be able to rebuild the program completely
based on all those object files
that have already been compiled
and linking them all together into the finished product
that you're going to be able to use on your system.