Tech Tutorial: 206.1 Make and install programs from source #
Introduction #
Installing software from source code is a common practice in the Linux world, especially when dealing with software not available in binary form or when specific customizations are needed. This tutorial aims to equip you with the knowledge and skills necessary to download, compile, and install software from source code.
Exam Objective: #
- Understanding of source code and its compilation.
- Ability to prepare the environment for building software.
- Proficiency in using make and install commands.
Key Knowledge Areas: #
- Use of
tar
,gzip
,bzip2
to unpack sources. - Understanding of
./configure
,make
, andmake install
commands. - Ability to handle typical issues that might occur during the process.
Utilities: #
- tar
- gzip
- bzip2
- patch
- make
- configure scripts
Step-by-Step Guide #
1. Preparing Your Environment #
Before compiling from source, ensure you have the necessary development tools, like gcc
, g++
, make
, and libraries required by the software.
# Install basic development tools on Debian/Ubuntu
sudo apt-get install build-essential
# Install basic development tools on CentOS/RHEL
sudo yum groupinstall 'Development Tools'
2. Downloading the Source Code #
Source code is often distributed in compressed files. Here’s how to download and extract source code using wget
, tar
, gzip
, and bzip2
.
Example: Downloading and extracting the source code of Vim
# Download Vim source code
wget https://github.com/vim/vim/archive/refs/tags/v8.2.3456.tar.gz
# Extract the gzipped tar archive
tar -xzvf v8.2.3456.tar.gz
3. Configuring the Build #
Most source code distributions include a script called configure
that prepares the makefile and checks for dependencies.
# Navigate to the source directory
cd vim-8.2.3456
# Run the configure script
./configure
You can customize the build process with various options:
# Configure Vim with a custom prefix and Python support
./configure --prefix=/usr/local --enable-pythoninterp=yes
4. Compiling the Source Code #
Use make
to compile the source code. This might take some time depending on the size and complexity of the software.
# Compile the source code
make
5. Installing the Compiled Program #
After compilation, you install the software using make install
, typically requiring administrative privileges.
# Install the software
sudo make install
6. Handling Patches #
Sometimes, you may need to apply patches to the source code before compiling. Here’s how to use patch
.
Example: Applying a patch to Vim
# Assuming patchfile.patch is in the current directory
patch -p1 < patchfile.patch
Detailed Code Examples for Every Command #
tar #
# Extracting tar.gz archive
tar -xzvf filename.tar.gz
# Extracting tar.bz2 archive
tar -xjvf filename.tar.bz2
gzip #
# Compressing a file
gzip filename
# Decompressing a file
gzip -d filename.gz
bzip2 #
# Compressing a file
bzip2 filename
# Decompressing a file
bzip2 -d filename.bz2
patch #
# Applying a patch
patch < some-fix.patch
# Applying a patch with different strip level
patch -p1 < some-fix.patch
make #
# Compile source code
make
# Install the software
sudo make install
# Clean build files
make clean
configure scripts #
# Configure with default options
./configure
# Configure with custom options
./configure --prefix=/opt/software --enable-feature-X
Conclusion #
Compiling and installing software from source in Linux allows for maximum customization and optimization of the software to your specific needs. By mastering these steps and utilities, you enhance your control over the software installation and maintenance processes on Linux systems. This skill is essential for system administrators and developers who work with Linux environments.