要編譯 Caffe,第一步不是裝 Caffe,而是先有一個乾淨穩定的 Linux 環境。這篇先談虛擬機與發行版的選擇,再進入 Ubuntu 的基礎設定。
建議用 VMware Workstation 而不是 Player,因為快照功能在反覆嘗試編譯的過程中幾乎是必需品——設定搞爛了,一鍵回滾比重裝省事太多。另外要注意版本相容性:Windows 8 與 VMware 12 的相容性不佳,所以筆者桌上機裝的是 VMware 11;筆電是 Windows 7,裝 VMware 12,但建立虛擬機時刻意把相容性設為 VMware 11 等級,如此一旦不得不複製系統,也能在兩台機器之間順利移植。VirtualBox 試用後感覺不夠順手,因此放棄。
Linux 版本盡量挑主流發行版。理由有兩個:一是穩定性較有保障,二是出問題時查資料方便得多,大部分常見錯誤網路上都找得到答案。一開始筆者對這個說法頗不以為然,但真的裝過幾個版本之後就發現差異很實在,而這些差異往往會吃掉你不少時間。
舉例來說,xubuntu 14.04 雖然是 Ubuntu 的衍生版、版本號也一樣,但裝好虛擬機後要使用 VMware Tools 的檔案共享功能,得先補裝一堆元件;而 ubuntu 14.04 完全不需要手動介入,12.04 則要手動安裝。這就是差別。
先備份再編輯來源清單:
sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup sudo gedit /etc/apt/sources.list
身為新手時筆者常忘記加 sudo。不論有沒有報錯,Linux 下的回傳訊息都值得細讀,裡面其實處處是提示——看到 permission denied,就是權限不足要加 sudo。另外,安裝軟體一定要看輸出,別在報錯的情況下還以為裝好了就往下一步走,最後出問題時反而無從回溯(這都是血淚經驗)。
接著在檔案中填入需要的來源,並把預設那些來源刪掉,否則更新時會去下載一堆用不上的清單,既沒用又浪費時間。要注意 trusty 代表 14.04,其他版本這個位置的字串不同;如果你複製的是別人的清單,記得把 trusty 換成對應的版本代碼名稱,具體對照可參考 Ubuntu 官方 wiki 的源列表頁面。以下只列三個主要來源:
# 官方源 deb https://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb https://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse deb https://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse deb https://archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse deb https://archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse deb-src https://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb-src https://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse deb-src https://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse deb-src https://archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse deb-src https://archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse # 163 源 deb https://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse deb https://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse deb https://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse deb https://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse deb https://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse # 阿里源 deb https://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse # 搜狐源 deb https://mirrors.sohu.com/ubuntu/ trusty main restricted universe multiverse deb https://mirrors.sohu.com/ubuntu/ trusty-security main restricted universe multiverse deb https://mirrors.sohu.com/ubuntu/ trusty-updates main restricted universe multiverse deb https://mirrors.sohu.com/ubuntu/ trusty-proposed main restricted universe multiverse deb https://mirrors.sohu.com/ubuntu/ trusty-backports main restricted universe multiverse
存檔後執行更新:
sudo apt-get update
網路上有人說一定要 update,個中原因筆者也不確定,不過照做就對了。至於 sudo apt-get upgrade 是升級已安裝的套件,並非必要;試跑一次會發現連 Firefox 之類的都被升級掉了。
環境準備到這裡,下一步就可以開始處理 GPU 驅動與 Caffe 的相依套件了。