TechBuzzPro

How to Patch Software Using the FreeBSD Ports Tree

· 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

To patch software using the FreeBSD ports tree, you edit or add files in the port's files/ directory, then rebuild and reinstall the port so the patches are applied automatically. This is the standard way to fix a bug, adjust a default, or add a local change without touching the upstream source. You need the ports tree installed (via portsnap or git), the patch utility (part of the base system), and basic familiarity with a text editor. The steps below assume you are working as root or can use su/sudo; labels and paths can differ slightly by FreeBSD version, so check the port's Makefile if something looks unfamiliar.

What You'll Need

  • A FreeBSD system with the ports tree at /usr/ports (run portsnap fetch extract if it is missing, or use git per the handbook).
  • Root privileges or sudo.
  • The patch and diff utilities (in the base system).
  • A text editor such as vi or ee.
  • The port name and category (for example, editors/vim).

Step-by-Step

  1. Locate the port directory. Change into the port's directory: cd /usr/ports/<category>/<portname>. For example, cd /usr/ports/editors/vim. Confirm you are in the right place by checking that a Makefile and a files/ directory (or the lack of one) are present.

  2. Fetch the original source. Run make fetch to download the upstream tarball. Then run make extract to unpack it under work/. This gives you a clean copy to compare against.

  3. Create a working copy for editing. Copy the extracted source to a scratch directory, for example cp -R work/<source-dir> /tmp/<portname>-orig. Keep this pristine copy; you will diff against it later.

  4. Make your changes. Edit the files in the scratch copy (or directly in work/ if you prefer) to implement the fix. Keep changes minimal and focused on one issue per patch file; this makes future updates easier.

  5. Generate a unified diff. From the directory containing both the original and modified trees, run diff -u -r <original-dir> <modified-dir> > /tmp/mypatch.diff. Review the diff with less to ensure it contains only the intended changes and uses paths relative to the port's build directory (often the source root).

  6. Place the patch in the port's files directory. Create files/ if it does not exist: mkdir -p files. Copy your diff there with a descriptive name, for example cp /tmp/mypatch.diff files/patch-src_main.c. The patch- prefix is required for the ports framework to pick it up automatically.

  7. Clean and rebuild. Run make clean to remove old work files, then make to build with your patch applied. Watch the output for the patch being applied; if it fails, the build stops with an error and you can inspect work/ for rejects.

  8. Install the patched port. Once the build succeeds, run make install (or make reinstall if it is already installed). This replaces the installed files with the patched versions.

  9. Record the change. Keep a note of why the patch exists and which upstream version it targets. When the port is updated, re-check whether the patch still applies; if not, regenerate it against the new source.

Common Mistakes

  • Naming the patch file without the patch- prefix, so the ports framework ignores it.
  • Generating a diff with absolute paths; the patch must use paths relative to the build directory.
  • Editing files in work/ and then running make clean, which deletes your changes. Always save the diff to files/ first.
  • Including unrelated whitespace or formatting changes, which makes the patch harder to maintain and more likely to conflict on updates.
  • Forgetting to run make clean before rebuilding, so stale objects hide the effect of the patch.

Tips

  • Use make patch to apply existing patches and leave the source in work/ for inspection; this is handy when debugging a failed patch.
  • Keep one logical change per patch file and name it after the file it touches (for example, patch-src_foo.c).
  • Check the port's Makefile for PATCHFILES or EXTRA_PATCHES if you need to pull patches from elsewhere; local files in files/ are applied automatically.
  • Test the patched software before installing system-wide; run it from work/ if possible.
  • When submitting a patch upstream, follow the port's Makefile comments and the FreeBSD Porter's Handbook for formatting.

Troubleshooting

  • Patch fails to apply: The source may have changed. Re-extract with make clean extract, regenerate the diff against the current source, and replace the patch file.
  • Build errors after patching: Check the diff for context mismatches or missing includes. Use make patch to apply and inspect the result in work/.
  • Patch ignored: Confirm the file is in files/ and starts with patch-. Also check that the port's Makefile does not override patch handling.
  • Installed files unchanged: Run make reinstall rather than make install if the port was already installed, and verify the install log shows the new files.
  • Permission errors: Ensure you are root or using sudo for make install and for writing to /usr/ports.

Frequently Asked Questions

Do I need to rebuild the whole port after adding a patch?

Yes. The ports framework applies patches during the build, so run make clean then make and make install to ensure the patch takes effect.

Where should patch files be stored?

In the port's files/ directory, with names starting with patch-. The framework applies them automatically in alphabetical order.

Can I patch a port without root access?

You can edit files in a copy of the ports tree you own, but installing system-wide requires root. For testing, build in a user-writable directory and run the binary from work/.

What if my patch conflicts with an existing one?

Patches are applied in order; a conflict means the context no longer matches. Regenerate your patch against the source after all existing patches are applied, or adjust the order by renaming files.

How do I remove a patch later?

Delete the patch file from files/, then run make clean and rebuild/reinstall. The change will no longer be applied.

Does make patch install the software?

No. make patch only extracts the source and applies patches, leaving the tree in work/ for inspection. Use make install to install.