4.5 使用 Ports 管理機制

下面我們會介紹如何使用 Ports Collection 來安裝、移除軟體的基本用法。 至於其他可用的 make 詳細用法與環境設定,可參閱 ports(7)

4.5.1 記得安裝 Ports Collection

在安裝任一 ports 之前,必須先裝上 Ports Collection —— 它主要是由 /usr/ports 內一堆 Makefiles, patches 以及一些軟體簡介檔所組成的。

在裝 FreeBSD 時,若忘了在 sysinstall 內勾選要裝 Ports Collection 的話, 沒關係,可以照下列方式來安裝 ports collection:

CVSup 方式

使用 CVSup 是安裝、更新 Ports Collection 的快速方法之一。 若想更瞭解 CVSup 用法的話,請參閱 使用 CVSup

第一次跑 CVSup 之前,請先確認 /usr/ports 是空的! 若你已經裝了 Ports Collection ,但又自行加上其他 patch 檔,那麼 CVSup 並不會刪除你自行加上的 patch 檔,這樣可能會導致要安裝某些軟體時,發生 patch 失敗或編譯失敗。

  1. 安裝 net/cvsup-without-gui package:

    # pkg_add -r cvsup-without-gui
    

    細節用法請參閱 安裝 CVSup(Section A.5.2)。

  2. 執行 cvsup

    # cvsup -L 2 -h cvsup.tw.FreeBSD.org /usr/share/examples/cvsup/ports-supfile
    

    請把 cvsup.tw.FreeBSD.org 請改成離你比較近(快)的 CVSup 主機。 這部分可以參閱完整的 CVSup mirror 站列表(Section A.5.7)。

    Note: 若想改用自己設的 ports-supfile,比如說,不想每次都得打指令來指定所使用的 CVSup 主機。

    1. 這種情況下,請以 root 權限把 /usr/share/examples/cvsup/ports-supfile 複製到其他位置,比如 /root 或者自己帳號的家目錄。

    2. 修改新的 ports-supfile 檔。

    3. CHANGE_THIS.FreeBSD.org 改為離你比較近(快)的 CVSup 主機。 這部分可以參閱完整的 CVSup Mirrors (Section A.5.7) 站列表

    4. 然後就開始以類似下列指令跑 cvsup

      # cvsup -L 2 /root/ports-supfile
      
  3. 執行 cvsup(1) 之後,就會開始更新 Ports Collection。 不過這動作只是『更新』並不是『升級』,不會把已裝的軟體重新編譯、升級。

Portsnap 方式

portsnap(8) 也是更新 Ports Collection 的方式之一。 FreeBSD 6.0 起開始內建 Portsnap 機制,而較舊的系統,則可透過 sysutils/portsnap port 來安裝:

# pkg_add -r portsnap

Portsnap 細節功能,請參閱 Portsnap 使用篇

  1. /usr/ports 目錄不存在的話,就建立一下吧:

    # mkdir /usr/ports
    
  2. 接下來,下載壓縮的 Ports Collection 定期更新檔到 /var/db/portsnap 目錄。 完成下載後,要斷線與否都可以。

    # portsnap fetch
    
  3. 若是第一次跑 Portsnap 的話,則需要先解壓到 /usr/ports

    # portsnap extract
    

    若已有 /usr/ports 而且只是想更新而已,那麼就照下面作:

    # portsnap update
    

Sysinstall 方式

這方式要用 sysinstall 透過安裝來源來裝 Ports Collection。 請注意:所安裝的 Ports Collection 版本只是該 release 發佈時的版本而已,而非最新。 若能上網(Internet)的話,請使用上述方式之一會比較好。

  1. root 權限執行 sysinstall (在 FreeBSD 5.2 之前版本則是 /stand/sysinstall),方式如下:

    # sysinstall
    
  2. 請以方向鍵移動選擇項目,選擇 Configure,然後按 Enter 鍵。

  3. 選擇 Distributions,然後按 Enter 鍵。

  4. 選擇 ports,然後按 Space 鍵。

  5. Exit,然後按 Enter 鍵。

  6. 選擇要用的安裝來源,比如:CDROM(光碟)、FTP 等方式。

  7. Exit,然後按 Enter 鍵。

  8. 按下 X 鍵就可離開 sysinstall 程式。

4.5.2 Ports 的安裝方式

提到 Ports Collection,首先要先說明的是:何謂 “skeleton”。 簡單來講,port skeleton 就是讓軟體如何在 FreeBSD 順利編譯、安裝的最基本檔案組合。 每份 port skeleton 基本上會有:

有些 port 還會有其他檔案,像是 pkg-message 檔。 port 系統在一些情況時,會用這些檔案。 如果想知道這些檔案的更多細節用途,以及 port 一般用法,請參閱 FreeBSD Porter's Handbook

port 內寫的是告訴系統如何編譯 source code 的相關指令,但並不是真正的 source code。 而 source code 可以從光碟或網路(Internet)來取得, 該軟體開發者可能會把 source code 以各種格式來發佈。 通常是以 tar 以及 gzip 這兩者工具一起壓縮的檔案,也有可能是以其他工具壓縮,或根本沒壓縮。 而軟體的 source code 無論是以哪一種壓縮檔型態,我們都稱之為 “distfile”。 下面將介紹兩種安裝 FreeBSD port 的方式。

Note: 要安裝 port 的話,請務必切為 root 身份。

Warning: 在安裝任何 port 之前,請務必確認有更新 Ports Collection 到最新版, 此外請檢閱 http://vuxml.freebsd.org/ 來檢查所要裝的 port 是否有相關安全漏洞議題需要注意的。

portaudit 會在安裝任何 port 之前, 先自動檢查是否有相關已知的安全漏洞。這個工具在 Ports Collection 內有 (security/portaudit)。 在安裝 port 之前,可以先跑 portaudit -F 指令, 如此一來就會抓最新的資安漏洞資料庫回來核對。 每天的系統定期安檢會自動更新資料庫,並作安全稽核。 詳情請參閱 portaudit(1) 以及 periodic(8) 的線上說明。

Ports Collection 會假設你的網路是可正常連線的。 如果沒有的話,那麼需手動把所需的 distfile 檔複製到 /usr/ports/distfiles 才行。

開始操作之前,要先進入打算安裝的 port 目錄內:

# cd /usr/ports/sysutils/lsof

一旦進入 lsof 目錄後,就可以看到這個 port 的 skeleton 結構。 接下來,就是編譯,也就是 “build” 這個 port。 只需簡單輸入 make 指令,就可輕鬆完成編譯。 完成後,應該可以看到類似下面訊息:

# make
>> lsof_4.57D.freebsd.tar.gz doesn't seem to exist in /usr/ports/distfiles/.
>> Attempting to fetch from ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/.
===>  Extracting for lsof-4.57
...
[extraction output snipped]
...
>> Checksum OK for lsof_4.57D.freebsd.tar.gz.
===>  Patching for lsof-4.57
===>  Applying FreeBSD patches for lsof-4.57
===>  Configuring for lsof-4.57
...
[configure output snipped]
...
===>  Building for lsof-4.57
...
[compilation output snipped]
...
#

請注意:編譯完成後,就會回到提示列(prompt)。接下來就是安裝該 port 了, 要裝的話,只需在原本的 make 指令後面再加上一個字即可, 那個字就是 install

# make install
===>  Installing for lsof-4.57
...
[installation output snipped]
...
===>   Generating temporary packing list
===>   Compressing manual pages for lsof-4.57
===>   Registering installation for lsof-4.57
===>  SECURITY NOTE: 
      This port has installed the following binaries which execute with
      increased privileges.
#

一旦回到提示列(prompt),就可以執行剛裝的程式了。 另外,因為 lsof 這程式執行時會有額外權限, 所以會出現安全警告。在編譯、安裝 port 的時候, 請留意任何出現的警告。

此外,建議刪除編譯用的工作目錄(預設是 work), 這目錄內為在編譯過程中所用到的一些臨時檔案, 這些檔案不只佔硬碟空間,而且也可能會在該 port 升級新版時, 造成不必要的困擾。

# make clean
===>  Cleaning for lsof-4.57
#

Note:make install clean 就可以一口氣完成剛所說 makemake installmake clean 這三個步驟了。

Note: 有些 shell 會依據 PATH 環境變數的路徑, 把那些路徑的執行檔 cache 起來,來加速搜尋執行檔。 如果你用的是這類的 shell,那麼在裝完 port 後需要打 rehash 指令,才能執行新裝的執行檔,而 rehash 指令可以在 tcsh 之類的 shell 上使用,若是 sh 的話,則是 hash -r。 詳情請參閱你所使用的 shell 相關文件。

Some third party DVD-ROM products such as the FreeBSD Toolkit from the FreeBSD Mall contain distfiles. They can be used with the Ports Collection. Mount the DVD-ROM on /cdrom. If you use a different mount point, set CD_MOUNTPTS make variable. The needed distfiles will be automatically used if they are present on the disk.

Note: Please be aware that the licenses of a few ports do not allow for inclusion on the CD-ROM. This could be because a registration form needs to be filled out before downloading or redistribution is not allowed, or for another reason. If you wish to install a port not included on the CD-ROM, you will need to be online in order to do so.

The ports system uses fetch(1) to download the files, which honors various environment variables, including FTP_PASSIVE_MODE, FTP_PROXY, and FTP_PASSWORD. You may need to set one or more of these if you are behind a firewall, or need to use an FTP/HTTP proxy. See fetch(3) for the complete list.

For users which cannot be connected all the time, the make fetch option is provided. Just run this command at the top level directory (/usr/ports) and the required files will be downloaded for you. This command will also work in the lower level categories, for example: /usr/ports/net. Note that if a port depends on libraries or other ports this will not fetch the distfiles of those ports too. Replace fetch with fetch-recursive if you want to fetch all the dependencies of a port too.

Note: You can build all the ports in a category or as a whole by running make in the top level directory, just like the aforementioned make fetch method. This is dangerous, however, as some ports cannot co-exist. In other cases, some ports can install two different files with the same filename.

In some rare cases, users may need to acquire the tarballs from a site other than the MASTER_SITES (the location where files are downloaded from). You can override the MASTER_SITES option with the following command:

# cd /usr/ports/directory
# make MASTER_SITE_OVERRIDE= \
ftp://ftp.FreeBSD.org/pub/FreeBSD/ports/distfiles/ fetch

In this example we change the MASTER_SITES option to ftp.FreeBSD.org/pub/FreeBSD/ports/distfiles/.

Note: Some ports allow (or even require) you to provide build options which can enable/disable parts of the application which are unneeded, certain security options, and other customizations. A few which come to mind are www/mozilla, security/gpgme, and mail/sylpheed-claws. A message will be displayed when options such as these are available.

4.5.2.1 更改(Override)預設的 Ports 目錄

Sometimes it is useful (or mandatory) to use a different distfiles and ports directory. The PORTSDIR and PREFIX variables can override the default directories. For example:

# make PORTSDIR=/usr/home/example/ports install

will compile the port in /usr/home/example/ports and install everything under /usr/local.

# make PREFIX=/usr/home/example/local install

will compile it in /usr/ports and install it in /usr/home/example/local.

And of course,

# make PORTSDIR=../ports PREFIX=../local install

will combine the two (it is too long to completely write on this page, but it should give you the general idea).

Alternatively, these variables can also be set as part of your environment. Read the manual page for your shell for instructions on doing so.

4.5.2.2 Dealing with imake

Some ports that use imake (a part of the X Window System) do not work well with PREFIX, and will insist on installing under /usr/X11R6. Similarly, some Perl ports ignore PREFIX and install in the Perl tree. Making these ports respect PREFIX is a difficult or impossible job.

4.5.3 移除已安裝的 Ports

Now that you know how to install ports, you are probably wondering how to remove them, just in case you install one and later on decide that you installed the wrong port. We will remove our previous example (which was lsof for those of you not paying attention). Ports are being removed exactly the same as the packages (discussed in the Packages section), using the pkg_delete(1) command:

# pkg_delete lsof-4.57

4.5.4 升級已安裝的 Ports

首先,用 pkg_version(1) 指令來列出目前 Ports Collection 中提供了那些可升級的 port 版本:

# pkg_version -v

4.5.4.1 /usr/ports/UPDATING

每次更新完 Ports Collection 之後,請務必記得在升級 port 前, 先看看 /usr/ports/UPDATING, 這裡會寫升級方面的各式問題,比如:檔案格式改變、變更設定檔位置、與舊版不相容的問題等, 以及怎麼解決的完整步驟。

UPDATING 內容與你看到的其他文件有些不同、相衝的話, 那麼以 UPDATING 為主。

4.5.4.2 以 Portupgrade 來升級已安裝的 Ports

portupgrade 可以輕鬆升級已裝的軟體。 該工具可從 sysutils/portupgrade port 安裝, 安裝方式就如同其他 port 一樣,用 make install clean 指令就可以了:

# cd /usr/ports/sysutils/portupgrade
# make install clean

首先最好先以 pkgdb -F 來掃瞄已裝的 ports 資料庫是否有誤,並修正有問題的地方。 在每次做升級之前,最好定期做一下 pkgdb -F 動作會較為妥當。

When you run portupgrade -a, portupgrade will begin to upgrade all the outdated ports installed on your system. Use the -i flag if you want to be asked for confirmation of every individual upgrade.

# portupgrade -ai

If you want to upgrade only a certain application, not all available ports, use portupgrade pkgname. Include the -R flag if portupgrade should first upgrade all the ports required by the given application.

# portupgrade -R firefox

To use packages instead of ports for installation, provide -P flag. With this option portupgrade searches the local directories listed in PKG_PATH, or fetches packages from remote site if it is not found locally. If packages can not be found locally or fetched remotely, portupgrade will use ports. To avoid using ports, specify -PP.

# portupgrade -PR gnome2

To just fetch distfiles (or packages, if -P is specified) without building or installing anything, use -F. For further information see portupgrade(1).

4.5.4.3 以 Portmanager 來升級已安裝的 Ports

Portmanager 也可以用來輕鬆升級已裝的軟體。 該工具可從 sysutils/portmanager port 安裝:

# cd /usr/ports/sysutils/portmanager
# make install clean

所有已裝的軟體,都可以輕鬆用類似下列指令來升級:

# portmanager -u

此外,使用參數可以改為 -ui,如此一來 Portmanager 在升級一些有特殊選項的軟體時,就會詢問該如何升級。 Portmanager 也可以用來裝新 port。與以往常用的 make install clean 指令不同之處在於:它會先升級你要裝的 port 所相依的所有 ports, 然後才開始編譯、安裝要裝的 port。

# portmanager x11/gnome2

若要裝的 port 之軟體相依關係有問題時,也可以用 Portmanager 使它們重歸正軌。而 Portmanager 解決相依問題完畢之後,該 port 也會重新編譯,以因應正確的相依關係。

# portmanager graphics/gimp -f

其他運用方式,請參閱 Portmanager 的線上手冊。

4.5.5 Ports 與硬碟空間

因為使用 Ports Collection 遲早可能會用光硬碟空間, 所以在裝完軟體後,記得要以 make clean 指令來清除臨時的 work 目錄。 此外,可以用下列指令來清除整個 Ports Collection 內的臨時目錄:

# portsclean -C

You will accumulate a lot of old source distribution files in the distfiles directory over time. You can remove them by hand, or you can use the following command to delete all the distfiles that are no longer referenced by any ports:

# portsclean -D

Note: The portsclean utility is part of the portupgrade suite.

Do not forget to remove the installed ports once you no longer need them. A nice tool to help automate this task is available from the sysutils/pkg_cutleaves port.

This, and other documents, can be downloaded from ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/.

For questions about FreeBSD, read the documentation before contacting <questions@FreeBSD.org>.
For questions about this documentation, e-mail <doc@FreeBSD.org>.