How to Patch Software Using the FreeBSD Ports Tree
In this article
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(runportsnap fetch extractif it is missing, or use git per the handbook). - Root privileges or
sudo. - The
patchanddiffutilities (in the base system). - A text editor such as
vioree. - The port name and category (for example,
editors/vim).
Step-by-Step
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 aMakefileand afiles/directory (or the lack of one) are present.Fetch the original source. Run
make fetchto download the upstream tarball. Then runmake extractto unpack it underwork/. This gives you a clean copy to compare against.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.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.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 withlessto ensure it contains only the intended changes and uses paths relative to the port's build directory (often the source root).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 examplecp /tmp/mypatch.diff files/patch-src_main.c. Thepatch-prefix is required for the ports framework to pick it up automatically.Clean and rebuild. Run
make cleanto remove old work files, thenmaketo 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 inspectwork/for rejects.Install the patched port. Once the build succeeds, run
make install(ormake reinstallif it is already installed). This replaces the installed files with the patched versions.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 runningmake clean, which deletes your changes. Always save the diff tofiles/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 cleanbefore rebuilding, so stale objects hide the effect of the patch.
Tips
- Use
make patchto apply existing patches and leave the source inwork/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
MakefileforPATCHFILESorEXTRA_PATCHESif you need to pull patches from elsewhere; local files infiles/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
Makefilecomments 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 patchto apply and inspect the result inwork/. - Patch ignored: Confirm the file is in
files/and starts withpatch-. Also check that the port'sMakefiledoes not override patch handling. - Installed files unchanged: Run
make reinstallrather thanmake installif the port was already installed, and verify the install log shows the new files. - Permission errors: Ensure you are root or using
sudoformake installand 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.