Package management in FreeBSD is usually handled by using one of two methods:  pkg_add and ports.  Technically pkg_add is package management, whereas ports are a means by which to compile source code tailor made for your machine.

A package in technical nomenclature is a precompiled package, usually a binary file.  It’s compiled to run on as many systems as possible, whereas compiling source code will create a binary that should run as best as possible on your server.  While I will explain pkg_add, I strongly suggest you use the BSD ports system.

In order to install a package using pkg_add, as root you simply invoke pkg_add -r ‘packagename’.  The -r tells pkg_add to fetch the package remotely from repositories.  For example, to install screen:

pkg_add -r screen

This will download and install the package for you.  In order to remove packages, conversely you will use pkg_delete.  With pkg_delete you need to know the exact package name and version, so you would type:

pkg_info | grep screen

which will return the exact version.  Use pkg_delete and that exact package name and version number like this:

pkg_delete screen-4.0.2_2

It’s better to not mix ports and pkg_add.  Packages are installed by means of versioning, where ports are built based on your server’s ports system. 

We usually install ports and base on all of our FreeBSD servers, and nothing else unless otherwise specified.  To begin using ports, you’ll need to log into the server as root.  FreeBSD installs ports to /usr/ports, so to begin navigate there.  Since there are a whole lot of ports, you’ll need to identify where your desired port is within the directory hierarchy.  I do this by means of the find command.  For example, if I wanted to install screen from ports, I would do this:

find /usr/ports -name ‘screen’

and the resulting line, after a bit of searching, is:

/usr/ports/sysutils/screen

Now that I know where the port is, I’ll cd into /usr/ports/sysutils and type

make install clean

What happens now?  You may be presented with a menu with options depending on the port that you’re installing.  After that, FreeBSD makes the package (compiles it), installs it to it’s proper location, and cleans out any files that aren’t necessary any longer.  To remove an installed port, you use pkg_delete just like you would if you had used pkg_add.  Then the big question comes, “How do I maintain my installed ports?”

On all FreeBSD systems I run, two of the first ports I install are portaudit and portupgrade.  They can both be found in /usr/ports/ports-mgmt and are installed like any other port.  Once installed, they give you the ability to audit your ports for security issues and upgrade ports when a new one is available.  You use them like this:

portaudit -F -a

If any of your ports fail the audit, use pkg_delete to remove them, then build them from a safe version.  To upgrade ports that are already installed on your server, install portupgrade then type:

/usr/local/sbin/pkgdb -F

which will compile a database of currently installed ports, then

/usr/local/sbin/portupgrade -a which will upgrade any ports which have newer versions available.

With these techniques in mind you can keep a very updated and stable FreeBSD server.  For further information, please visit the FreeBSD project Ports web page using the URL as follows:

http://www.freebsd.org/doc/en/books/handbook/ports-using.html