TechBuzzPro

How to Build Software from the FreeBSD Ports Collection

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You build software from the FreeBSD Ports Collection by installing the ports tree, moving into the directory of the port you want, and running make install clean as root. The ports system downloads the source, applies FreeBSD-specific patches, configures, compiles, and installs the program under /usr/local, giving you control over compile-time options that prebuilt packages do not expose. This guide assumes a working FreeBSD install with root or su access and a network connection.

What You'll Need

  • A FreeBSD system with a matching source tree or at least the ports tree (the source tree is needed only for kernel/world builds, not for ports).
  • Root privileges (or sudo) to install into /usr/local.
  • git (or portsnap on older releases) to fetch the ports tree.
  • Enough disk space — a single large port can need several gigabytes for build artifacts.
  • A working internet connection and correct DNS. If you are behind a proxy, set HTTP_PROXY/HTTPS_PROXY in the environment before building.
  • Optionally, ccache to speed up repeated builds.

Step-by-Step

  1. Fetch the ports tree. On current FreeBSD, the ports tree is a Git checkout. Run git clone --depth 1 https://git.FreeBSD.org/ports.git /usr/ports as root. If /usr/ports already exists, use git -C /usr/ports pull instead. On older releases that still ship portsnap, run portsnap fetch extract (first time) or portsnap fetch update. Menu labels and command availability differ slightly by FreeBSD version.
  2. Verify the tree. Run ls /usr/ports. You should see category directories such as editors, www, security, and a Mk directory containing the build framework. If the directory is empty, the clone failed — check network and DNS.
  3. Find the port you want. Use whereis or search the tree, for example make -C /usr/ports search name=nginx, or browse /usr/ports/www/nginx. The port directory name is the package name; the version is in the Makefile.
  4. Change into the port directory. For example cd /usr/ports/www/nginx. Every port lives in exactly one category directory.
  5. Review the options. Run make config to open the options dialog. Toggle features with the arrow keys and spacebar, then save. Options control optional dependencies and features (for example, SSL support or database backends). Choosing fewer options means fewer dependencies and a faster build.
  6. Build and install. Run make install clean. The system will fetch the distfile, verify its checksum, apply patches, configure, compile, and install. clean removes the work directory afterward to save space. Expect this to take minutes to hours depending on the port and your CPU.
  7. Confirm the install. Run pkg info | grep or check that the binary appears in /usr/local/bin. Configuration files usually land in /usr/local/etc, and the port's message (printed at the end of the build) tells you about services to enable or sample configs to copy.
  8. Keep the tree current. Periodically run git -C /usr/ports pull so you build against current versions and security fixes. Do not mix a stale ports tree with a freshly updated one in the same build session.

Common Mistakes

  • Building as a normal user — make install needs root to write to /usr/local.
  • Skipping make config and then wondering why optional features are missing; defaults are used silently if you never open the dialog.
  • Running make install without clean, leaving large work directories that fill the disk.
  • Mixing packages and ports for the same software without care — installing a port over a package of the same name can conflict. Remove the package first with pkg delete if you intend to replace it.
  • Forgetting to update the ports tree before building, which can pull outdated distfiles or fail checksum verification.
  • Ignoring the pkg-message printed after install; it often contains required post-install steps such as enabling a service in /etc/rc.conf.

Tips

  • Use make install clean BATCH=yes to accept defaults non-interactively, useful for scripted builds.
  • Install ccache and set WITH_CCACHE_BUILD=yes in /etc/make.conf to speed up rebuilds of the same or similar ports.
  • Set WRKDIRPREFIX in /etc/make.conf to move work directories to a larger disk.
  • Use make missing to list dependencies before building, and make fetch to download distfiles ahead of time.
  • For a batch of ports, consider portmaster or poudriere rather than building one at a time by hand.
  • Read the port's pkg-descr and Makefile before building to understand dependencies and options.

Troubleshooting

  • Checksum mismatch: the distfile changed upstream or is corrupt. Delete the file under /usr/ports/distfiles and re-run make fetch. If it persists, update the ports tree.
  • Missing dependency: run make missing and install what it lists, or let the build install dependencies automatically (the default).
  • Compile errors: check that your FreeBSD version matches what the port expects; some ports require a recent source tree or specific compiler. Read the error output for the failing header or library.
  • Disk full: run make clean in the port directory and check df -h. Move WRKDIRPREFIX if needed.
  • Permission denied: you are not root, or /usr/local ownership is wrong. Use su or sudo.
  • Port not found: the category or name is wrong, or the tree is stale. Search with make -C /usr/ports search name=.

Frequently Asked Questions

Do I need the FreeBSD source tree to build ports?

No. Ports build against installed headers and libraries. The source tree is needed only for kernel or world rebuilds.

Can I build ports as a non-root user?

You can compile as a normal user in many cases, but make install requires root to write to /usr/local. Use sudo make install clean.

How do I update a port I already built?

Update the ports tree, return to the port directory, and run make install clean again. Use portmaster or poudriere for bulk updates.

What is the difference between ports and packages?

Packages are prebuilt binaries installed with pkg install. Ports compile from source with your chosen options. Both install to /usr/local.

Why does the build take so long?

Compiling large software from source is CPU-bound. Fewer options, ccache, and a faster disk reduce build time.

Can I install a port without keeping the work directory?

Yes — make install clean removes the work directory after a successful install.