How to Manage Packages with pkg Binary Package Manager on FreeBSD 12

How to Manage Packages with pkg Binary Package Manager on FreeBSD 12


Linux Articles / All 25 Views comments

FreeBSD is an open-source Unix-like working system used to energy trendy servers, desktops, and embedded platforms. A big group has regularly developed it for greater than thirty years. Its superior networking, safety, and storage options have made FreeBSD the platform of selection for most of the busiest web pages and most pervasive embedded networking and storage units. Giants like Netflix, Yahoo!, WhatsApp, BBC, and Sony use FreeBSD in some type. The FreeBSD system shouldn't be as extensively often known as Linux primarily as a result of Linux has targeted for a few years on the Desktop and the FreeBSD challenge has tended to be extra server-based.

On this tutorial, you'll study binary package deal administration by way of pkg on FreeBSD 12 working system which is the newest model on the time this text is being written. A lot of the stuff additionally applies to the sooner FreeBSD 11 model.


  • FreeBSD 12 working system
  • Entry to root or sudo account

Ports and Packages

On the FreeBSD system, we now have two totally different strategies to put in add-on software program: by way of ports and by way of pre-configured packages that the majority customers will select to put in and handle software program.

Ports is a system for constructing further software program on FreeBSD. With Ports you begin with the uncooked supply code offered by the software program vendor and construct the software program in precisely the best way you want, enabling and disabling options as you need.

Packages are pre-compiled software program, and they're the results of constructing ports, utilizing the choices the port maintainer believes can be most helpful to the widest number of individuals, and bundling them up in a package deal to make them simply installable. Packages allow you to shortly set up, uninstall, and improve add-on software program. They're what most customers will use. FreeBSD packaging system is known as package deal, or simply pkg. Package deal info will get saved in an SQLite database which you could question about package deal knowledge.

One other vital details about FreeBSD package deal administration is the situation of package deal binaries. Packages set up binaries beneath /usr/native, and most configuration information find yourself in /usr/native/and so on fairly than /and so forth. When you come from Linux world chances are you'll discover this very uncommon.

The FreeBSD package deal supervisor - pkg

pkg is the next-generation alternative for the normal FreeBSD package deal administration instruments, providing many options that make coping with binary packages faster and simpler. pkg is the simplest approach to set up software program that isn’t already included within the base system of FreeBSD. pkg is a single program with plenty of subcommands. You'll use pkg for almost each operation on packages, like putting in, eradicating and investigating packages. All package deal operations and modifications have to be run as root or by way of sudo.

Right here is how you'd set up package deal on FreeBSD:

pkg set up

You'll be able to feed the command with -y to keep away from Proceed with this motion? [y/N]: query when putting in software program or you'll be able to configure pkg to all the time assume -y in a configuration file.

To take away the package deal, you'd use:

pkg delete

Use pkg assist for a fast reference on the out there subcommands, or pkg assist command to show the guide web page for a specific subcommand.

pkg assist pkg 
assist set up
pkg assist delete

Putting in pkg

The inventory model of FreeBSD does not ship with the pkg package deal supervisor put in. You'll want to set up it. The primary time you attempt to set up some package deal pkg prompts you to put in the package deal administration software. For instance, as an instance the primary package deal you need to set up in your recent FreeBSD is wget, and you will notice the next immediate in your terminal:

The package deal administration device just isn't but put in in your system.
Do you need to fetch and set up it now? [y/N]: y

You'll press Y and ENTER in your keyboard and package deal administration device set up will begin. After packaging system is in place, the preliminary software program package deal that you simply needed to put in (wget) will probably be put in.

You can even set up the packaging system by itself, with out including different packages, by operating pkg bootstrap, however that is not often used, perhaps solely in system setup scripts. pkg is aware of how one can set up and replace itself and different packages.

Easy methods to seek for packages

Now that you've a package deal supervisor put in, you'll be able to set up packages. In case you are a system administrator, you're conversant in the truth that totally different working techniques assign totally different names to packaged variations of the identical software program. A package deal for Apache webserver on FreeBSD, for instance, may have a totally totally different identify than the packaged Apache on totally different Linux distributions. So, earlier than you'll be able to set up something, you’ll want to determine what is the identify of the package deal you need to set up.

FreeBSD's listing of obtainable purposes is rising on a regular basis. The FreeBSD Challenge presents a number of units of packages in a public repository, and they're up to date each few days. There are presently over 25,000 packages.

For instance, let's attempt to seek for Apache webserver.

pkg search apache 
# apache24-2.four.38 Model 2.four.x of Apache net server

It'll discover all packages with apache of their names. This can return an extended listing, however what you're on the lookout for is apache24 package deal. There's a brief description of each package deal. This could enable you to when deciding what package deal to put in. Nevertheless it's not all the time easy.

Some searches can generate a whole lot of outcomes. You will want to make the most of totally different command-line choices to trim or regulate the search outcomes. Seek the advice of pkg-search man web page or assist web page pkg assist search to study extra about widespread search choices.

Should you’re unsure whether or not a package deal is what you need you need to use the next command to lookup particulars of the package deal:

pkg search -R apache24

# identify: "apache24"
# origin: "www/apache24"
# model: "2.four.38"
# remark: "Model 2.four.x of Apache net server"
# maintainer: "[email protected]"
# www: ""
# abi: "FreeBSD:12:amd64"
# arch: "freebsd:12:x86:64"
# prefix: "/usr/native"
# . . .
# . . .

This command provides you with a variety of helpful details about the package deal.

Methods to set up new packages with pkg

To put in software program use pkg's set up subcommand and the identify of a package deal to put in.

pkg set up apache24

If you set up packages with pkg set up, pkg consults the native package deal catalog, then downloads the requested package deal from the repository at As soon as the package deal is put in, it’s registered in an SQLite database stored in /var/db/pkg/native.sqlite. Take care to not delete this file, in any other case, your system will lose monitor of which packages have been put in. If the software program has dependencies, pkg will determine them out, and set up them together with the bottom package deal. Packages put in as dependencies are referred to as automated packages.

pkg has the power to only obtain packages over the web, save them in a single location on the disk, and you may set up them at one other time. You need to use the pkg fetch command to obtain the package deal with out putting in it.

pkg fetch nginx 

This command will fetch simply the Nginx package deal with out its dependencies. You need to use the -d flag to seize all of the dependencies in addition to the named package deal.

pkg fetch -d nginx 

The packages are downloaded to the package deal cache listing /var/cache/pkg. After you've fetched packages, pkg will stick them to this listing. You possibly can listing information to see what it incorporates.

ls /var/cache/pkg 

Now, to put in a downloaded package deal after a fetch, run pkg set up usually. The set up course of makes use of the cached information relatively than the downloaded ones.

Over time, the package deal cache listing can develop huge. The pkg clear command removes any cached packages which were changed by newer variations, in addition to any package deal information which are not within the repository.

pkg clear 

If you wish to take away all cached packages, use the -a flag.

pkg clear -a 

If you wish to clear the package deal cache routinely after every package deal set up or improve, set the pkg.conf choice AUTOCLEAN to true.

Methods to configure pkg

The pkg program is designed to be extremely versatile. Every subcommand has an entire bunch of choices. You possibly can set up custom-made however constant conduct for many packages with the system-wide configuration file for pkg, situated in /usr/native/and so forth/pkg.conf.

pkg.conf file incorporates commented-out defaults for pkg. Simply by studying that file, you'll be able to study lots about how pkg behaves. The configuration is written in common configuration language (UCL) and there are many commented-out configuration choices and fairly a couple of aliases. Variables could be set to an integer, a string, or a Boolean worth

#PKG_DBDIR = "/var/db/pkg";
#PKG_CACHEDIR = "/var/cache/pkg";
#PORTSDIR = "/usr/ports";
#INDEXFILE = "INDEX-10";        # Autogenerated
. . .

You'll be able to outline aliases for pkg subcommands in pkg.conf. On the backside of pkg.conf, you’ll discover a part labeled ALIAS. When you end up repeatedly operating complicated instructions, you need to add aliases.

For extra info on the file format and choices, you'll be able to seek advice from the pkg.conf(5) man web page.

man pkg.conf

The right way to view details about put in packages

For those who overlook which packages you’ve put in on a system you should use pkg information command to get an entire record of put in software program.

pkg information
# atk-2.28.1 GNOME accessibility toolkit (ATK)
# avahi-app-Zero.7_2 Service discovery on an area community
# ca_root_nss-Three.42.1 Root certificates bundle from the Mozilla Challenge
# . . .
# . . .

If you would like extra details about an put in package deal, use pkg information and the package deal identify. This exhibits the package deal set up particulars in a human-friendly report.

pkg information nginx
# nginx-1.14.2_3,2
# Identify : nginx
# Model : 1.14.2_3,2
# . . .
# . . .

You possibly can see lots of helpful info like a model of the software program, the time of software program set up, software program license, compile-time flags, and so forth. See the pkg-info man web page for the entire particulars.

Easy methods to take away packages

To take away or uninstall binary packages use the pkg delete subcommand. It’s additionally obtainable as pkg take away.

pkg delete nginx
# or
pkg take away nginx

You’ll get an inventory of packages to be eliminated and the way a lot area they’ll unencumber.

Should you take away a package deal that different packages depend upon, pkg removes the relying packages as nicely.

Methods to lock packages

There could also be a time once you need a package deal in your server to by no means improve. Once you lock a package deal, pkg gained’t improve, downgrade, uninstall or reinstall it. It applies the identical guidelines to the package deal’s dependencies and the packages it is determined by.

Use pkg lock to lock a package deal.

pkg lock openssl 

This openssl package deal is now locked.

To record all at present locked packages on the system, use the -l flag.

pkg lock -l 

To take away the lock use the pkg unlock command.

pkg unlock openssl 

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

pkg lock -a
pkg unlock -a

Package deal repositories

pkg helps package deal repositories, that are named collections of packages. You possibly can add, take away, allow, and disable repositories. You need to configure every repository in its personal file utilizing UCL format. Official FreeBSD repositories belong in /and so forth/pkg listing. FreeBSD ships with the repo "FreeBSD" enabled. You’ll discover its configuration file in /and so on/pkg/FreeBSD.conf.

  url: "pkg+$ABI/quarterly",
  mirror_type: "srv",
  signature_type: "fingerprints",
  fingerprints: "/usr/share/keys/pkg",
  enabled: sure

You'll be able to add and take away repositories as wanted. As /and so on/pkg is reserved for official FreeBSD repositories, you’ll want one other listing. The normal location is /usr/native/and so forth/pkg/repos. If you wish to use a special listing, you’ll have to set a location in pkg.conf with the REPO_DIRS choice. The native repository listing doesn’t exist by default, so that you’ll have to create it with mkdir -p /usr/native/and so on/pkg/repos. Put your personal repository configurations in that listing

Instance pkg instructions

On this part, I'll record a few of the mostly used subcommands that you'll more than likely use when administering the FreeBSD server.

# Installs a package deal with out asking any questions
pkg set up -y package deal

# Makes a backup of the native package deal database
pkg backup

# Lists all put in packages
pkg information

# Exhibits prolonged info for a package deal
pkg information package deal

# Searches package deal repository
pkg search -i package deal

# Exhibits packages with recognized safety vulnerabilities
pkg audit -F

# Exhibits which package deal owns the named file
pkg which file

# Removes unused packages
pkg autoremove

# Uninstalls a package deal
pkg delete package deal

# Removes cached packages from /var/cache/pkg
pkg clear -ay

# Updates native copy of the package deal catalog
pkg replace

# Upgrades put in packages to their newest model
pkg improve

# Checks the integrity of all of your packages
pkg examine -saq

# Verifies that a package deal's information are unaltered
pkg examine -s nginx

# Exhibits what information got here with the package deal
pkg information -l nginx

# Lists non-automatic packages
pkg prime-list


FreeBSD implements two companion applied sciences for putting in third-party software program: the FreeBSD Ports Assortment, for putting in from supply, and packages, for putting in from pre-built binaries. However as FreeBSD is shifting the system extra decisively towards common package deal administration, it is best to attempt to handle third-party software program with pkg as a lot as potential. Keep away from utilizing ports until the software program you need has no packagized model or you could customise compile-time choices.