By Mohamad Wael

Posted :

FreeBSD Pkg Cheat sheet

Information / Searching for available packages:


pkg search --search name ere_regex_pattern
# Matches the extended regular expression
# pattern, against remote packages names,
# and prints, the matching packages.
# For example:
pkg search --search name nginx
# if you want, full information to be displayed,
# you can use:
pkg search --search name --full ere_regex_pattern
# For example:
pkg search --search name --full '^nginx$'


pkg search --search pkg-name ere_regex_pattern
# Searches the package name-version,
# for the extended regular expression,
# and prints matching results.
# For full, instead of brief information,
# to be displayed, you can use:
pkg search --search pkg-name --full ere_regex_pattern
# Example:
pkg search --search pkg-name --full 'goog.?e'


pkg search --search description ere_regex_pattern
# Searches the packages descriptions
# for the extended regular expression,
# this will print matching results with full
# details.
# If you wish to print matching results,
# with quite details, you can use:
pkg search --search description --quiet ere_regex_pattern
# Example:
pkg search --search description --quiet 'goog?e'


pkg rquery %dn-%dv a_package_name
# Lists the packages, which are
# the dependencies, of the
# named packages.


pkg rquery %B a_package_name
# Lists the required shared libraries,
# of the named packages.


pkg rquery %rn-%rv a_package_name
# Lists the packages, which depend,
# on the named packages.


pkg rquery %b a_package_name
# Lists the shared libraries, provided
# by the named packages.


pkg rquery %Ok:%Ov a_package_name
# Lists the options, of the named
# packages, as in, if nginx has
# HTTP_PERL on or off

Installing / Downloading packages:

pkg install a_package_name
# Installs, one or more named
# package, from the configured
# repositories.
# To auto confirm any prompt,
# you can use --yes, as in:
pkg install --yes a_package_name


pkg fetch --output location a_package_name
# Downloads the named packages,
# into a directory called: All
# which is created, inside the
# specified location.
# To additionally download the
# dependencies, you can use:
# --dependencies, as in:
pkg fetch --dependencies --output location a_package_name


pkg add path_to_package
# Installs a package, located,
# on the specified path.
# For example:
pkg add ~/nano-5.9.pkg


pkg add a_remote_package_location
# Installs a package, located
# remotely, for example,
# on an HTTP, or FTP server.
# Example:
pkg add https://packages.ntop.org/FreeBSD/FreeBSD:13:amd64/latest/ntop-1.0.pkg

Updating / Upgrading:

pkg update
# Updates, what packages are
# available.
# Using pkg install and pkg upgrade
# will automatically update, what
# packages are available.


pkg upgrade
# Upgrades all installed packages.
# To upgrade only a specific package,
# you can use:
pkg upgrade a_package_name
# To auto confirm any prompt, you
# can use the --yes option, as in:
pkg upgrade --yes

Deleting / Cleaning packages:

pkg delete a_package_name
# Deletes the named packages,
# and the depending packages,
# which would have their dependencies
# missing.
# To auto confirm any prompt, you
# can use --yes, as in:
pkg delete --yes a_package_name


pkg autoremove
# Removes orphan packages.
# An orphan package, is a
# package, which was installed,
# as part of a dependency, so to
# fulfill, the dependencies requirements,
# for other packages, and which is no
# longer needed, as in, the package,
# which required it, was removed.
# If you want, to get an idea, of what
# orphan packages, should be removed,
# without actually doing that, you can use:
pkg autoremove --dry-run


pkg clean
# cleans the cache, which is
# the downloaded packages.
# When not using the --all option,
# this will only remove, the
downloaded packages, which are
# no longer needed, because of
# the availability of newer versions,
# or because they are no longer offered.
# you can use --all to remove all
# downloaded packages, as in:
pkg clean --all
# If you want to get an idea, of what
# would be cleaned, without the actual
# cleaning, you can use the --dry-run
# option, as in:
pkg clean --dry-run
# To auto confirm any prompt, you
# can use the --yes option, as in:
pkg clean --yes

Information about installed packages:

pkg info --all
# Lists all the installed packages,
# with a brief description.


pkg info a_package_name
# Displays information, about the named
# package, such as its name, version,
# when it was installed, its origin,
# prefix, category, comment, options,
# the libraries it provides, and requires,
# its size, and description...


pkg info --size a_package_name
# Prints the size of the files,
# installed by a package


pkg info --pkg-message a_package_name
# Display the named packages message.
# The message, is just a note, displayed
# after having installed a package,
# and which might contain, some
# useful information.


pkg info --dependencies a_package_name
# Displays the packages, on which,
# the named installed packages,
# depends.
# Example:
pkg info --dependencies nginx mariadb103-server


pkg info --required-shlibs a_package_name
# Prints the shared libraries, required
# by the named packages.


pkg info --required-by a_package_name
# Prints the packages, which require,
# the named packages.


pkg info --provided-shlibs a_package_name
# Prints the shared libraries,
# provided, by the named packages.


pkg info --list a_package_name
# Lists the files, installed,
# by the named packages


pkg which a_file_path
# Lists the packages, for which
# the named files, belong.


pkg shlib a_library_name
# For a shared library, lists the
# package, which provides it,
# and the packages, which are
# depending on it.
# For example:
pkg shlib libiconv.so.2


pkg query --evaluate '%a =0' %n
# Lists packages, which were
# explicitly installed.


pkg query --evaluate '%a =1' %n
# Lists the packages, which
# were not explicitly installed,
# for example, installed as being
# part of a dependency.


pkg query --evaluate '%m =ports@FreeBSD.org' %n
# List packages which have no
# maintainers.


pkg version --verbose --remote --not-like =
# List installed packages, which
# need to be upgraded.


pkg version --verbose --ports --not-like =
# Lists installed packages, which
# version, is not the same, as
# the port version.


pkg autoremove --dry-run
# Lists packages, marked to be
# autoremoved, so the packages,
# which are no longer needed, and
# which were installed, as part of a
# dependency.

Locking / Unlocking packages:

pkg lock a_package_name
# Locks the named packages, as in
# making impossible, upgrading,
# removal, or downgrading...
# Additionally, this affects packages,
# that the named packages,
# depends upon.
# --yes can be used, for auto
# accepting any prompt, as in:
pkg lock --yes a_package_name


pkg lock --show-locked
# Shows all locked packages.


pkg unlock a_package_name
# Unlocks the named packages.
# --yes can be used, for auto
# confirmation, as in:
pkg unlock --yes a_package_name

Auditing / Checking packages:

pkg audit --fetch --recursive
# Fetches the vulnerabilities list,
# and checks for packages, which
# have vulnerabilities, and displays if
# any, these packages, and packages
# which depends upon them.


pkg check --dependencies a_package_name
# Checks and installs, the named
# packages, missing dependencies.
# if no package names are specified,
# all packages dependencies are
# verified.
# You can use --dry-run if you do not
# want to install, the missing
# dependencies, as in:
pkg check --dependencies --dry-run a_package_name


pkg check --checksums a_package_name
# Checks, if the named packages,
# have an invalid checksums.
# if no package names are specified,
# all packages checksums, are verified
# for validity.

Stats / Configuration details:

pkg stats
# Displays statistics, such as
# the number of installed packages,
# the disk space used by the installed
# packages, the number of
# repositories, and the number of
# available packages ..


pkg config PKG_CACHEDIR
# Prints the location, of pkg
# cache directory, in
# other words, the directory
# where packages, are downloaded
# to.


pkg config REPOS_DIR
# Lists where repositories
# configuration files, are
# located.
# pkg configuration file, is
# located in:
/usr/local/etc/pkg.conf
# The pkg config command,
# reads information from
# this file.


pkg config ALIAS
# This will list all the defined
# aliases.
# Users, can define aliases to be
# used by pkg. An example of an
# alias, is: all-depends, so instead
# of doing:
pkg query %dn-%dv a_package_name
# to get the dependencies of a
# package, you can use:
pkg all-depends a_package_name
# to get the dependencies of the
# package.

Installing pkg

$ su -
$ /usr/sbin/pkg -v