下面的章节提供了使用ports collection来安装或卸载程序的基本用法。
在您能使用ports之前, 您必须先获得ports collection--本质上是/usr/ports目录下的一堆 Makefiles,补丁, 和描述文件。
在您安装FreeBSD系统的时候,sysinstall会询问您是否需要安装ports collection. 如果您选择no,那您可以根据下面的指令来安装ports collection:
Sysinstall 方式
这种方式使用sysinstall再次手动安装ports collection。
用root用户, 运行 /stand/sysinstall命令,像下面显示的:
# /stand/sysinstall
选择Configure, 按Enter。
选择Distributions, 按 Enter。
选择ports, 按Space。
选择Exit, 按Enter。
选择您的安装介质,就像CDROM,FTP,等等
选择Exit并按Enter。
按X键退出sysinstall。
您可以使用CVSup,这个程序让您获得和更新 ports collection 。CVSup需要使用 /usr/share/examples/cvsup/ports-supfile这个配置文件。 如果您想获得关于CVSup这个应用程序的更多信息,请看使用 CVSup (µÚ A.5 节)和本文件。
CVSup 方法
使用CVSup获得 ports collection ,是一个非常快捷的方法。 如果您想保持您的 ports 树的最新状态,或学习更多关于CVSup的内容您可以阅读刚才提及的内容。
安装net/cvsup port。详情请参阅CVSup 安装 (µÚ A.5.2 节)。
用root 用户,复制 /usr/share/examples/cvsup/ports-supfile 到一个新的地方,比如 /root 和您的家目录。
编辑 ports-supfile。
修改 CHANGE_THIS.FreeBSD.org 到离您比较近的CVSup 服务器。请看 CVSup 镜像 (µÚ A.5.7 节) 服务器的完整列表。
运行 cvsup:
# cvsup -g -L 2 /root/ports-supfile
运行这个命令之后将下载和应用所有变化的ports系统,除了在您的系统中重建ports系统。
当提到Ports collection时,第一个要说明的就是何谓“``skeleton''”。简单地说,一个port skeleton让一个程序在FreeBSD上简洁地编译并安装的所需文件的最小组合。每个port skeleton包含:
一个 Makefile。Makefile包括好几个部分,指出应用程序是如何编译和它将被安装在系统的哪个地方。
一个 distinfo 文件。这个文件包括这些信息:这些文件用来对下载后的文件checksums,使用 md5(1) 来确保在下载过程中文件不会被破坏。
一个files目录。这个目录包括在您的FreeBSD系统上编译和安装程序需要用到的补丁。这些补丁基本上都是些小文件,指出特定文件作了哪些修正。他们都是纯文本的的格式,基本上是这样的``Remove line 10'' 或``Change line 26 to this ...'',补丁文件使用了``diffs'',他们由 diff(1) 程序产生。
这个目录也包含了在建立port时要用到的其它文件T。
一个pkg-descr文件。这是一个提供更多细节,有软件的多行描述。
一个 pkg-plist 文件。这是即将被安装的所有文件的列表。它告诉ports系统在重新安装时什么文件需要被删除。
一些ports还有些其它的文件,就像 pkg-message。ports系统会在特殊的情况下使用这些文件。如果您想知道这些文件更多的细节以及ports的概要,请参阅 FreeBSD Porter's Handbook.
port里面包含着如何编译源代码的指令,但不包含真实的源代码。您可以在网上或CD-ROM上获得源代码。 Source code is distributed in whatever manner the software author desires. Frequently this is a tarred and gzipped file, but it might be compressed with some other tool or even uncompressed. The program source code, whatever form it comes in, is called a ``distfile''. The two methods for installing a FreeBSD port are described below.
注: 您必须使用 root 用户登录后安装ports.
警告在安装任何 port 之前, 应该首先确保已经更新到了最新的 ports collection, 并检查 http://vuxml.freebsd.org/ 中是否有与那个 port 有关的安全问题。
在安装应用程序之前, 可以使用 portaudit 来自动地检查是否存在已知的安全问题。 这个工具同样可以在 ports collection (security/portaudit) 中找到。 在安装新的 port 之前, 可以考虑先运行一下 portaudit -F 来抓取最新的漏洞数据库。 在每天的周期性系统安全检察时, 数据库会被自动更新, 并且会在这之后实施安全审计。 欲了解进一步的情况,请参阅 portaudit(1) 和 periodic(8)。
The FreeBSD Project's official CD-ROM images no longer include distfiles. They take up a lot of room that is better used for precompiled packages. CD-ROM products such as the FreeBSD PowerPak do include distfiles, and you can order these sets from a vendor such as the FreeBSD Mall. This section assumes you have such a FreeBSD CD-ROM set.
Place your FreeBSD CD-ROM in the drive. Mount it on /cdrom. (If you use a different mount point, the install will not work.) To begin, change to the directory for the port you want to install:
# cd /usr/ports/sysutils/lsof
Once inside the lsof directory, you will see the port skeleton. The next step is to compile, or ``build'', the port. This is done by simply typing make at the prompt. Once you have done so, you should see something like this:
# make >> lsof_4.57D.freebsd.tar.gz doesn't seem to exist in /usr/ports/distfiles/. >> Attempting to fetch from file:/cdrom/ports/distfiles/. ===> 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] ... #
Notice that once the compile is complete you are returned to your prompt. The next step is to install the port. In order to install it, you simply need to tack one word onto the make command, and that word is 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. #
Once you are returned to your prompt, you should be able to run the application you just installed. Since lsof is a program that runs with increased privileges, a security warning is shown. During the building and installation of ports, you should take heed of any other warnings that may appear.
注: You can save an extra step by just running make install instead of make and make install as two separate steps.
注: Some shells keep a cache of the commands that are available in the directories listed in the PATH environment variable, to speed up lookup operations for the executable file of these commands. If you are using one of these shells, you might have to use the rehash command after installing a port, before the newly installed commands can be used. This is true for both shells that are part of the base-system (such as tcsh) and shells that are available as ports (for instance, shells/zsh).
注: 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 (see the next section).
As with the last section, this section makes an assumption that you have a working Internet connection. If you do not, you will need to perform the CD-ROM installation, or put a copy of the distfile into /usr/ports/distfiles manually.
Installing a port from the Internet is done exactly the same way as it would be if you were installing from a CD-ROM. The only difference between the two is that the distfile is downloaded from the Internet instead of read from the CD-ROM.
The steps involved are identical:
# make install >> lsof_4.57D.freebsd.tar.gz doesn't seem to exist in /usr/ports/distfiles/. >> Attempting to fetch from ftp://ftp.FreeBSD.org/pub/FreeBSD/ports/distfiles/. Receiving lsof_4.57D.freebsd.tar.gz (439860 bytes): 100% 439860 bytes transferred in 18.0 seconds (23.90 kBps) ===> 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] ... ===> 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. #
As you can see, the only difference is the line that tells you where the system is fetching the port distfile from.
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.
注: 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/.
注: 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.
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.
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.
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). As with installing ports, the first thing you must do is change to the port directory, /usr/ports/sysutils/lsof. After you change directories, you are ready to uninstall lsof. This is done with the make deinstall command:
# cd /usr/ports/sysutils/lsof # make deinstall ===> Deinstalling for lsof-4.57
That was easy enough. You have removed lsof from your system. If you would like to reinstall it, you can do so by running make reinstall from the /usr/ports/sysutils/lsof directory.
The make deinstall and make reinstall sequence does not work once you have run make clean. If you want to deinstall a port after cleaning, use pkg_delete(1) as discussed in the Packages section of the Handbook.
Using the ports collection can defiantly eat up your disk space. For this reason you should always remember to clean up the work directories using the make clean option. This will remove the work directory after a port has been built, and installed. You can also remove the tar files from the distfiles directory, and remove the installed ports when their use has delimited.
Some users choose to limit the port categories by placing an entry in the refuse file. This way when they run the CVSup application, it will not download the files in that category.
注: Once you updated your ports collection, before attempting a port upgrade, you should check the /usr/ports/UPDATING file. This file describes various issues and additional steps users may encounter and need to perform when updating a port.
Keeping your ports up to date can be a tedious job. For instance, to upgrade a port you would go to the ports directory, build the port, deinstall the old port, install the new port, and then clean up after the build. Imagine doing that for five ports, tedious right? This was a large problem for system administrators to deal with, and now we have utilities which do this for us. For instance the sysutils/portupgrade utility will do everything for you! Just install it like you would any other port, using the make install clean command.
Now create a database with the pkgdb -F command. This will read the list of installed ports and create a database file in the /var/db/pkg directory. Now when you run portupgrade -a, it will read this and the ports INDEX file. Finally, portupgrade will begin to download, build, backup, install, and clean the ports which have been updated. portupgrade comes with a lot of options for different use cases, the most important ones will be presented below.
If you want to upgrade only a certain application, not the complete database, use portupgrade pkgname, include the flags -r if portupgrade should act on all those packages depending on the given package as well, and -R to act on all packages required by the given packages. To use packages instead of ports for installation, provide -P and to just fetch distfiles without building or installing anything, use -F. For further information see portupgrade(1).
注: It is important to regularly update the package database using pkgdb -F to fix inconsistencies, especially when portupgrade asks you to. Do not abort portupgrade while it is updating the package database, this will leave you an inconsistent database.
Other utilities exist which will do this, check out the ports/sysutils directory and see what you come up with.