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.

Requirements:

  1. FreeBSD 11.x Installation

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/
List Port Repository Categories

List Port Repository Categories

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
List Database Category

List Database Category

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
List Available Packages in Category

List Available Packages in Category

As you can see in the above screenshot, FreeBSD database category holds more than 1000 database pre-complied packages.

4. In order to see if a specific application is available in a category, again, use the ls command and filter the result via grep utility in order to search for a custom application.

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
List MongoDB Packages

List MongoDB Packages

As you can see, multiple versions of an application can be available in FreeBSD Ports.

List Clam Antivirus Packages

List Clam Antivirus Packages

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
Find Software Category

Find Software Category

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.

# /usr/libexec/locate.updatedb
Update Locate Database

Update Locate Database

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
Search Software Package

Search Software Package

As you can see, there are two packages available for mailx utility, both located in /usr/ports/mail/ category.

8. Similar to finding a package with locate command, you can also use whereis command, to view the application category.

# whereis mailx
View Software Category

View Software Category

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
Search and Find Package

Search and Find Package

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
Search Category of Package

Search Category of Package

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
Install Package in FreeBSD

Install Package in FreeBSD

12. To query information about a specific installed package in the system, issue the below command.

# pkg info package_name
Query Installed Package Information

Query Installed Package Information

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
Find Package Info

Find Package Info

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
List All Installed Packages in FreeBSD

List All Installed Packages in FreeBSD

15. In order to remove a package from the system, issue the below commands.

# pkg remove package_name
or
# pkg delete package_name
Remove Package in FreeBSD

Remove Package in FreeBSD

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
Lock Package from Removing in FreeBSD

Lock Package from Removing in FreeBSD

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
Find Command or File Package Name

Find Command or File Package Name

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
Download Package Without Installing

Download Package Without Installing

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
Find Vulnerabilities of Software
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
Update Packages in FreeBSD

Update Packages in FreeBSD

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
List Installed Package Summary

List Installed Package Summary

22. To delete all dependencies left behind by installed packages in the system issue the below command.

# pkg autoremove
Delete Dependency Packages

Delete Dependency Packages

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
Delete PKG Local Cache

Delete PKG Local Cache

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.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Matei Cezar

I'am a computer addicted guy, a fan of open source and linux based system software, have about 4 years experience with Linux distributions desktop, servers and bash scripting.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

2 Responses

  1. freefall says:

    cd /usr/ports;
    echo */porttofind*

    is another way of finding ports with a quick response. Besides that, don’t name your article PKG commands something when talking about ports. Those two are different things

  2. Bernd says:

    To search a software package go to /usr/ports and do a ‘make search name=part_of_portname’ or ‘make search key=description’. You can combine it with grep, wc and so on and I think it’s much more efficient then “ls” or “locate” .

Leave a Reply to Bernd Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.