FreeBSD is a variant of the Unix operating system, extensively used in servers, desktops, and embedded platforms. Major corporations such as Netflix, Yahoo, WhatsApp, BBC, and Sony utilize FreeBSD. It is inherently an operating system crafted for server use. This article explains how to employ the pkg package manager in FreeBSD, necessitating a cloud server running FreeBSD 12.
In FreeBSD, there are two methods for software installation: Ports and Packages. Ports utilize source code provided by software vendors, enabling or disabling certain features as per our requirements, and install software in a build-from-source manner. Packages, on the other hand, are tools for installing pre-compiled software, defaulting to options most people find acceptable, thereby streamlining the installation process. Packages simplify the processes of installing, uninstalling, and upgrading software. The FreeBSD Packages system, also known as pkg, stores its software packages in an SQLite database, facilitating easy retrieval and querying.
It's important to note that the software installation path in FreeBSD’s pkg package manager differs from Linux. The installation path is /usr/local, while the software configuration path is /usr/local/etc. This is a complete divergence from Linux.
FreeBSD's pkg package manager offers numerous functionalities that make software installation quick and simple. pkg is a standalone application with various subcommands, each responsible for tasks like installation, uninstallation, and querying. All pkg operations must be executed as root or via sudo. For instance, the following command is used to install the curl package on FreeBSD. Adding -y at the end of the command skips the confirmation prompt: Proceed with this action? [y/N]
pkg install curl
Uninstalling packages is also straightforward and intuitive:
pkg delete curl
Use the pkg help command to quickly view reference documentation, or specify a subcommand to see its manual.
pkg help
pkg help install
pkg help delete
The default version of FreeBSD does not come with the pkg package manager pre-installed. When we attempt to install software for the first time, the system will prompt whether to install the pkg package manager. For instance, if we want to install wget, the command line will prompt as follows:
pkg install wget
The package management tool is not yet installed on your system.
Do you want to fetch and install it now? [y/N]: y
After entering y and pressing enter, pkg will commence installation, and the desired software will be installed following pkg’s installation. It's also possible to install pkg alone without installing other software simultaneously. Use the following command to install only pkg.
pkg bootstrap
Note: FreeBSD cloud servers from Chaomo Data come with the pkg package manager pre-installed, eliminating the need for reinstallation.
The pkg package manager is highly flexible, with each subcommand having numerous configuration options. Global configuration settings can be customized in the /usr/local/etc/pkg.conf file. This file is written in Unified Configuration Language (UCL) and contains many commented-out options, i.e., default configurations. The values for configuration items can be integers, strings, or boolean values. For example:
#PKG_DBDIR = "/var/db/pkg";
#PKG_CACHEDIR = "/var/cache/pkg";
#PORTSDIR = "/usr/ports";
#INDEXDIR = "";
#INDEXFILE = "INDEX-10"; # Autogenerated
#HANDLE_RC_SCRIPTS = false;
#DEFAULT_ALWAYS_YES = false;
#ASSUME_ALWAYS_YES = false;
In the pkg.conf configuration file, you can also define aliases for pkg subcommands. At the end of the file, there's a section marked “ALIAS,” where aliases can be added. Aliases are commonly used to simplify complex or lengthy commands. More information about the pkg.conf configuration file can be obtained through the following command.
man pkg.conf
Having installed and configured the pkg package manager, we are now ready to install software. Those with server operation experience know that even the same software may have different package names across different operating systems and versions. For instance, Apache's name varies across different Linux distributions. Therefore, before installing software, it's necessary to ascertain the exact name of the package.
FreeBSD's public software repository is updated every few days, containing over 25,000 packages. For example, to find the specific name of Apache, use the following command.
pkg search apache
# apache24-2.4.38 Version 2.4.x of Apache web server
This command returns all software with 'apache' in their names, possibly yielding a long list. We can adjust the search results using the pkg search subcommand. Instructions for using this command can be obtained via pkg help search.
If you're uncertain whether a package meets your needs, add the -R option to view more information about it.
pkg search -R apache24
# name: "apache24"
# origin: "www/apache24"
# version: "2.4.38"
# comment: "Version 2.4.x of Apache web server"
# maintainer: "apache@FreeBSD.org"
# www: "https://httpd.apache.org/"
# abi: "FreeBSD:12:amd64"
# arch: "freebsd:12:x86:64"
# prefix: "/usr/local"
# . . .
Taking Apache as an example, use the following command for installation.
pkg install apache24
The installation process goes like this: when the pkg install command is executed, pkg first checks the local software cache, then downloads from the public software repository (pkg.FreeBSD.org). Upon successful installation, pkg registers the software in the local SQLite database, located at /var/db/pkg/local.sqlite. If this database file is deleted, the system will not be able to track which software has been installed. Dependencies are also automatically installed during this process.
pkg package manager can also download software without installing it, allowing installation at a more suitable time. Use the pkg fetch command to download software; the -d option indicates downloading dependent software as well. The download path is the /var/cache/pkg directory.
pkg fetch nginx -d
Later, when ready to install the software, execute the pkg install command. At this point, pkg uses the local software cache instead of re-downloading from the public repository.
Over time, the local software cache directory may become quite large. The pkg clean subcommand is used to clear the cache, with the -a option indicating to clear all cache.
pkg clean -a
If you wish to automatically clean up after each software installation or upgrade, set the AUTOCLEAN configuration item in the pkg.conf file to true.
To understand what software is installed on the server, use the pkg info subcommand to get a list of all 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 the Mozilla Project
# . . .
To get detailed information about a specific software, use pkg info followed by the software name. The system will return a lot of useful information, such as software version, installation time, and software license.
pkg info nginx
# nginx-1.14.2_3,2
# Name : nginx
# Version : 1.14.2_3,2
# . . .
Uninstalling software is convenient, using the pkg delete or pkg remove subcommands, and the system will list the software being uninstalled. When uninstalling a particular software, other software dependent on it will also be uninstalled.
pkg delete nginx
There might be a need to keep certain software on the cloud server from updating, which can be achieved using the lock feature. When a package is locked, the pkg package manager will not upgrade, downgrade, uninstall, or reinstall it. The same approach is applied to dependent packages. Use the pkg lock subcommand to lock packages.
pkg lock openssl
Unlocking packages:
pkg uklock openssl
Viewing the list of locked packages:
pkg lock -l
Use the -a option to lock or unlock all packages:
Locking all packages:
pkg lock -a
pkg unlock -a
The public software repository is an integral part of the pkg package manager. Multiple software repositories can exist on the operating system, which can be added, removed, enabled, or disabled. Each repository can be configured using UCL. The official FreeBSD public repository is located in the /etc/pkg directory, named “FreeBSD,” enabled by default, with its configuration file located at /etc/pkg/FreeBSD.conf, as follows.
FreeBSD: {
url: "pkg+http://pkg.FreeBSD.org/${ABI}/quarterly",
mirror_type: "srv",
signature_type: "fingerprints",
fingerprints: "/usr/share/keys/pkg",
enabled: yes
}
We can add or remove third-party software repositories. Since the /etc/pkg directory is dedicated to the official repository, third-party repositories are typically located in the /usr/local/etc/pkg/repos directory. This directory does not exist by default and must be created manually using mkdir. The configuration files for third-party repositories are also located in this directory.
Here are other commonly used pkg subcommands, frequently employed by FreeBSD operating system administrators.
Installing software and skipping confirmation prompts:
pkg install -y package
Backing up the local pkg database:
pkg backup
Listing all installed software:
pkg info
Displaying detailed information about a software:
pkg info package
Searching for software packages:
pkg search -i package
Displaying known security vulnerabilities in a software package:
pkg audit -F
Deleting unused software packages:
# Removes unused packages
pkg autoremove
Uninstalling software packages:
pkg delete package
Cleaning the local software cache:
pkg clean -ay
Updating installed software:
pkg update
Upgrading installed software:
pkg upgrade
Checking the integrity of software packages:
pkg check -saq
Displaying filenames related to a software package
pkg info -l nginx
In summary, FreeBSD offers two techniques for software package installation: Ports, which involve compiling from the source, and Packages, which involve installing from pre-compiled software. As the FreeBSD operating system is clearly transitioning towards a unified package management approach, it's advisable to use the pkg package manager for third-party software management, avoiding complex configurations required by compilation methods.
23-02-2024 02:02:07
22-02-2024 03:19:32
22-02-2024 03:16:03
22-02-2024 03:14:03
22-02-2024 03:11:58