23 Useful PKG Command Examples to Manage Packages in FreeBSD
In this tutorial we’ll explain how to manage pre-compiled binary package applications in FreeBSD with the help of the package management tool named PKG via Ports software collection repository.
Ports repository offers the necessary tools for compiling applications from source code, alongside with their dependencies, but also maintains a huge collection of pre-compiled packages, currently more than 24.000 packages, that can be installed on a FreeBSD system with pkg command.
Search and Find Applications in Ports Tree in FreeBSD
1. Ports repositories are divided in categories in FreeBSD, each category being represented by a directory in /usr/ports/ file system path.
A simple listing of the directory /usr/ports/ will display all available categories as shown in the below screenshot.
# ls /usr/ports/
2. To see all available applications belonging to a category, issue a ls command against category directory.
Suppose you want to display all available software packages that the database category has to offer, execute the below command in console. Pipe the result via less command to navigate more easily through output.
# ls /usr/ports/databases/ | less
3. In order to view how many packages are available in a category, list the category directory and pipe the result via wc command as shown in the below example.
# ls /usr/ports/databases/ | wc -l
As you can see in the above screenshot, FreeBSD database category holds more than 1000 database pre-complied packages.
In the below examples will search for mongodb database available packages and clam antivirus security packages.
# ls /usr/ports/databases/ | grep mongodb # ls /usr/ports/security/ | grep clam
As you can see, multiple versions of an application can be available in FreeBSD Ports.
5. In case you don’t know to which category a software belongs to, you can use another approach to find the software category. Use shell globbing wildcard
* character to search for a pattern through the entire Ports directories tree.
Assuming you want to see in what category you can find the software packages for mailx utility, you can run the following command.
# ls /usr/ports/*/*mailx
6. Another method for searching a software package and the category the package belongs to, is by using the locate command against a string pattern.
Before performing the search string, you should update the locate database with the following command.
7. After you’ve updated locate database, search for a specific software package by using a keyword pattern from the package’s name. For instance, if you want to search for the mailx utility, you can run the below command.
# locate mailx
As you can see, there are two packages available for mailx utility, both located in /usr/ports/mail/ category.
# whereis mailx
Search Software via PKG Command in FreeBSD
9. The easiest method to search and find an application in FreeBSD is via PKG package management command line. In order to search the binary packages for an application, for instance postfix software, issue the below command.
# pkg search package_name
10. In case you want to see to which category the package belongs to, run the same command as above with the
-o flag, as illustrated in the below examples.
# pkg search -o package_name
Manage Software in FreeBSD
11. In order to install a pre-compiled package from Ports repositories in FreeBSD, issue the pkg command as illustrated in the below example.
# pkg install package_name
12. To query information about a specific installed package in the system, issue the below command.
# pkg info package_name
13. The pkg info command switch will display the message “No packages(s) matching package_name” if the software package is not already installed in your system, as shown in the below screenshot.
# pkg info tcpdump
14. In order to list all installed software packages in FreeBSD, execute pkg info command without any option or switches.
The grep filter against pkg info command can show you if some specific packages or applications are already present in the system, as illustrated in the below example.
# pkg info | grep ftp
15. In order to remove a package from the system, issue the below commands.
# pkg remove package_name or # pkg delete package_name
16. In case you want to prevent the removal or modification of an installed package, you can use the lock switch for pkg command, as shown in the below image.
# pkg lock package_name
Unlock pkg command switch will allow you to remove the package restriction and modify or uninstall the package.
# pkg unlock package_name
17. In order to find out to which installed package a command or an executable file belongs to, issue the following command, as illustrated in the below screenshot examples.
# pkg which /path/to/executable
18. In order to locally download a package from Ports repository, without installing the package on the system, run pkg command with the fetch switch.
The downloaded package binary, which is a compressed .txz file, can be found in /var/cache/pkg/ system path.
# pkg fetch package_name # ls /var/cache/pkg/ | grep package_name
19. To check if the installed packages are exposed to common vulnerabilities or bugs issue the below command.
# pkg audit -F
To see a list of old vulnerabilities that where affecting a software package in earlier versions issue the below command.
# pkg audit package_name
Below is an excerpt of all known vulnerabilities that where found in Nginx web server compiled for FreeBSD.
# pkg audit nginx
nginx is vulnerable: Affected versions: <= 0.8.41 : > 1.4.4,1 nginx -- Request line parsing vulnerability CVE: CVE-2013-4547 WWW: https://vuxml.FreeBSD.org/freebsd/94b6264a-5140-11e3-8b22-f0def16c5c1b.html nginx is vulnerable: Affected versions: < 1.0.15 nginx -- Buffer overflow in the ngx_http_mp4_module CVE: CVE-2012-2089 WWW: https://vuxml.FreeBSD.org/freebsd/0c14dfa7-879e-11e1-a2a0-00500802d8f7.html nginx is vulnerable: Affected versions: < 1.4.7 nginx -- SPDY heap buffer overflow CVE: CVE-2014-0133 WWW: https://vuxml.FreeBSD.org/freebsd/fc28df92-b233-11e3-99ca-f0def16c5c1b.html ...
Maintain Package Management Utility in FreeBSD
20. To ensure that software repositories and all your installed packages and are up-to-date with the latest versions or security patches, issue the following commands.
# pkg update # pkg upgrade
21. To show remote repositories and local packages statistics, such as how many packages are installed in your system and how much disk space is filled by installed software, execute the following command.
# pkg stats
22. To delete all dependencies left behind by installed packages in the system issue the below command.
# pkg autoremove
23. In order to automatically erase package management local cache directory for remote downloaded packages, run the below command. You should first verify the list of locally downloaded binary packages.
# pkg clean -a -n # pkg clean -a -y
That’s all! As you can see, FreeBSD has an impressive package collection system, similar to package management tools used in Linux distributions such as Yum, RPM and APT with a huge number of pre-compiled software binaries and a simple and effective command line, pkg, which can be used to manage the software in a decent manner.