While installing software through Packages does not allow us to customize our install the way installing from Ports does, Packages nevertheless have their uses. For starters, installing a piece of software through Packages takes a small fraction of the time it takes to install through Ports. That is because whereas when you install through Ports, your system has to compile the application from source, installing through Packages extracts and registers precompiled binaries on your system.
Sometimes packages can provide major headaches when they were built on another system with different versions of libs that you have on your system or with options set that you don't want. Just try installing a 4.x Package in 5.x and see what I mean. Furthermore, because most packages are likely built with make options that differ from your make.conf, you may find yourself with additional packages when you use pkg_add. That said, there may still be times when you wish to install a piece of software from its binary Package rather than through Ports. OpenOffice.org is a magnificent example of this. Installing this piece of software through Ports requires approximately 4 GB of hard disk space (for all of the temporary files used during the compiling process) and an overnight wait even on pretty decent hardware. By contrast, it takes only a few minutes to install this same application by binary Package.
In order to make packages useful, we need to tell our system to use more up to date packages, which are associated with the STABLE branch of FreeBSD. Otherwise, if you install a RELEASE version of FreeBSD, all of your packages will be those compiled at the time of release, so they will be very out of date. As user:
vi ~/.xinitrc
Now add these few lines:
rand13=$(( (`fortune | cksum | cut -f1 -d" "` % 13) + 1))
version=$(uname -a | cut -d' ' -f3 | sed "s/./& /g" | awk '{print $1}')
export PACKAGESITE=ftp://ftp$rand13.freebsd.org/pub/FreeBSD/ports/i386/packages-$version-stable/Latest/
What this will do is choose a random ftp server between 1 and 13 for the packagesite (to not strain any specific server) and then it will glean your version number of FreeBSD from uname. You could also just insert and modify the last line manually, but then if you upgraded major versions and forgot to change it, you might wonder why pkg_add is broken.
With all of that out of the way, we can get on to installing packages. Installing an application through Packages is pretty straightforward. If, for example, we wanted to install Firefox through Packages, as root type:
pkg_add -r firefox
This will download the most recent version of Firefox available as a binary package, and in a short process install it on your system.
Sometimes (due to the nuances of indexing on the server) this command will fail to retrieve a Package. If that is the case, or for any other reason, we can manually download a Package before attempting to install it. While this can also be done with a web browser, I like command-line tools so we're going to download this from the command line:
ftp -a ftp2.freebsd.org
You should be greeted with a message like this:
fongsaiyuk% ftp -a ftp2.freebsd.org
Connected to ftp2.us.freebsd.org.
220 ftp2.us.freebsd.org FTP server (Version 6.00LS) ready.
331 Guest login ok, send your email address as password.
230 Guest login ok, access restrictions apply.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
At the ftp> prompt, we can issue commands to navigate our way around the server. This can be done very similarly to browsing your local system:
cd pub/FreeBSD/ports/packages/
ls
This will look a lot like the directory structure of our local /usr/ports directory. Navigation is pretty similar. For Firefox:
cd www
ls
This will look a little bit different from what you're used to in Ports. Rather than a list of directories corresponding to available applications, this will generate a long list of packages, usually in *.tbz format. (Tar archives compressed using bzip). As of 4 July 2006, the package for Firefox listed on the server was "firefox-1.5.0.4,1.tbz"
To download this package, type:
get firefox-1.5.0.4,1.tbz
This will retrieve the package and download it to our current directory (your Home directory if you just opened a new terminal window).
ftp> get firefox-1.5.0.4,1.tbz
local: firefox-1.5.0.4,1.tbz remote: firefox-1.5.0.4,1.tbz
229 Entering Extended Passive Mode (|||50826|)
150 Opening BINARY mode data connection for 'firefox-1.5.0.4,1.tbz' (11526001 bytes).
20% |******* | 2256 KB 250.68 KB/s 00:35 ETA
Nice, huh? You even get a slick little download status indicator. Wouldn't it be great if cp gave you something like this?
Once your download has finished, disconnect from the server:
exit
A successful download and exit should look like this:
ftp> get firefox-1.5.0.4,1.tbz
local: firefox-1.5.0.4,1.tbz remote: firefox-1.5.0.4,1.tbz
229 Entering Extended Passive Mode (|||58719|)
150 Opening BINARY mode data connection for 'firefox-1.5.0.4,1.tbz' (11526001 bytes).
100% |*************************************| 11255 KB 207.10 KB/s 00:00 ETA
226 Transfer complete.
11526001 bytes received in 00:54 (207.10 KB/s)
ftp> exit
221 Goodbye.
fongsaiyuk%
To install this package, make sure you're in the download directory (do a quick 'ls' to double-check if you're not sure), and as root:
pkg_add firefox-1.5.0.4,1.tbz
That should install the package, after which time you can delete the .tbz file we downloaded.
What if the Package refuses to install, complaining about one or more missing dependencies? While not necessarily recommended (because you'll probably have additional headaches trying to get the app to run post-install), to override error messages and install the Package anyway, type:
pkg_add -f firefox-1.5.0.4,1.tbz
NOTE that the "-f" flag can also be set in the case of a remote Package. Simply combine it with the "-r" flag:
pkg_add -rf firefox
Again, generally it is not a great idea to force an installation using the "-f" flag, but at times it can be very useful. Sometimes the Package wants old versions of dependencies that you already have installed on your system. Forcing the installation to proceed anyway can yield satisfactory results, if the application recognizes and uses the newer versions of its dependencies already installed on your system.