- BSD Adventures - http://bsdadventures.com -

How to Install Software in FreeBSD

There are two ways to install software packages from the official FreeBSD repositories –
pkg and ports.

pkg

pkg lets you install software as pre-compiled binaries. It’s similar to the yum and apt package managers used in mainstream Linux distros. pkg is the easiest system to use if you don’t need the very latest version of the software. Like the other package managers you’re stuck with the default compile options. This is fine for the majority of use cases. Note that pkg is only available in FreeBSD 10 and newer.

The local pkg database should be updated before doing anything with it:

pkg update

To install a software package, simply type:

pkg install packagename

Installing apache24 with pkg

where packagename is the package’s name in the repository like apache24 or mariadb101. Many packages have multiple version branches and you need to specify which one to use. To see what’s available, type:

pkg search packagename

For example, apache would return apache branches and modules that are available.

ports

The ports system doesn’t have an equivalent in the mainstream Linux world. It’s like package managers in that it will take care of dependencies and keep track of what’s installed. The difference is that instead of just installing a binary, the package is compiled on your system. You can change any compile time options with an easy to use menu when you install a package.

The version in the ports also tends to be newer because the packages don’t need to be compiled by the package maintainer. The ports system gives you much more control over what’s installed. You can add or remove features as needed without having to locate and download the source code yourself.

The ports system, or ports tree, is a copy of the base files needed to compile all of the available packages. Typically these are just makefiles and some support files. The actual source code is not stored locally, just the references to where it’s located. When you request a package to be installed, the makefiles are consulted and the source code is pulled from the web server where the source code is hosted. If any compile time options can be set, the user is prompted. The defaults are already checked so you can just proceed if you don’t need to change anything.

The ports tree’s advantage is also its disadvantage. Because the packages need to be compiled, installation will take a lot longer than a standard binary.

If ports was not selected when FreeBSD was installed, it can be added to the system with:

portsnap fetch
portsnap extract

Even if ports was already installed, be sure to update the local copy to make sure the newest versions are available:

portsnap auto

The ports are located in /usr/ports. Ports are divided into categories like www and lang. You can search freshports.org [1] to find where the desired package is.

List of package categories in the ports tree

Once you’ve located the package, browse to the directory. For example:

cd /usr/ports/www/apache24

Once in the directory, compile the package with:

make install

The process is automated from here. You will be prompted if a package has compile options you can specify:

Selecting compile options for FreeBSD ports

Whether pkg or ports are used, the software package will be immediately available after the process completes. Some packages that install services are not enabled by default. Check the package’s documentation to see if there are any additional steps that need to be performed to get it up and running.

Be sure to update your packages to get new features as well as bug and security fixes. Check this post for details on how to do that [2].