一、安裝 Miniconda
1. 下載 Miniconda 安裝腳本
x86_64 架構:
cd ~
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
ARM 架構(如樹莓派或 Apple Silicon 的 Linux 虛擬機):
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-aarch64.sh
2. 運行安裝腳本
bash Miniconda3-latest-Linux-x86_64.sh
- 按回車閲讀許可協議(可按 q 跳過)。
- 輸入
yes接受協議。 - 安裝路徑默認為
~/miniconda3,直接回車即可。 - 是否初始化 conda?輸入
yes(這樣會自動配置 PATH)。
3. 重新加載 shell 配置
source ~/.bashrc
如果你用的是zsh,則source ~/.zshrc
驗證安裝:
conda --version
python --version
4. 配置清華鏡像(加速下載)
清華鏡像站提供了 Conda 和 pip 的國內加速源。
- 添加 Conda 清華鏡像(推薦使用 .condarc 配置文件)
運行以下命令生成配置文件:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ # 可選
# 設置搜索時顯示通道地址
conda config --set show_channel_urls yes
注意:不要添加 defaults 通道的清華鏡像,因為清華已將 main 和 free 分開。
- (可選)禁用默認的 Anaconda 通道(避免回源)
conda config --remove channels defaults
- 查看當前配置
conda config --show channels
你應該看到類似:
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
二、安裝 Xinference
1. 創建並激活 conda 環境
conda create -n xinference python=3.11 -y
conda activate xinference
重點:Python最新版本推薦3.11,不然會有各種報錯
-
如果後續用
pip,也可以配置 pip 清華源:pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
2. 安裝 Xinference
首先安裝以下依賴非常重要!!(最容易報錯,提前解決):
# 從 conda-forge 安裝 pynini(自動解決 OpenFST 依賴)
conda install -c conda-forge pynini
pip install Cython
可選(通常不需要,可以跳過):先用 conda 安裝部分依賴:
# 安裝 PyTorch(根據你的 CUDA 版本選擇)
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
# 安裝其他可能的依賴(可選)
conda install -c conda-forge sentencepiece protobuf
使用 pip 安裝最新版:
pip install "xinference[all]"
[all]表示安裝所有可選依賴(包括 GPU 支持、多模態模型等)。如果你只需要基礎功能,可以只運行pip install xinference。
可以加上鏡像加速,比如:pip install "xinference[all]" -i https://pypi.doubanio.com/simple
3. 驗證安裝
xinference --help
如果輸出幫助信息,説明安裝成功。
三、設置 Xinference 開機自啓動
1. 創建 systemd 服務文件
sudo vim /etc/systemd/system/xinference.service
粘貼以下內容(請根據你的用户名和路徑修改):
[Unit]
Description=Xinference Service
After=network.target
[Service]
Type=simple
User=你的實際用户名
Group=你的實際用户名
WorkingDirectory=/home/你的實際用户名
ExecStart=/bin/bash -c 'source /home/你的實際用户名/miniconda3/etc/profile.d/conda.sh && conda activate xin_env && xinference-local --host 0.0.0.0 --port 9997'
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
説明:
--host 0.0.0.0允許外部訪問(如需僅本地訪問,改為127.0.0.1)。--port 9997是默認端口,可按需修改。- 確保路徑
/home/你的實際用户名/miniconda3/envs/xinference/bin/xinference-local存在。可通過which xinference-local查看實際路徑。
2. 重載 systemd 配置
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
3. 啓用並啓動服務
sudo systemctl enable xinference.service # 開機自啓
sudo systemctl start xinference.service # 立即啓動
4. 檢查服務狀態
sudo systemctl status xinference.service
如果看到 active (running),説明服務已正常運行。
5. 查看日誌(排錯用)
journalctl -u xinference.service -f
四、訪問 Xinference Web UI
默認情況下,Xinference 會啓動一個 Web 管理界面。
在瀏覽器中訪問:
http://<你的服務器IP>:9997
例如:http://192.168.1.100:9997
五、可選:配置防火牆(如啓用)
如果你使用的是 ufw:
sudo ufw allow 9997/tcp
如果是雲服務器(如 AWS、阿里雲),記得在安全組中開放 9997 端口。
六、常見問題
Q1: 啓動失敗,提示找不到命令?
- 檢查
ExecStart路徑是否正確,使用which xinference-local確認。 - 確保 conda 環境已正確創建並安裝了 xinference。
Q2: 如何停止服務?
sudo systemctl stop xinference.service
Q3: 如何更新 Xinference?
conda activate xinference
pip install --upgrade xinference
sudo systemctl restart xinference.service
Q4: 報錯信息:
error: command 'g++' failed: No such file or directory
...
error: command 'gcc' failed: No such file or directory
這兩個包(pyworld 和 cdifflib)都包含 C/C++ 擴展,需要本地編譯。
解決方法:
Ubuntu/Debian:
sudo apt update && sudo apt install build-essential
CentOS/RHEL/Fedora:
-
CentOS/RHEL:
sudo yum groupinstall "Development Tools" -
Fedora:
sudo dnf groupinstall "Development Tools"
使用的是 Conda 環境 也可以嘗試安裝編譯器:
conda install -c conda-forge gcc gxx_linux-64
注意:有些 Conda 環境仍依賴系統級編譯器,建議優先用系統包管理器安裝 build-essential。
總結
- 安裝 Miniconda → 提供 Python 環境管理。
- 創建 conda 環境並安裝 Xinference。
- 使用 systemd 創建開機自啓服務。
- 訪問 Web UI 管理模型。
按照以上步驟,你就可以在 Linux 上穩定運行 Xinference 並實現自動啓動。
提示:部署具體模型,可通過 Web UI 或命令行加載。