Managing packages in FreeBSD using pkg

In FreeBSD we can install programs by using pkg (for installation binary packages) and portsnap, svn (for installation from source codes).

pkg is the modern version of pkg_add, pkg_info… utilities which were on older versions of FreeBSD.

At the first time we start pkg without parameters, we are offered to install this package manager:

The package management tool is not yet installed on your system.
Do you want to fetch and install it now? [y/N]: y

The question must be answered “y” to install pkg. This may take some time.

Help can be viewed with the following command:

pkg help

Information about a specific command can be shown this way:

pkg help update

To update the package tree information (update FreeBSD repository catalogue), make this command:

pkg update

To update all installed packages we can use the following command:

pkg upgrade

To update a specific installed package, we must specify the name of this package:

pkg upgrade firefox

We can check the package availability in the repository with the following command. Also additional packages like extensions and plugins can be found:

pkg search mpv
mpv-0.29.1_5,1       Free and open-source general-purpose media player
gnome-mpv-0.16     Simple GTK+ frontend for mpv

List of all installed packages and their description:

pkg info

Install the package with this command, all dependencies will be installed:

pkg install mpv

To remove a package:

pkg delete nano

Remove all packages that were installed as dependencies and are no longer needed:

pkg autoremove

We can find out which package the specific file belongs to:

pkg which /usr/local/man/man1/st.1.gz
/usr/local/man/man1/st.1.gz was installed by sterm-0.8.2

Show file list for specific package:

pkg info -l dmenu

Download the database of known vulnerabilities in packages (~785 kB on my system):

pkg audit -F
0 problem(s) in the installed packages found.

Check the specific package in the FreeBSD repository for known vulnerabilities. The output is very huge because all versions of the package will be listed:

pkg audit firefox

Check all installed packages for validity of checksums of files included in packages:

pkg check -s -a

Check all installed packages for required dependencies. This is useful when something went wrong during the installation.:

pkg check -d -a

Remove old package archives from the cache. This will free some disk space:

pkg clean

Show the current status of packages in the FreeBSD repository and in our system:

pkg stats
Shows current state both for FreeBSD repository and system
pkg stats command in FreeBSD. Shows packages state both for FreeBSD repository and system

We can disable receiving updates for a specific package:

pkg lock firefox

To re-enable receiving updates for this package, use the following command:

pkg unlock firefox

Show a list of packages that are not allowed to receive updates:

pkg lock -l

As seen, pkg is definitely different from other package managers. It has commands that don’t exist in Linux package managers (like apt) and vice versa. For example, the repository is not flexible enough to not install unnecessary packages, such as developer documentation, certain plugins, and some other unnecessary packages. However, for example, I found a xorg-minimal package that is more lighter than xorg package. There is no such a lightweight package for xorg even in Debian-based repositories where we can disable recommended and suggested dependencies via apt.

In general, pkg is the easiest and fastest package manager I’ve dealt with.