Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 原始碼套件安裝完整指南:./configure、make、make install、前綴路徑、清理與解除安裝

編輯:關於 Linux

原始碼安裝 (Source Build) 能針對系統優化、啟用特定功能、安裝未打包軟體。本文標準化流程:解壓、configure、make、make install、make clean/uninstall,並解析關鍵參數 --prefix、--enable/--disable、DESTDIR 打包技巧。

標準安裝流程

# 1. 取得原始碼
wget https://example.com/foo-1.2.3.tar.gz
# 或 git clone https://github.com/user/foo.git

# 2. 解壓
tar xf foo-1.2.3.tar.gz      # 自動偵測 .gz/.bz2/.xz
cd foo-1.2.3

# 3. 閱讀文件 (關鍵步驟)
cat README INSTALL NEWS       # 依賴、編譯選項、已知問題

# 4. 配置 (生成 Makefile)
./configure --prefix=/opt/foo  # 指定安裝根目錄
# 常用選項:
#   --enable-feature / --disable-feature
#   --with-lib=/path / --without-lib
#   --sysconfdir=/etc/foo --localstatedir=/var/foo
#   CC=clang CFLAGS="-O2 -march=native"  # 編譯器優化

# 5. 編譯
make -j$(nproc)                # 並行編譯,加速

# 6. 安裝 (需 root)
make install

# 7. 驗證
/opt/foo/bin/foo --version

關鍵參數深度解析

參數用途範例
--prefix=DIR安裝根目錄 (預設 /usr/local)--prefix=/opt/nginx
--exec-prefix=DIR架構相關檔案安裝目錄 (通常同 prefix)
--bindir=DIR執行檔目錄--bindir=/usr/bin
--libdir=DIR庫檔目錄--libdir=/usr/lib64
--sysconfdir=DIR設定檔目錄--sysconfdir=/etc/nginx
DESTDIR=DIR安裝時加前綴 (打包用),不影響運行路徑make DESTDIR=/tmp/pkg install

清理與解除安裝

# 清理編譯中間檔 (保留 configure 生成的 Makefile)
make clean

# 徹底清理 (含 Makefile,需重新 configure)
make distclean

# 解除安裝 (需原始碼目錄未刪除,且 Makefile 支援 uninstall 目標)
make uninstall
# 若不支援,需手動刪除 --prefix 下對應檔案

打包為 RPM/DEB (進階)

# 使用 checkinstall 自動產生套件 (需安裝 checkinstall)
make clean
./configure --prefix=/usr
make -j$(nproc)
checkinstall -D --install=no --pkgname=foo --pkgversion=1.2.3 \
    --maintainer="you@example.com" --default
# 產生 foo_1.2.3-1_amd64.deb 或 .rpm,可用 dpkg/rpm 安裝管理

常見問題排查

configure: error: no acceptable C compiler found
安裝編譯工具鏈:yum groupinstall "Development Tools" / apt install build-essential。
checking for libfoo... not found
安裝開發套件:yum install libfoo-devel / apt install libfoo-dev,或指定 --with-libfoo=/custom/path。
make[1]: *** [foo.o] Error 1
查看完整錯誤上下文 (make 2>&1 | head -30),常為缺頭文件、C 標準不符、編譯器版本過舊。
安裝後 command not found
確認 $PATH 包含 $PREFIX/bin,或建立軟連結 ln -s /opt/foo/bin/foo /usr/local/bin/。

最佳實務清單

  • 優先使用發布版套件管理器 (dnf/apt) 安裝穩定版;原始碼安裝僅用於:需要最新功能、自訂編譯選項、無套件可用。
  • 統一安裝於 /opt/-,以 stow 或 lndir 管理符號連結。
  • 保留原始碼目錄 (含 config.log、Makefile) 以便日後 make uninstall 或升級。
  • 記錄編譯參數 (./configure --help | grep enable) 於部署文件。

遵循標準流程與參數控制,原始碼安裝將變得可預測、可維護、可複現。

Copyright © Windows教程網 All Rights Reserved