Package manager is a collection of software tools used to install, remove upgrade, and manage programs in a computer system. Package management is an essential skill when working in a command-line interface. FreeBSD provides a pkg package manager to install and manage different packages via the command line.

Step 1 – Getting Help Information

Before working with a pkg package manager, we will show you how to use the pkg command with the help flag to find all sub-commands.

To see all available options of the pkg command, run the following command:

pkg help

You will see all supported sub-commands in the following output:

Commands supported:
	add            Compatibility interface to install a package
	alias          List the command line aliases
	annotate       Add, modify or delete tag-value style annotations on packages
	audit          Reports vulnerable packages
	autoremove     Removes orphan packages
	check          Checks for missing dependencies and database consistency
	clean          Cleans old packages from the cache
	config         Display the value of the configuration options
	create         Creates software package distributions
	delete         Deletes packages from the database and the system
	fetch          Fetches packages from a remote repository
	help           Displays help information
	info           Displays information about installed packages
	install        Installs packages from remote package repositories and local archives
	lock           Locks package against modifications or deletion
	plugins        Manages plugins and displays information about plugins
	query          Queries information about installed packages
	register       Registers a package into the local database
	remove         Deletes packages from the database and the system
	repo           Creates a package repository catalogue
	rquery         Queries information in repository catalogues
	search         Performs a search of package repository catalogues
	set            Modifies information about packages in the local database
	ssh            Package server (to be used via ssh)
	shell          Opens a debug shell
	shlib          Displays which packages link against a specific shared library
	stats          Displays package database statistics
	triggers       Execute deferred triggers
	unlock         Unlocks a package, allowing modification or deletion
	update         Updates package repository catalogues
	updating       Displays UPDATING information for a package
	upgrade        Performs upgrades of packaged software distributions
	version        Displays the versions of installed packages
	which          Displays which package installed a specific file

If you want to see the help information of any sub-command, run the following command:

pkg help search

You will see how to use the pkg command with the search sub-command:

NAME
     pkg search – search package repository catalogues

SYNOPSIS
     pkg search [-U] [-r reponame] [-S search] [-L label] [-Q query-modifier]
                [-Cegix] pattern
     pkg search [-cDdfopqRsU] [-r reponame] [-Cegix] pattern

     pkg search [--no-repo-update] [--repository reponame] [--search search]
                [--label label] [--query-modifier query-modifier]
                [--{case-sensitive,exact,glob,case-insensitive,regex}] pattern
     pkg search [--{comment,description,depends-on,full,origins,prefix}]
                [--{quiet,raw,size,no-repo-update}] [--repository reponame]
                [--{case-sensitive,exact,glob,case-insensitive,regex}]
                [--raw-format format] pattern

To see the help information about the install sub-command, run the following command:

pkg help install

You will see the following command:

NAME
     pkg install – install packages from remote package repositories or local
     archives

SYNOPSIS
     pkg install [-AfIMnFqRUy] [-r reponame] [-Cgix]
                 <pkg-origin|pkg-name|pkg-name-version> ...

     pkg install [--{automatic,force,no-scripts,ignore-missing}]
                 [--{dry-run,fetch-only,quiet,recursive,no-repo-update,yes}]
                 [--repository reponame]
                 [--{case-sensitive,glob,case-insensitive,regex}]
                 <pkg-origin|pkg-name|pkg-name-version> ...

Step 2 – Search Packages Using pkg Command

The basic syntax to search a package is shown below.

pkg search package-name

For example, to search for Nginx packages, run the following command:

pkg search nginx

You will see all Nginx packages in the following output:

nginx-1.24.0_12,3              Robust and small WWW server
nginx-devel-1.25.2_7           Robust and small WWW server
nginx-lite-1.24.0,3            Robust and small WWW server (lite package)
nginx-naxsi-1.24.0,3           Robust and small WWW server (plus NAXSI)
nginx-prometheus-exporter-0.11.0_2 Prometheus exporter for NGINX and NGINX Plus stats
nginx-ultimate-bad-bot-blocker-4.2020.03.2005_1 Nginx bad bot and other things blocker
nginx-vts-exporter-0.10.7_14   Server that scraps NGINX vts stats and export them via HTTP
p5-Nginx-ReadBody-0.07_1       Nginx embeded perl module to read and evaluate a request body
p5-Nginx-Simple-0.07_1         Perl 5 module for easy to use interface for Nginx Perl Module
p5-Test-Nginx-0.30             Testing modules for Nginx C module development
py39-certbot-nginx-2.6.0       NGINX plugin for Certbot

To see the information about a specific package, use the -f flag followed by the package name.

pkg search -f nginx-1.24.0_12,3

You will see the following output:

nginx-1.24.0_12,3
Name           : nginx
Version        : 1.24.0_12,3
Origin         : www/nginx
Architecture   : FreeBSD:13:amd64
Prefix         : /usr/local
Repository     : FreeBSD [pkg+http://pkg.FreeBSD.org/FreeBSD:13:amd64/quarterly]
Categories     : www
Licenses       : BSD2CLAUSE
Maintainer     : [email protected]
WWW            : https://nginx.com/
Comment        : Robust and small WWW server

Step 3 – Install Packages Using pkg Command

The basic syntax to install a package is shown below.

pkg install package-name

For example, to install an Nginx package, run the following command:

pkg install nginx

If you want to download the package without installing it on your system, run the following command:

pkg fetch nginx 

To download the package with all of its dependencies use the -d flag.

pkg fetch -d nginx 

You can see all fetched packages in the /var/cache/pkg directory.

ls /var/cache/pkg 

Output:

brotli-1.1.0,1.pkg				indexinfo-0.3.1~bd05368104.txz			nettle-3.9.1.pkg
brotli-1.1.0,1~2e5f90142a.pkg			libassuan-2.5.6.pkg				nettle-3.9.1~6bc5253893.pkg
e2fsprogs-1.46.2.txz				libassuan-2.5.6~7b204af578.pkg			nginx-1.24.0_12,3.pkg
e2fsprogs-1.46.2~a2abff02bc.txz			libedit-3.1.20221030,1.pkg			nginx-1.24.0_12,3~af8e35c324.pkg
e2fsprogs-1.46.2~b1851d3194.txz			libedit-3.1.20221030,1~02edf68058.pkg		npth-1.6.pkg
e2fsprogs-1.47.0.pkg				libffi-3.4.4.pkg				npth-1.6~55fe55fef2.pkg
e2fsprogs-1.47.0~196e9c9c54.pkg			libffi-3.4.4~3c80f31254.pkg			p11-kit-0.24.1_2.pkg
e2fsprogs-core-1.47.0.pkg			libgcrypt-1.10.2.pkg				p11-kit-0.24.1_2~d841595c85.pkg
e2fsprogs-core-1.47.0~584e640d80.pkg		libgcrypt-1.10.2~4ef2ab992e.pkg			pcre2-10.42.pkg
e2fsprogs-libblkid-1.46.2.txz			libgpg-error-1.47.pkg				pcre2-10.42~0252e988c3.pkg
e2fsprogs-libblkid-1.46.2~11342cdd9e.txz	libgpg-error-1.47~a11cc8f9f8.pkg		perl5-5.34.1_3.pkg
e2fsprogs-libblkid-1.46.2~d0aac929ad.txz	libhsts-0.1.0.pkg				perl5-5.34.1_3~be88f18f29.pkg

To remove packages from the cache directory, run the following command:

pkg clean 

Output:

The following package files will be deleted:
	/var/cache/pkg/e2fsprogs-1.46.2~b1851d3194.txz
	/var/cache/pkg/gettext-runtime-0.21.txz
	/var/cache/pkg/e2fsprogs-1.46.2.txz
	/var/cache/pkg/e2fsprogs-libuuid-1.46.2~b78457aa6b.txz
	/var/cache/pkg/gettext-runtime-0.21~7f53ebc469.txz
	/var/cache/pkg/e2fsprogs-libuuid-1.46.2.txz
	/var/cache/pkg/e2fsprogs-libblkid-1.46.2~11342cdd9e.txz
	/var/cache/pkg/e2fsprogs-libblkid-1.46.2.txz
	/var/cache/pkg/indexinfo-0.3.1~bd05368104.txz
	/var/cache/pkg/indexinfo-0.3.1.txz
	/var/cache/pkg/e2fsprogs-libss-1.46.2~d0e44d1a51.txz
	/var/cache/pkg/indexinfo-0.3.1~75a19b3acb.txz
	/var/cache/pkg/e2fsprogs-libss-1.46.2.txz
	/var/cache/pkg/vim-tiny-8.2.2725~640683e659.txz
	/var/cache/pkg/vim-tiny-8.2.2725.txz
	/var/cache/pkg/vim-tiny-8.2.2725~a1de39a0a9.txz
	/var/cache/pkg/gettext-runtime-0.21~4f88146680.txz
	/var/cache/pkg/e2fsprogs-libuuid-1.46.2~b01e611407.txz
	/var/cache/pkg/e2fsprogs-libss-1.46.2~b2cac5e088.txz
	/var/cache/pkg/e2fsprogs-libblkid-1.46.2~d0aac929ad.txz
	/var/cache/pkg/e2fsprogs-1.46.2~a2abff02bc.txz
The cleanup will free 5 MiB

Proceed with cleaning the cache? [y/N]: y

To remove all cached packages, run the following command:

pkg clean -a

Step 4 – View Information About Installed Package

The basic syntax to see the information about installed packages is shown below.

pkg info package-name

For example, to see the Nginx package information, run the following command:

pkg info nginx

You will see the following output:

nginx-1.24.0_12,3
Name           : nginx
Version        : 1.24.0_12,3
Installed on   : Sat Oct 14 08:53:08 2023 UTC
Origin         : www/nginx
Architecture   : FreeBSD:13:amd64
Prefix         : /usr/local
Categories     : www
Licenses       : BSD2CLAUSE
Maintainer     : [email protected]
WWW            : https://nginx.com/
Comment        : Robust and small WWW server

Step 5 – Remove Packages Using pkg Command

The basic syntax to remove a package is shown below.

pkg delete package-name

For example, to remove a package named nginx, run the following command:

pkg delete nginx

To remove additional dependencies that are installed with the package, run the following command:

pkg autoremove

Step 6 – Lock and Unlock Packages Using pkg Command

Sometimes, you don’t want to upgrade a package on your server. In this case, you can lock that package so that pkg never upgrades it.

To lock the Nginx package, run:

pkg lock nginx

To list all locked packages, run:

pkg lock -l 

Output:

Currently locked packages:
nginx-1.24.0_12,3

To unlock an Nginx package, run:

pkg unlock nginx

To lock all packages, run:

pkg lock -a

To unlock all packages, run:

pkg unlock -a 

Conclusion

In this post, we showed you how to manage packages with the pkg command in FreeBSD. You can now easily install, remove, update, upgrade, and manage packages via the command line interface. Try to manage packages using the pkg command on dedicated server hosting from Atlantic.Net!