Home / How To / How to manage packages with pkg Binary Package Manager on FreeBSD 12

How to manage packages with pkg Binary Package Manager on FreeBSD 12

FreeBSD is a open source Unix-like operating system used to power modern servers, desktops, and embedded platforms. A large society has continuously developed it for more than thirty years. Its advanced networking, security and storage features have made FreeBSD the chosen platform for many of busiest websites and most pervasive embedded network and storage devices. Giants like Netflix, Yahoo !, WhatsApp, BBC and Sony use FreeBSD in some form. The FreeBSD system is not as widely known as Linux, mainly because Linux has focused on Desktop for many years and the FreeBSD project has tended to be more server-based.

In this tutorial you will learn about binary package management via pkg on FreeBSD 1

2 operating system which is the latest version at the time of writing this article. Most things also apply to the previous version of FreeBSD 11.


  • FreeBSD 12 operating system
  • Access to root or sudo account

Ports and packages

On the FreeBSD system we have two different methods of installing additional software: via p location and via preconfigured packages that most users will choose to install and manage software.

Ports is a system for building additional software on FreeBSD. With ports, you start with the raw source code provided by the software vendor and build the software on exactly what you need, which enables and disables features that you want.

Packages are pre-compiled software and they are the result of building ports, using the options that port managers think will be most useful to most people, and gathering them into a package to make them easy to install. With packages, you can quickly install, uninstall and upgrade additional software. That's what most users will use. FreeBSD packaging system is called package or just pkg . Packet information is stored in a SQLite database that you can query for packet data.

Another important information about FreeBSD package management is the location of package binaries. Packages install binaries under / usr / local and most configuration files end up in / usr / local / etc rather than / etc . If you come from the Linux world, you might find this very unusual.

FreeBSD Package Manager – pkg

pkg is the next-generation replacement for the traditional FreeBSD package management tools that offer many features that make binary package management faster and easier. pkg is the easiest way to install software that is not already included in the FreeBSD base system. pkg is a single program with lots of subcommands. You will use pkg for almost every package operation, such as installing, removing, and examining packages. All package operations and changes must be run as root or via sudo .

How to install package acme.sh on FreeBSD: [19659019] pkg install acme.sh

You can enter the command with -y to avoid Continue with this action? [y/N]: ask when installing software or you can configure pkg to always adopt -y in a configuration file.

To remove the package you would use:

  pkg delete acme.sh 

Use pkg help for a quick reference to available subcommands, or pkg help for to display the manual page for a particular subcommand. [19659019] pkg help pkg
help install
pkg help delete

Installing pkg

The stock version of FreeBSD does not come with pkg package manager installed. You must install it. The first time you try to install a package pkg ask you to install the package management tool. For example, let's say that the first package you want to install on your new FreeBSD is wget and you will see the following prompt in your terminal:

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

You press Y and ENTER on the keyboard and the package management tool installation will start. After the packaging system is in place, the first software package that you wanted to install ( wget ) will be installed.

You can also install the packaging system on your own, without adding other packages, by running pkg bootstrap but it is rarely used, perhaps only in system setup scripts. pkg knows how to install and update himself and other packages.

How to search for packages

Now that you have installed a package manager, you can install packages. If you are a system administrator, you know the fact that different operating systems assign different names to packaged versions of the same software. A package for Apache web server on FreeBSD, for example, will have a completely different name than the packaged Apache on various Linux distributions. So before you can install anything, you need to find out what is the name of the package you want to install.

FreeBSD's list of available applications is constantly growing. The FreeBSD project offers several package sets in a public archive, and they are updated in a few days. There are currently over 25,000 packages.

For example, let's try searching for Apache web server.

  pkg search apache 
# apache24-2.4.38 Version 2.4.x of Apache web server

It will find all packages with apache in their name. This gives a long list, but what you are looking for is the package apache24 . There is a brief description of each package. This should help you in deciding which package to install. But it's not always easy.

Some searches can generate hundreds of results. You must use different command line options to trim or adjust the search results. Consult pkg-search man page or help page pkg help search to learn more about common search options.

If you are not sure if a package is what you want, you can use the following command to find the package details:

  pkg search -R apache24 

# name: "apache24"
# origin : "www / apache24"
# version: "2.4.38"
# comment: "Version 2.4.x of Apache web server"
# entertainer: "[email protected]"
# www: " https://httpd.apache.org/"ebrit19459023vard# abi: "FreeBSD: 12: amd64"
# arch: "freebsd: 12: x86: 64"
# prefix: "/ usr / local"
#. .
#. . .

This command gives you very useful information about the package.

How to install new packages with pkg

To install software use pkg & # 39; s install subcommand and the name of a package to install.

  pkg install apache24 

When installing packages with pkg install pkg consult the local package directory and download the requested package from the repository at pkg.FreeBSD.org . Once the package is installed, it is registered in a SQLite database located in /var/db/pkg/local.sqlite . Be careful not to delete this file, otherwise your system will lose track of which packages have been installed. If the software has dependencies, pkg will calculate them and install them together with the base package. Packages installed as dependent are called automatic packages.

pkg has the option to just download packages over the internet, save them to a place on disk and you can install them at another time. You can use the command pkg fetch to download the package without installing it.

  pkg fetch nginx 

This command retrieves only the Nginx package without its dependencies. You can use the flag -d to address all dependencies as well as the mentioned package.

  pkg fetch -d nginx 

The packages are downloaded to the package's cache catalog / var / cache / pkg . Once you have downloaded packages, pkg will keep them in this directory. You can list files to see what it contains.

  ls / var / cache / pkg 

Now, to install a downloaded package after a download, pkg install runs normally. The installation process uses cached files rather than the downloaded ones.

Over time, the package's cache directory may become larger. The command pkg clean removes all cached packages that have been replaced by newer versions, as well as all package files that are no longer in the archive.

  pkg clean 

If you want to remove all cached packages, use the -a flag.

  pkg clean -a 

To clean the package cache automatically after each package installation or upgrade, set pkg.conf option AUTOCLEAN to true .

How to configure pkg

pkg program is designed to be very flexible. Each sub-command has a whole bunch of options. You can create custom but consistent behavior for most applications with the pkg system-wide configuration file located in /usr/local/etc/pkg.conf .

pkg.conf file contains annotated defaults for pkg . Only by reading that file can you learn much about how pkg behaves. The configuration is written in universal configuration language (UCL) and there are many commented configuration options and a lot of aliases. Variables can be set to an integer, a string, or a boolean value

  #PKG_DBDIR = "/ var / db / pkg";
#PKG_CACHEDIR = "/ var / cache / pkg";
#PORTSDIR = "/ usr / portar";
#INDEXFILE = "INDEX-10"; # Auto generated
. . .

You can define the alias for pkg subcommands in pkg.conf . At the bottom of pkg.conf you will find a section labeled ALIAS . When you repeatedly run complex commands yourself, you should add aliases.

For more information on file formats and options, please refer to pkg.conf (5) man page.

  man pkg.conf 

How to see information about installed packages

If you forget what packages you have installed on a system you can use the pkg info command to get a complete list of installed software.

  pkg info 
# atk-2.28.1 GNOME Accessibility Toolkit (ATK)
# avahi-app-0.7_2 Service Discovery on a Local Network
# ca_root_nss-3.42.1 Root Certificate Bundle from Mozilla Project
#. .
#. . .

For more information about an installed package, use pkg info and the package name. This shows the package installation information in a human friendly report.

  pkg info nginx 
# nginx-1.14.2_3,2
# Name: nginx
# Version: 1.14.2_3,2
#. .
#. . .

You can see very useful information like a version of the software, the time of software installation, software license, compilation time flags, etc. See pkg-info man page for complete details.

How to remove packages

To remove or uninstall binary packages, use the pkg delete subcommand. There are also like pkg remove .

  pkg remove nginx 
# or
pkg remove nginx

You will get a list of packages to be removed and how much space they "I will free.

If you remove a package that other packages depend on, pkg also removes the dependent packages.

How to lock packages

This can be a time when you want a package on your server to never upgrade When you lock a package, pkg will not upgrade, downgrade, uninstall, or reinstall it, it applies the same rules to the package dependencies and the programs it depends on.

Use pkg lock to lock a package.

  pkg lock openssl 

This openssl package is now locked.

To list all currently locked packages in the system, use the flag -l .

  pkg lock -l 

To remove the lock used [1 9459018] pkg unlock command.

  pkg unlock openssl 

To lock or unlock all packages on the system at once, use the -a flag.

  pkg lid -a 
pkg unlock -a

Package repositories

pkg supports package repositories, called package collections. You can add, remove, enable and disable repositories. You should configure each repository in its own UCL file format. Official FreeBSD archives belong to the / etc / pkg directory. FreeBSD is sent with repo "FreeBSD" enabled. You can find its configuration file in /etc/pkg/FreeBSD.conf .

  FreeBSD: {
url: "pkg + http: //pkg.FreeBSD.org/$ {ABI} / Quarterly",
mirror_type: "srv",
signature_type: "fingerprint",
fingerprint: "/ usr / share / keys / pkg",
enabled: yes

You can add and remove repositories if needed. Because / etc / pkg is reserved for official FreeBSD archives, you need another directory. The traditional location is / usr / local / etc / pkg / repos . To use another directory, you must specify a location in pkg.conf with the option REPO_DIRS . The local archive directory does not exist by default, so you must create it with mkdir -p / usr / local / etc / pkg / repos . Add your own archive configurations to that directory

Example pkg commands

In this section, I will list some of the most commonly used subcommands that you are likely to use when administering the FreeBSD server. [19659019] # Installs a package without asking any questions
pkg install-y package

# Make a backup of the local package database
pkg backup

# Lists all installed packages
pkg info [19659092] # Displays extended information for a package
pkg information packet

# Seeks package repository
pkg search -i package

# Displays packages with known security problems
pkg audit -F

# Displays which package owns the named file
pkg which file

# Removes unused packages
pkg autoremove

# Uninstalls a package
pkg delete package

# Removes cache packages from / var / cache / pkg
pkg clean -ay

# Up date local copy of package directory
pkg update

# Upgrades installed packages to their latest version
pkg upgrade

# Checks the privacy of everyone of your packages
pkg check -saq

# Verifies that a package's files are unchanged
pkg check-s nginx

# Shows which files came with the package
pkg info -l nginx [19659091] # Lists non-automatic packages
pkg prime-list


FreeBSD implements two additional techniques for installing third-party software: FreeBSD Ports Collection, for source and package installation, for pre-installed binaries . But when FreeBSD moves the system more resolutely toward universal package management, you should try to manage third-party software with pkg as much as . Avoid using ports unless the software you want has a packaged version or you need to customize compilation times.

Source link