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.
pkg-update Notes:
- It’s used to refresh repositories.
- To suppress output, use
-qor--quietflag.
Refresh the enabled repositories:
root@freebsd:~ # pkg update
Force refreshing the enabled repositories:
root@freebsd:~ # pkg update -f
--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
-Uor--no-repo-updateflag is used. - To assume yes as an answer to all confirmation questions in the upgrade process, use
-yor--yesflag. - To suppress output, use
-qor--quietflag. - If
-qand-nflags are used, a summary of the work list will be displayed.
Upgrade all installed packages:
root@freebsd:~ # pkg upgrade
Just fetch upgrades & detect possible conflicts:
root@freebsd:~ # pkg upgrade -F
--fetch-only.Simulate the upgrade process:
root@freebsd:~ # pkg upgrade -n
--dry-run.Upgrade one or more packages. (e.g: vim):
root@freebsd:~ # pkg upgrade vim
vim.Force upgrade or re-installation of installed packages:
root@freebsd:~ # pkg upgrade -f
--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
-Uor--no-repo-updateflag is used. - To produce less output, use
-qor--quietflag.
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
--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
--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
--comment.Same as
pkg search -S comment debian.Search for gnu.org in packages descriptions:
root@freebsd:~ # pkg search -D 'gnu.org'
--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
--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
--exact.Display a package’s information. (e.g: vim-8.2.0869):
root@freebsd:~ # pkg search -f vim-8.2.0869
--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
--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
--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
-Uor--no-repo-updateflag is used. - To assume yes as an answer to all confirmation questions in the installation process, use
-yor--yesflag. - To suppress output, use
-qor--quietflag. - If
-qand-nflags 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
/var/cache/pkg/. Even after the installation.Re-install an installed package. (e.g: vim):
root@freebsd:~ # pkg install -f vim
--force.Re-install an installed package and all its dependents. (e.g: vim):
root@freebsd:~ # pkg install -Rf vim
--recursive.Fetch a package and detect conflicts. (e.g: vim):
root@freebsd:~ # pkg install -F vim
--fetch-only.Install a package and mark it as automatic. (e.g: vim):
root@freebsd:~ # pkg install -A vim
--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
--ignore-missing.Simulate the installation process. (e.g: vim):
root@freebsd:~ # pkg install -n vim
--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-addorpkg-install. - By default it stores the downloaded packages in
/var/cache/pkg/. - It will automatically update/refresh the repository catalogues, unless the
-Uor--no-repo-updateflag is used. - To assume yes as an answer to all confirmation questions in the fetching process, use
-yor--yesflag. - To suppress output, use
-qor--quietflag.
Download all available updates:
root@freebsd:~ # pkg fetch -u
--available-updates.Download a package. (e.g: vim):
root@freebsd:~ # pkg fetch vim
/var/cache/pkg/.Download a package with all its dependencies. (e.g: vim):
root@freebsd:~ # pkg fetch -d vim
--dependencies.Download all available packages:
root@freebsd:~ # pkg fetch -a
--all.Download a package to custom folder. (e.g: vim):
root@freebsd:~ # mkdir /tmp/pkgs root@freebsd:~ # pkg fetch -o /tmp/pkgs/ vim
--output.pkg-info Notes:
- It’s used to display some information about packages.
- To suppress output and print only the requested information, use
-qor--quietflag.
Check whether or not a package is installed. (e.g: vim):
root@freebsd:~ # pkg info -a | grep -i vim
--all.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 $?
--exists.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
--regex.List installed packages (with dependencies):
root@freebsd:~ # pkg info -a
--all.List installed packages (without dependencies):
root@freebsd:~ # pkg query -e '%a = 0' %n-%v
--evaluate.-
%aReturns1if the matched package was automatically installed as a dependency of another package,0otherwise. -
%nName of the matched package. -
%vVersion of the matched package.
List installed packages as dependencies:
root@freebsd:~ # pkg query -e '%a = 1' %n-%v
- Keep in mind, if a package is manually installed with the
-Aflag, 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
--prefix.Get information about an installed package. (e.g: vim):
root@freebsd:~ # pkg info vim
- For full information, use
-for--full.
Get the total size of files installed by a package. (e.g: vim):
root@freebsd:~ # pkg info -s vim
--size.Display the installed package’s short description. (e.g: vim):
root@freebsd:~ # pkg info -I vim
--comment.List the dependencies of an installed package (e.g: vim):
root@freebsd:~ # pkg info -d vim
--dependencies.List an installed package’s dependents (e.g: vim)
root@freebsd:~ # pkg info -r vim
--required-by.List all files installed by a package (e.g: vim):
root@freebsd:~ # pkg info -l vim
--list-files.Display an installed package’s origin. (e.g: vim):
root@freebsd:~ # pkg info -o vim
--origin.Display an installed package’s message. (e.g: vim):
root@freebsd:~ # pkg info -D vim
--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
-yor--yesflag. - To suppress output except for the confirmatory questions, use
-qor--quietflag.
Lock a package. (e.g: vim):
root@freebsd:~ # pkg lock vim
Lock all installed packages:
root@freebsd:~ # pkg lock -a
--all.List the locked installed packages:
root@freebsd:~ # pkg lock -l
--show-locked.Unlock a package. (e.g: vim):
root@freebsd:~ # pkg unlock vim
Unlock all installed packages:
root@freebsd:~ # pkg unlock -a
--all.Check if there is any locked packages:
root@freebsd:~ # pkg lock --has-locked-packages
0, otherwise 1 is returned.pkg-delete Notes:
- It’s used to delete the installed packages from the database and the system.
pkg removeis an alias topkg 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
-yor--yesflag. - To suppress output, use
-qor--quietflag. - If
-qand-nflags 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
pkg delete vim py37-ranger.Force the deletion of a package. (e.g: vim):
root@freebsd:~ # pkg delete -f vim
--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
--all.Simulate the deletion process. (e.g: vim):
root@freebsd:~ # pkg delete -n vim
--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
-yor--yesflag. - To suppress most output, use
-qor--quietflag.
Remove all orphan installed packages:
root@freebsd:~ # pkg autoremove
Simulate the removal of orphan packages:
root@freebsd:~ # pkg autoremove -n
--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
-yor--yesflag. - To suppress most output, use
-qor--quietflag. - If both
-yand-qflags are used, all output will be suppressed.
Remove the old versions of packages cache:
root@freebsd:~ # pkg clean
Simulate the removal of packages cache:
root@freebsd:~ # pkg clean -n
--dry-run.Remove all packages cache:
root@freebsd:~ # pkg clean -a
--all.pkg-stats Notes:
- It’s used to display package database statistics.
- To suppress most output, use
-qor--quietflag. - To display disk space usage in bytes, use
-bor--bytesflag.
Get general packages statistics:
root@freebsd:~ # pkg stats
Get remote package database stats:
root@freebsd:~ # pkg stats -r
--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
--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 🙂

[…] FreeBSD Binary Package Manager Cheatsheet. http://unix.cafe/wp/en/2020/06/the-freebsd-binary-package-manager-cheatsheet/ […]
[…] реализована. The FreeBSD binary package manager cheatsheet. #cheat #шпаргалки […]