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 系統編譯安裝 Mono 完整教學

編輯:關於 Linux

Mono 是一個開源的跨平台 .NET 執行環境,讓開發者能在 Linux、macOS 等非 Windows 系統上運行 .NET 應用程式。本文將詳細介紹如何在 Linux 伺服器上從原始碼編譯安裝 Mono,適用於需要特定版本或自訂編譯選項的場景。

一、安裝必要的依賴套件

在開始編譯前,請先安裝建置工具與函式庫開發套件。以 CentOS/RHEL 為例,使用 yum 安裝:

yum install gcc bison pkgconfig glib2-devel gettext make gcc-c++ libstdc++-devel libgdiplus-devel

其他發行版請對應安裝同名或相當的套件(如 Debian/Ubuntu 使用 apt-get install build-essential libglib2.0-dev pkg-config ...)。

二、下載並解壓原始碼

Mono 官方原始碼鏡像站點:https://ftp.novell.com/pub/mono/sources/mono/。本教學以 2.10.2 版為例(請根據實際需求選擇較新穩定版):

cd /usr/local/src
wget -c https://ftp.novell.com/pub/mono/sources/mono/mono-2.10.2.tar.bz2
tar xf mono-2.10.2.tar.bz2
cd mono-2.10.2

三、配置、編譯與安裝

使用 configure 指定安裝路徑(此處為 /usr/local/mono),然後執行編譯與安裝:

./configure --prefix=/usr/local/mono
make
make install

編譯過程可能需要數分鐘至數十分鐘,視伺服器效能而定。若遇到錯誤,請檢查依賴套件是否齊全。

四、設定環境變數

為了讓使用者能直接執行 mono、mcs 等指令,並正確找到 pkg-config 路徑,請將以下內容加入使用者的 ~/.bash_profile(或全域 /etc/profile):

export PKG_CONFIG_PATH=/usr/local/mono/lib/pkgconfig:$PKG_CONFIG_PATH
export PATH=/usr/local/mono/bin:$PATH

修改後執行 source ~/.bash_profile 使其生效。

五、驗證安裝

執行 Mono 內建的測試腳本:

/usr/local/mono/bin/mono-test-install

若輸出包含 Active Mono: /usr/local/mono/bin/mono 以及 Your have a working System.Drawing setup,表示安裝成功。若出現 pkg-config could not find mono 警告,請檢查環境變數 PKG_CONFIG_PATH 是否正確設定。

常見問題與提示

  • 若需支援 Windows Forms、ASP.NET 等功能,請確保 libgdiplus 正確安裝。
  • 生產環境建議使用發行版套件管理器安裝穩定版(如 yum install mono-core),僅在需要特定版本時才自行編譯。
  • 升級 Mono 時,建議先備份 /usr/local/mono 目錄,或使用不同前綴路徑並行安裝。

透過以上步驟,您已在 Linux 伺服器上成功編譯安裝 Mono。現在可以開始部署 .NET 應用程式或進行跨平台開發測試。

Copyright © Windows教程網 All Rights Reserved