Basic Linux Tutorial: Install software from source code
Why install software from source code?
Under what circumstances do we need to install software from the source code? I think it is similar to these situations: one case is that the software releases a new version, and the release version is not followed up in a timely manner. At this time, if you want to taste it, you have to rely on yourself; another scenario is that no binary package can be directly used by software developers or existing systems, and you have to use the software yourself; of course, there are other situations. All in all, learning to install software from source code is a very important skill.
What is make and configure?
Make is a program. When you execute make, make searches for the makefile text file in the current directory. Makefile defines how the source code is compiled in detail, how to call gcc to compile the source code to generate the target file, and how to link to generate executable files.
How is makefile generated? Generally, software developers provide a detection program to detect the user's operating system environment. After the program is detected, the makefile will be automatically created.
Preparations
So how can we install software from the source code? First, you must prepare the source code for compilation. This includes two aspects:
Compilation tools: To compile source code into executable binary files, compilation tools are essential: gcc, g ++, make, autoconfig, and so on. In the CentOS series, you can install yum-y groupinstall "Development Tools. In Ubuntu, You can executesudo apt-get install build-essential
Command to install the basic compilation tool. Depending on the actual situation of the compiled program, you may also need to install other tools.
Compile dependencies: In addition to installing basic compilation tools, we also need to install the dependencies required by the program in order to compile the source code smoothly. Contains the library functions provided by the system and the dependent packages of third-party software. In the CentOS series, you can install the corresponding dependency package through yum-y install xxxx-devel. The Ubuntu system can usesudo apt-get build-dep
The command is followed by the package name to prepare the required dependencies. In general, if configure or make reports an error, the error information clearly indicates that the dependency is missing. Or copy the error information to google to solve the problem.
Source code installation 3 environment:./configure; make & make install
Before compiling the source code, you must download the software source code package to your hard disk. Generally, program source code packages are often packaged into the .tar.gz and .tar.bz2 formats. The former is available.tar zxvf *.tar.gz
To unpack the package. The latter usestar jxvf *.tar.bz2
. We recommend that you extract the package to the/usr/local/src directory: # tar xf * .tar.gz-C/usr/local/src
Generally, after unpacking, we should read the relevant instruction files attached to the program, such as README (self-report file) and INSTALL (Installation File), to learn about program compilation. This step is very important !!!
When everything is ready, let's perform the source code compilation trilogy.
Configure and create the makefile file: This is the first step to compile the source code../configure
Command complete. Execute this step to prepare for source code compilation. Common options are:--
Prefix = PREFIX, used to specify the installation location of the program. You can use -- help to query more options. Some programs do not need to perform this step.
Compile: Once the configuration is passed, you can use it immediately.make
Command according to the definition of makefile, call gcc to execute the source code compilation process. Depending on the specific circumstances of the software, the time required for compilation varies. What we need to do is to wait patiently and stay calm. Although this step only contains simple instructions, the problems encountered sometimes are very complicated. The most common case is that the program compilation fails to be completed successfully. In this case, you need to analyze the error prompt to find the corresponding solution. You need to execute make clean and then re-execute the make command.
Installation: If the compilation is normal, executemake install
You can install the program in the system.
Note that the above steps must be performed step by step. As long as one of the steps fails, there is no way to proceed in the future.
Management: Directory planning, uninstallation, and upgrade
The source code is usually decompressed to the/usr/local/src directory.
It is best to install it in the/usr/local directory during installation.
Add the man page of the software to man path, edit/etc/man. config, and add MANPATH items.
Add the bin directory of the software to the PATH environment variable, edit/etc/profile. d/xxxx. sh, and add export PATH = $ PATH:/usr/local/xxx/bin.
What should I do if I think the compiled software is not suitable enough and want to delete it? We only need to go to the directory where the source code is compiled and runsudo make uninstall
Command.
Related reading: C ++ static library and dynamic library
For more details, please continue to read the highlights on the next page: