FreeBSD binary package manager cheatsheet

Let’s find out the essential commands that are needed to manage our binary packages in FreeBSD operating system. By looking into more than 70 practical pkg commands.

Keep in mind that a root‘s privileges are needed. To gain those privileges, either login as root with su if you have the root‘s password, or prefix each of the following command with sudo if your user is a sudoer. Except when you’re working with pkg query, pkg info or pkg stats as they don’t need the root‘s privileges.

UpdateupgradeSearchInstallFetchInfoLock/UnlockDeleteAuto RemoveCleanStatistics

pkg-update Notes:

  • It’s used to refresh repositories.
  • To suppress output, use -q or --quiet flag.

Refresh the enabled repositories:

root@freebsd:~ # pkg update
It simply synchronizes the catalogues of the enabled repositories from their servers.

Force refreshing the enabled repositories:

root@freebsd:~ # pkg update -f
Or by using the full-form flag --force.

pkg-upgrade Notes:

  • It’s used to install the newest versions available for one or more packages.
  • By default it stores the downloaded packages in /var/cache/pkg/.
  • It will automatically update/refresh the repository catalogues, unless the -U or --no-repo-update flag is used.
  • To assume yes as an answer to all confirmation questions in the upgrade process, use -y or --yes flag.
  • To suppress output, use -q or --quiet flag.
  • If -q and -n flags are used, a summary of the work list will be displayed.

Upgrade all installed packages:

root@freebsd:~ # pkg upgrade
It simply installs the newest available versions of the installed packages.

Just fetch upgrades & detect possible conflicts:

root@freebsd:~ # pkg upgrade -F
Or by using the full-form flag --fetch-only.

Simulate the upgrade process:

root@freebsd:~ # pkg upgrade -n
Or by using the full-form flag --dry-run.

Upgrade one or more packages. (e.g: vim):

root@freebsd:~ # pkg upgrade vim
It simply installs the newest available version of the vim.

Force upgrade or re-installation of installed packages:

root@freebsd:~ # pkg upgrade -f
Or by using the full-form flag --force.

pkg-search Notes:

  • It’s used to search for a package in the repositories catalogues.
  • It will automatically update/refresh the repository catalogues, unless the -U or --no-repo-update flag is used.
  • To produce less output, use -q or --quiet flag.

Search for a package. (e.g: vim):

root@freebsd:~ # pkg search vim

Search for packages start with vim-:

root@freebsd:~ # pkg search '^vim-'

Search for two packages. (e.g: neofetch and screenfetch):

root@freebsd:~ # pkg search 'neofetch|screenfetch'

Since both of those packages end with fetch, we can make it like:

root@freebsd:~ # pkg search '(neo|screen)fetch'

Search for packages start with any alpha-character, and followed by top:

root@freebsd:~ # pkg search '^[a-z]top'

Display a package’s downloadable URL. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -Q url vim-8.2.0869
Or by using the full-form flag --query-modifier.

Display a package’s size. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -Q pkg-size vim-8.2.0869

Display a package’s size after getting unpacked and installed. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -s vim-8.2.0869
Or by using the full-form flag --size.
Same as pkg search -Q size vim-8.2.0869.

Display a package’s maintainer email. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -Q maintainer vim-8.2.0869

Display a package’s categories. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -Q categories vim-8.2.0869

Display a package’s options. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -Q options vim-8.2.0869

Display a package’s dependents. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -Q required-by vim-8.2.0869

Search for debian in packages comments:

root@freebsd:~ # pkg search -c debian
Or by using the full-form flag --comment.
Same as pkg search -S comment debian.

Search for gnu.org in packages descriptions:

root@freebsd:~ # pkg search -D 'gnu.org'
Or by using the full-form flag --description.
Same as pkg search -S description 'gnu.org'.

Search for a package’s dependencies. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -d vim-8.2.0869
Or by using the full-form flag --depends-on.
Same as pkg search -Q depends-on vim.

Search for exact package. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -e vim-8.2.0869
Or by using the full-form flag --exact.

Display a package’s information. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -f vim-8.2.0869
Or by using the full-form flag --full.
Same as pkg search -Q full vim-8.2.0869.

Display a package’s origin. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -o vim-8.2.0869
Or by using the full-form flag --origins.
Same as pkg search -L origin vim-8.2.0869.

Display where a package will be installed in. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -p vim-8.2.0869
Or by using the full-form flag --prefix.
Same as pkg search -Q prefix vim-8.2.0869.

Display which repository provides a package. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -Q repository vim-8.2.0869

Display a package’s project URL. (e.g: vim-8.2.0869):

root@freebsd:~ # pkg search -Q www vim-8.2.0869

pkg-install Notes:

  • It’s used for installation of packages from package repositories or local archives.
  • By default it stores the downloaded packages in /var/cache/pkg/.
  • It will automatically update/refresh the repository catalogues, unless the -U or --no-repo-update flag is used.
  • To assume yes as an answer to all confirmation questions in the installation process, use -y or --yes flag.
  • To suppress output, use -q or --quiet flag.
  • If -q and -n flags are used, packages that are going to be installed, upgraded or deleted, will always be shown.

Install a package. (e.g: vim):

root@freebsd:~ # pkg install vim
Note that it’ll keep the downloaded package in /var/cache/pkg/. Even after the installation.

Re-install an installed package. (e.g: vim):

root@freebsd:~ # pkg install -f vim
Or by using the full-form flag --force.

Re-install an installed package and all its dependents. (e.g: vim):

root@freebsd:~ # pkg install -Rf vim
Or by using the full-form flag --recursive.

Fetch a package and detect conflicts. (e.g: vim):

root@freebsd:~ # pkg install -F vim
Or by using the full-form flag --fetch-only.

Install a package and mark it as automatic. (e.g: vim):

root@freebsd:~ # pkg install -A vim
Or by using the full-form flag --automatic.

Which means if there are no packages depend on vim and the command pkg autoremove is issued, then vim will be removed automatically.

Force installation of a package even with missing dependencies. (e.g: vim):

root@freebsd:~ # pkg install -m vim
Or by using the full-form flag --ignore-missing.

Simulate the installation process. (e.g: vim):

root@freebsd:~ # pkg install -n vim
Or by using the full-form flag --dry-run.

Which prints the list of changes to packages, without actually applying them.

pkg-fetch Notes:

  • It is used for downloading binary packages without installing them automatically.
  • It’s useful for offline uses or to manually interact with package’s contents.
  • Downloaded packages can be installed by using pkg-add or pkg-install.
  • By default it stores the downloaded packages in /var/cache/pkg/.
  • It will automatically update/refresh the repository catalogues, unless the -U or --no-repo-update flag is used.
  • To assume yes as an answer to all confirmation questions in the fetching process, use -y or --yes flag.
  • To suppress output, use -q or --quiet flag.

Download all available updates:

root@freebsd:~ # pkg fetch -u
Or by using the full-form flag --available-updates.

Download a package. (e.g: vim):

root@freebsd:~ # pkg fetch vim
It’ll be downloaded to /var/cache/pkg/.

Download a package with all its dependencies. (e.g: vim):

root@freebsd:~ # pkg fetch -d vim
Or by using the full-form flag --dependencies.

Download all available packages:

root@freebsd:~ # pkg fetch -a
Or by using the full-form flag --all.

Download a package to custom folder. (e.g: vim):

root@freebsd:~ # mkdir /tmp/pkgs
root@freebsd:~ # pkg fetch -o /tmp/pkgs/ vim
Or by using the full-form flag --output.

pkg-info Notes:

  • It’s used to display some information about packages.
  • To suppress output and print only the requested information, use -q or --quiet flag.

Check whether or not a package is installed. (e.g: vim):

root@freebsd:~ # pkg info -a | grep -i vim
Or by using the full-form flag --all.
If there is no output, it means vim is not installed, Otherwise it is.

Another method to check the existence of a package. (e.g: vim):

root@freebsd:~ # pkg info -e vim ; echo $?
Or by using the full-form flag --exists.
If vim is installed and registered in the database, it’ll return 0, Otherwise 1 is returned.

Alternative way to know if a package is installed. (e.g: vim):

root@freebsd:~ # pkg info -x vim
Or by using the full-form flag --regex.

List installed packages (with dependencies):

root@freebsd:~ # pkg info -a
Or by using the full-form flag --all.

List installed packages (without dependencies):

root@freebsd:~ # pkg query -e '%a = 0' %n-%v
Or by using the full-form flag --evaluate.
  • %a Returns 1 if the matched package was automatically installed as a dependency of another package, 0 otherwise.
  • %n Name of the matched package.
  • %v Version of the matched package.

List installed packages as dependencies:

root@freebsd:~ # pkg query -e '%a = 1' %n-%v
Already explained above.
  • Keep in mind, if a package is manually installed with the -A flag, it will be shown here too even it’s not a dependency.

Display where a package is installed. (e.g: vim):

root@freebsd:~ # pkg info -p vim
Or by using the full-form flag --prefix.

Get information about an installed package. (e.g: vim):

root@freebsd:~ # pkg info vim
  • For full information, use -f or --full.

Get the total size of files installed by a package. (e.g: vim):

root@freebsd:~ # pkg info -s vim
Or by using the full-form flag --size.

Display the installed package’s short description. (e.g: vim):

root@freebsd:~ # pkg info -I vim
Or by using the full-form flag --comment.

List the dependencies of an installed package (e.g: vim):

root@freebsd:~ # pkg info -d vim
Or by using the full-form flag --dependencies.

List an installed package’s dependents (e.g: vim)

root@freebsd:~ # pkg info -r vim
Or by using the full-form flag --required-by.

List all files installed by a package (e.g: vim):

root@freebsd:~ # pkg info -l vim
Or by using the full-form flag --list-files.

Display an installed package’s origin. (e.g: vim):

root@freebsd:~ # pkg info -o vim
Or by using the full-form flag --origin.

Display an installed package’s message. (e.g: vim):

root@freebsd:~ # pkg info -D vim
Or by using the full-form flag --pkg-message.

pkg-lock Notes:

  • It’s used to lock or unlock installed packages.
  • Locking a package, means make that package immune against re-installation, upgrading, downgrading or deletion.
  • All locked package’s dependents and dependencies will have the same effect. It may even forbid the installation of some packages if they require different version of the locked package. So, use it carefully.
  • To assume yes as an answer to all confirmation questions in the lock or unlock process, use -y or --yes flag.
  • To suppress output except for the confirmatory questions, use -q or --quiet flag.

Lock a package. (e.g: vim):

root@freebsd:~ # pkg lock vim

Lock all installed packages:

root@freebsd:~ # pkg lock -a
Or by using the full-form flag --all.

List the locked installed packages:

root@freebsd:~ # pkg lock -l
Or by using the full-form flag --show-locked.

Unlock a package. (e.g: vim):

root@freebsd:~ # pkg unlock vim

Unlock all installed packages:

root@freebsd:~ # pkg unlock -a
Or by using the full-form flag --all.

Check if there is any locked packages:

root@freebsd:~ # pkg lock --has-locked-packages
If there is at least one locked package, it’ll return 0, otherwise 1 is returned.

pkg-delete Notes:

  • It’s used to delete the installed packages from the database and the system.
  • pkg remove is an alias to pkg delete, which means both of them do the same exact job.
  • To assume yes as an answer to all confirmation questions in the deletion process, use -y or --yes flag.
  • To suppress output, use -q or --quiet flag.
  • If -q and -n flags are both used, a list of packages to be deleted will be displayed.

Delete an installed package. (e.g: vim):

root@freebsd:~ # pkg delete vim
To delete more than one package at a time, separate the packages with space. e.g: pkg delete vim py37-ranger.

Force the deletion of a package. (e.g: vim):

root@freebsd:~ # pkg delete -f vim
Or by using the full-form flag --force.

If -f and -a flags are used together, it will cause pkg package to be deleted as well.

Delete all installed packages except pkg:

root@freebsd:~ # pkg delete -a
Or by using the full-form flag --all.

Simulate the deletion process. (e.g: vim):

root@freebsd:~ # pkg delete -n vim
Or by using the full-form flag --dry-run.

pkg-autoremove Notes:

  • It’s used to remove the orphan installed packages.
  • To assume yes as an answer to all confirmation questions in the auto-removal process, use -y or --yes flag.
  • To suppress most output, use -q or --quiet flag.

Remove all orphan installed packages:

root@freebsd:~ # pkg autoremove
It’ll simply remove all unneeded packages anymore, which were installed during dependency resolution.

Simulate the removal of orphan packages:

root@freebsd:~ # pkg autoremove -n
Or by using the full-form flag --dry-run.

pkg-clean Notes:

  • It’s used to delete the previous downloaded packages.
  • By default the packages cache is stored in /var/cache/pkg/.
  • To assume yes as an answer to all confirmation questions in the installation process, use -y or --yes flag.
  • To suppress most output, use -q or --quiet flag.
  • If both -y and -q flags are used, all output will be suppressed.

Remove the old versions of packages cache:

root@freebsd:~ # pkg clean
It removes the old versions of packages, and the packages that are no longer provided.

Simulate the removal of packages cache:

root@freebsd:~ # pkg clean -n
Or by using the full-form flag --dry-run.

Remove all packages cache:

root@freebsd:~ # pkg clean -a
Or by using the full-form flag --all.

pkg-stats Notes:

  • It’s used to display package database statistics.
  • To suppress most output, use -q or --quiet flag.
  • To display disk space usage in bytes, use -b or --bytes flag.

Get general packages statistics:

root@freebsd:~ # pkg stats
Displays remote and local package databases stats.

Get remote package database stats:

root@freebsd:~ # pkg stats -r
Or by using the full-form flag --remote.

Count the installed packages as dependencies:

root@freebsd:~ # pkg query -e '%a = 1' %n-%v | wc -l

Get local package database stats:

root@freebsd:~ # pkg stats -l
Or by using the full-form flag --local.

Count the installed packages (with dependencies):

root@freebsd:~ # pkg info -a | wc -l

Count the installed packages (without dependencies):

root@freebsd:~ # pkg query -e '%a = 0' %n-%v | wc -l

Just to be clear you can install whatever package you want I just used vim as an example because it was the easiest 🙂

By DeaDSouL

A big fan of UNIX & Linux.. Who adores programming..

2 thoughts on “The FreeBSD binary package manager cheatsheet”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.