Compiling and installing programs from source. I want to take it and shoot it, or an educational program on why you should not use make install Installing a new kernel

Hi all!

This is a small note for Linux beginners about what these three great commands mean and what they are for. Let's start, as they say, from the beginning. Most programs must be compiled before use, that is, converted from human-readable text into a set of ones and zeros that a computer can understand. The process is conditionally divided into three stages: configuration (configure), assembly (make) and installation ( make install). Details under the cut 🙂

./configure

This command searches for libraries and header files necessary for compilation (this is for programs partially or completely written in C / C ++ and similar languages), as well as setting special parameters or connecting special libraries, if ./configure will find everything he needs, he will create Makefiles- the file needed to build the program

You can configure the configurator parameters using the keys and arguments of these same keys, for example:

./configure --prefix=/opt/my_program

With a key --prefix= You can specify a directory that will later act as a prefix for your program (that is, the root directory). This is due to the fact that in Linux world and not only, there is a special Hierarchy of the File System (HFS) according to which any program, in order to work without errors, must be compiled and installed.

AT file system There are three main prefixes that most programs are configured against, namely:

  • / - the root directory of the operating system, the so-called ROOT
  • /usr - directory where user environment applications are located
  • /usr/local - an additional directory for manually compiled user programs, specifically for operating system did not turn into a dump

If you open any of these directories, you can see a very similar structure, at least there will be folders: bin, etc, include, libs, sbin.

If run ./configure without keys, then the default prefix (the directory where the compiled program will be installed) will be /usr/local, remember this if you cannot run your program, you may not have a path in PATH.

Except the key --prefix in the configurator, as a rule, there are many other keys, you can see them all if you run:

./configure --help

make

The most important and simple command/program that starts the application compilation procedure from the source code. For your work this program uses special files Makefiles, which describe in detail the process of building the application with all the parameters that we specified to the configurator. The result of successful execution of the make command will be the compiled program in the current directory.

make install

This command performs a direct installation of the application to the directory specified at the configuration stage, after executing the make install command, you can run the newly installed program.

Afterword

In order not to write three commands in turn, you can write them in one line:

./configure && make && make install

&& is an AND operator that came from the C/C++ language, however, from the shell point of view, it means that the next command should be executed only if the previous command was successful, this is very convenient if one of the stages ends with an error.

In fact, make install can also build, because the install task depends on the all task (that is, directly building the application), which means that the make step can be skipped and only two commands can be executed if written on one line:

./configure && make install

Good luck to you! And thanks for reading!

There are situations when you need the latest version of a program, but it is not in your distribution's repository. Or this program is not added there at all for some reason. There are several options to get this program, one of them is to build the program from source code, directly under your distribution. Of course, we are talking about programs with open source code:)

Assembly (compilation) of a program is the transformation of its source code, written in some compiled programming language (for example, C ++), which is understandable to the programmer, into binary code (a sequence of zeros and ones), which is understandable to the computer's central processor. Not all programming languages ​​are compilable. For example, Python code can be run immediately without converting it to binary code (although this is also possible). To assemble the program, it is desirable to have a sufficiently powerful, and preferably a multi-core processor. Never compile programs on laptops! This will have an extremely negative impact on their life expectancy (they are not designed for such loads, unless of course you have gaming laptop).

There is nothing difficult in building a program from source code. The main thing to remember is one rule: in a package distribution, in no case should you use the method make install. Otherwise, in the future you will get such a big pile of problems. When you realize that you wanted to remove the program (delivered in this way), and package manager does not know about her. And the program itself consists of several hundred files scattered in different directories. Scary? Therefore, in packaged distributions, the program must be compiled into, in fact, a package. Then it can be easily removed, in which case. I wrote this because many manuals on compiling programs on Linux that I came across describe exactly make install. You can uninstall a program installed in this way only in two cases:

  • if you still have an archive with its code (then you can run make uninstall);
  • if the source code of the program supports it.
Don't use make install!

I note that not every program can be built in the same way. Therefore, you should always read the assembly instructions that are in the archive with the source code. It happens that the developer put a script there, which does everything on its own when it starts (collects and installs, but we remember about make install), or it may not be suitable for assembly make, but you need a different build system. Also, to build the program, you will need to install the necessary assembly dependencies for it (these are packages with the prefix -dev). In order to quickly assemble a program into a package in order to be able to install or remove it without problems, there is a utility called checkinstall. It will allow you to create a native package for the system ( deb or rpm), which will allow you to use a regular package manager to install / remove it

To build programs in GNU/Linux, one uses (mostly) the program make, which runs instructions from Makefile, but since there are many GNU / Linux distributions, and they are all different, in order to compile the program, you need to separately write the paths for each distribution, where libraries and header files lie. Programmers cannot study each distribution and create a Makefile for each one separately. Therefore, we came up with configurators that “study” the system and, in accordance with the knowledge gained, create a Makefile. To build, we need compilers: they are written in the dependencies of the package build-essential, so it's enough to install it with all dependencies. Still needed autoconf and automake. If the program is written in Qt, then it is usually assembled either by the team qmake(of course it must be installed), or by opening the project file in some IDE(usually Qt Creator) and assemblies in it.

First you need to prepare the system. To do this, install the necessary set of tools:

S udo apt install build-essential gcc devscripts git fakeroot automake autoconf

You can get the source code different ways. Download from the Internet (for example, from the developer's site), clone the source code repository, and so on. In the first case, in general, everything is clear. In the second: suppose that the program is in a git repository (on GitHub, for example). We can go to this repository and download the archive with the code from there

So copy the entire repository to yourself (as developers do). For example, take the program mgba. This is a game console emulator. Nintendo GameBoy. repository address. We copy it to ourselves:

git clone https://github.com/mgba-emu/mgba.git

In your home directory, you will have a directory with its source code. On the same page of the program, there is an assembly instruction.

We read carefully. Open a terminal and go to the directory with the source code:

cd ~/mgba

And we collect the program:

mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr ..
make
sudo checkinstall -D

Some data will be requested (package name, version, and so on. It is advisable to fill in all fields). After assembly, in the directory above (that is, in mgba), a deb package with the program will appear. Now you can simply install it with a double click or with the command sudo dpkg -i packagename.deb. If during the build you start to receive error messages, read them carefully. There may be some build dependencies missing.

Let's take a slightly different example that uses a configurator. In this case, in the directory with the source code, there are scripts: autogen.sh, configure and the like. autogen.sh generates a script configure, with which you can already configure the program before building (yes, the configurator of the configurator). As always, do not forget to read the instructions for assembling a particular program. Let's assume that the archive contains the script autogen.sh . Let's execute it:

./autogen.sh

After execution, the configure file should appear. To see with what parameters you can build the program, enter:

./configure --help

View all available options. Usually, these can be support for various plug-ins, an assembly with an alternative interface, even an assembly for a different processor architecture. Suppose a program uses an interface written in GTK+2, but has an alternative on GTK+ 3. Then the program configuration will look like this:

./configure --with-gtk3

./configure --enable-gtk3

Everything will be described in detail in the instructions. There are some standard options (after typing ./configure --help, they are written first), such as specifying the installation path:

prefix=/usr

After running configure and successfully configuring the code, you can run the build:

sudo checkinstall

That's all. As you can see, there is nothing complicated here. Although, I will not hide it, it happens that the developer did not bother with high-quality assembly instructions. But this rarely happens. I also want to draw your attention to the following: resort to building a program from source only as a last resort. If you are using Ubuntu LTS, then look (with the help of Google) if the program you need (or the latest version) is not in a more recent release of Ubuntu. Or maybe there is

Material from Bryansk Linux Users Group and www.rm.pp.ru

Each distribution has its own kernel build specifics, and this article focuses on exactly how to do this in Debian Etch. It also reveals the question of how to apply this or that patch to the kernel when it is necessary to support certain functionality or new hardware in your system. The article is intended primarily for more advanced users and there are no guarantees that this method will work as it should and all the described actions and responsibility fall on you.

  1. Note
  2. Applying patches
  3. Kernel configuration
  4. Compiling the kernel
  5. Installing a new kernel
  6. Problems
  7. Links

Note

Two methods for building the kernel will be described. The assembly option will be described first. deb package s that may be installed on your or another system. The second method is the so-called "traditional" way.

Method one. Building the kernel into .deb packages

Installing the necessary packages for compiling the kernel

First, let's update the package lists:

# apt-get update

Install the packages we need:

# apt-get install kernel-package libncurses5-dev fakeroot wget bzip2 build-essential

Downloading the kernel sources

Go to the /usr/src directory, go to www.kernel.org and select the desired kernel version. In this case, the version linux-2.6.23.1.tar.bz2 will be considered. Downloading:

# cd /usr/src # wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.23.1.tar.bz2

Unpack the sources and create a symbolic link:

# tar xjf linux-2.6.23.1.tar.bz2 # rm linux (remove previous symlink) # ln -s linux-2.6.23.1 linux # cd /usr/src/linux

Applying patches

Optional and unnecessarily do not do this!

Sometimes drivers or features are required that are not supported in the current kernel, such as virtualization technology or other specifics that are not in the current release. In any case, this is corrected by applying so-called patches (if any).

So let's say you've downloaded the required patch (let's call it patch.bz2 for example) to /usr/src. Apply the downloaded patch to our sources (You should still be in the /usr/src/linux directory):

# bzip2 -dc /usr/src/patch.bz2 | patch -p1 --dry-run # bzip2 -dc /usr/src/patch.bz2 | patch-p1

The first command is just a test and no changes will be applied to the sources. If no errors were returned after the first command, you can run the second command to apply the patch. In no case do not execute the second command if errors were issued after the first one!

This way you can apply patches to kernel sources. For example, there are some features that are only available in the 2.6.23.8 kernel, and the sources did not contain the necessary functionality, but patch-2.6.23.8.bz2 was released. You can apply this patch to 2.6.23 kernel sources, but not 2.6.23.1 or 2.6.23.3, etc. You can read more about this at:

Prefixes (prepatches) - equivalent to alpha releases; patches must be applied to sources of a complete previous release with a 3-digit version (for example, patch 2.6.12-rc4 can be applied to sources of version 2.6.11, but not to version 2.6.11.10.)

This means that if we want to build the 2.6.23.8 kernel, we need to download the sources for version 2.6.23 (http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.23.tar.gz) applied in the second way "traditonal" way!

Apply patch-2.6.23.8.bz2 to kernel 2.6.23:

# cd /usr/src # wget http://www.kernel.org/pub/linux/kernel/v2.6/patch-2.6.22.8.bz2 # cd /usr/src/linux # bzip2 -dc /usr/ src/patch-2.6.23.8.bz2 | patch -p1 --dry-run # bzip2 -dc /usr/src/patch-2.6.23.8.bz2 | patch-p1

Kernel Configuration

It's a good idea to use the existing configuration file running kernel and for the new one. Therefore, we copy the existing configuration to /usr/src/linux:

# make clean && make mrproper # cp /boot/config-`uname -r` ./.config # make menuconfig

after which the graphical kernel configuration menu will be loaded. Select "Load an Alternate Configuration File" in the configurator menu and click "OK". Then (if required) make the necessary changes to the kernel configuration by navigating through the menus (kernel configuration details can be found at www.google.com ). When you are done and press "Exit", the question "Do you wish to save your new kernel configuration?" will be asked, we answer in the affirmative "Yes".

Compiling the kernel

Building the kernel is done in just two commands:

# make-kpkg clean # fakeroot make-kpkg --initrd --append-to-version=-cybermind kernel_image kernel_headers

After --append-to-version=, you can write whatever name you want, but it must start with a minus sign (-) and not have spaces.

The process of compiling and building .deb packages can take quite a long time. Everything will depend on the configuration of the kernel and the capabilities of your processor.

Solving the problem with creating initrd.img

Recently, a bug appeared in Debian, consisting in the fact that after installing packages with kernels built in the way described here, the /boot/initrd.img file corresponding to them is not created. To fix an already installed kernel, you will have to create initrd.img manually:

update-initramfs -c -k<полная-версия-ядра>

To solve the problem "for the future" - comment out, as shown, the second of the lines quoted below in the /etc/kernel/postinst.d/initramfs-tools file:

# kernel-package passes an extra arg; hack to not run under kernel-package #[ -z "$2" ] || exit 0

Installing a new kernel

When the kernel build completes successfully, two .deb packages will be created in the /usr/src directory:

# cd /usr/src # ls -l

linux-image-2.6.23.1-cybermind_2.6.23.1-cybermind-10.00.Custom_i386.deb - actual kernel itself and linux-headers-2.6.23.1-cybermind_2.6.23.1-cybermind-10.00.Custom_i386.deb - headers kernels needed to build other modules (for example, when building modules nVidia drivers). Installing them:

# dpkg -i linux-image-2.6.23.1-cybermind_2.6.23.1-cybermind-10.00.Custom_i386.deb # dpkg -i linux-headers-2.6.23.1-cybermind_2.6.23.1-cybermind-10.00.Custom_i386.deb

(These packages can now be installed on another system without the need to build them again.)

Everything, the installation is complete, the bootloader menu, the installation of a new RAM disk and the kernel will be done automatically. It remains only to reboot:

# reboot

Method two. "traditional" way

We carry out all the points described above BEFORE the point "Compiling the kernel".

# make all # make modules_install # make install

As usual, the build can take a long time, depending on the kernel configuration and processor capabilities.

The main disadvantage of this method is that if you frequently update kernels, then after a while they will accumulate. a large number of and want to delete unused ones. To make it easier, you can build the kernel and other files that are installed into the system using the "make modules_install" and "make install" commands into a deb package (or rather, two starting from kernel 2.6.27) like the first method, but we will use here with kernel scripts:

# make all # make deb-pkg

Two .deb files will appear in the directory one level above the source directory. I compiled the kernel in the /usr/src/linux-2.6.27.10 directory and I got files in the /usr/src/ directory

# linux-2.6.27.10_2.6.27.10-1_amd64.deb # linux-firmware-image_2.6.27.10-1_all.deb

The kernel is installed with the command

# dpkg -i linux-2.6.27.10_2.6.27.10-1_amd64.deb

Old kernels can be removed, for example, from synaptic "a

Next steps

The kernel is built and installed, but now you need to create a RAM disk (without which the kernel simply won't boot) and you need to update the GRUB bootloader. To do this, do the following:

# depmod 2.6.23.1 # apt-get install yaird

Install the RAM disk:

# mkinitrd.yaird -o /boot/initrd.img-2.6.23.1 2.6.23.1

Let's update the bootloader easily and painlessly:

# update grub

Everything, the bootloader and the new kernel are ready, it remains only to reboot:

# reboot

Problems

If after a reboot your chosen new kernel does not boot, reboot and select your previous kernel and you can try to do the whole process again to build a working kernel. In this case, do not forget to delete the lines of the non-working kernel in /boot/grub/menu.lst.

terminal and commands

Have you often encountered desired version there is simply no application for your architecture in the Ubuntu distribution, but this program is available on the developers' site in the form of source codes in the .tar.gz archive. I think this situation is familiar to many, but not everyone continued to look for ways to solve this issue and simply looked for another analogue of the application or a slightly older version and continued to work.

I would like to immediately add a few words for newcomers to Linux, before doing something from this article, thoroughly study the terminal and the commands that are used to work with it, read mana or materials on the net.

  • For application builds of course, we will need developer tools, in our case this is a compiler and other programs accompanying it, the main work, of course, will be done by the make utility, and command line(terminal) will be like our kitchen where we will be prepare / assemble / install our application from source. AT linux terminal available by default, for your convenience, you can of course install any other more functional one that you are used to, for example, I use Guake, there are a lot of possibilities compared to the standard one, where it is easy to set up both copying and pasting commands or any text using CTRL +C,CTRL+V and much more, which makes working with the console more comfortable.
  • 1. Where to start when building applications from source, it is of course to download the application in a tar.gz or tar.bz2 archive, in my case, for example, the Gimp 2.9.2 application, although in our case the archive is not in tar.gz format, and tar.bz2, it makes no difference, download, after right-clicking on the archive - Extract here.

This is perhaps the first stage completed, what do we do next? And then we launch the terminal and go to our unpacked folder with files:

Cd /home/linux/Downloads/gimp-2.9.2/ls

  • 2. Before starting the preparation of the sources for the assembly, I would advise you to first open and familiarize yourself with the INSTALL file, you will find a lot useful information, in given file describes how to install the application, what commands you need to execute and much more interesting. Now I advise you to install additional package called - auto-apt, does a lot of routine work for you.
sudo apt-get install auto-apt

What does it mean to do a lot of routine work for me, you ask if run application source configuration with the prefix of this package, for example, in the form:

Auto-apt -y run ./configure

Of course, you can perform the configuration without the help of this package and simply run the command:

./configure

If you perform the configuration with the prefix - auto-apt -y run, then preparation of sources for assembly will take place in automatic mode, that is, this command can automatically download and install all the necessary files and libraries instead of you and satisfy all dependencies which will be required.

  • 3. When real work not everything is so smooth, maybe in one case everything will go well and the stage of preparing sources for assembly will pass without errors, in other cases, and these are probably the majority, you will encounter various kinds of errors, for example, this or that package is missing for further preparation of the source code. In most cases, the name of the package is written which is missing.

We try to install the missing package with the command:

sudo apt-get install package_name

In the event that the package is not found when executing the command above, the following combination very often helps me out, looking for packages that we lack in the cache:

apt-cache search package_name

  • After executing this command, you can and will find a suitable package, it often happens that you do not find a suitable package, but it occurs exact copy package but with prefix dev that is, a package of the form package_name-dev and you can use it to satisfy dependencies.
  • 4. After successful completion of the configuration of the sources for the assembly, it is advisable to install the package checkinstall which makes it easier to build an application package for your distribution.
sudo apt-get install checkinstall

Installed the package, then you can run the command:

Checkinstall -D

  • Attribute -D will create deb package, attribute -R will create an rpm package that is used in Fedora, RHEL, ASP Linux distributions, ALT Linux, Mandriva, openSUSE, additional attribute -S which will create a package used by Slackware.

In my case, I am running Ubuntu and ran the command with -D attribute, then the application will be assembled into a ready-made Deb format package, we will need some clarification data, for example, such as adding a description to the package, since you are compiling it and, accordingly, the description is completely clean and, as it were, a brief reference is required from you, what kind of application is this for? . In my case, as I checked, the following fields are also automatically filled in:

1 - Summary: [ EOF ] 2 - Name: [ gimp ] 3 - Version: [ 2.9.2 ] 4 - Release: [ 1 ] 5 - License: [ GPL ] 6 - Group: [ checkinstall ] 7 - Architecture: [ i386 ] 8 - Source location: [ gimp-2.9.2 ] 9 - Alternate source location: 10 - Requires: 11 - Provides: [ gimp ] 12 - Conflicts: 13 - Replaces:

  • As you can see, before the build, there is a check to see if the configuration has completed successfully, if there are any unsatisfied dependencies or other conflicts, if everything is fine, then the package will be built without problems.

I previously installed it without assembling the package, executing the commands:

Make make install

If you would like to remove installed application, it is worth executing the command:

Make uninstall

the command above will automatically remove those related to the application that you installed and will not affect anything third-party, it is advisable not to run through the directories and execute it from the same application directory in which you worked, that is, prepared the configuration, etc.

Although everything went without errors, but the whole process took a very long time, about 20 minutes until I installed the gimp from the sources, I managed to go make coffee and still observe the installation process, everything takes a long time due to the fact that I have to work in many different folders to scatter application source files, each file has its own purpose and must be located in a specific directory, so after doing make install, the process of installing thousands of source files in the required directories takes place.

This is how the installation of the application from the source goes, yes, not everything is so simple at first glance, I didn’t say that it would be easy, but if you try it, it develops your thinking very well and ways to find solutions in a given situation, which there is even very good.

That's probably all, if you have questions about the material above, or you tried to install and encountered errors, ask in the comments to the material, we will look for a solution together.

A computer