How to Build Software from the FreeBSD Ports Collection
In this article
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(orportsnapon 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_PROXYin the environment before building. - Optionally,
ccacheto speed up repeated builds.
Step-by-Step
- 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/portsas root. If/usr/portsalready exists, usegit -C /usr/ports pullinstead. On older releases that still shipportsnap, runportsnap fetch extract(first time) orportsnap fetch update. Menu labels and command availability differ slightly by FreeBSD version. - Verify the tree. Run
ls /usr/ports. You should see category directories such aseditors,www,security, and aMkdirectory containing the build framework. If the directory is empty, the clone failed — check network and DNS. - Find the port you want. Use
whereisor search the tree, for examplemake -C /usr/ports search name=nginx, or browse/usr/ports/www/nginx. The port directory name is the package name; the version is in theMakefile. - Change into the port directory. For example
cd /usr/ports/www/nginx. Every port lives in exactly one category directory. - Review the options. Run
make configto 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. - Build and install. Run
make install clean. The system will fetch the distfile, verify its checksum, apply patches, configure, compile, and install.cleanremoves the work directory afterward to save space. Expect this to take minutes to hours depending on the port and your CPU. - Confirm the install. Run
pkg info | grepor 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. - Keep the tree current. Periodically run
git -C /usr/ports pullso 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 installneeds root to write to/usr/local. - Skipping
make configand then wondering why optional features are missing; defaults are used silently if you never open the dialog. - Running
make installwithoutclean, 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 deleteif 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=yesto accept defaults non-interactively, useful for scripted builds. - Install
ccacheand setWITH_CCACHE_BUILD=yesin/etc/make.confto speed up rebuilds of the same or similar ports. - Set
WRKDIRPREFIXin/etc/make.confto move work directories to a larger disk. - Use
make missingto list dependencies before building, andmake fetchto download distfiles ahead of time. - For a batch of ports, consider
portmasterorpoudriererather than building one at a time by hand. - Read the port's
pkg-descrandMakefilebefore building to understand dependencies and options.
Troubleshooting
- Checksum mismatch: the distfile changed upstream or is corrupt. Delete the file under
/usr/ports/distfilesand re-runmake fetch. If it persists, update the ports tree. - Missing dependency: run
make missingand 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 cleanin the port directory and checkdf -h. MoveWRKDIRPREFIXif needed. - Permission denied: you are not root, or
/usr/localownership is wrong. Usesuorsudo. - 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.