How to use pkgsrc on Linux

NetBSD's package manager is generic, flexible, and easy. Here's how to use it.
130 readers like this.
Jump-start your career with open source skills

Opensource.com

NetBSD is famous for running on basically anything, but did you know its second claim to fame is the pkgsrc package manager? Like NetBSD, pkgsrc runs on basically anything, or at least anything Unix and Unix-like. You can install pkgsrc on BSD, Linux, Illumos, Solaris, and Mac. All told, over 20 operating systems are supported.

Why use pkgsrc?

With the exception of MacOS, all Unix operating systems ship with a package manager included. You don't necessarily need pkgsrc, but here are three great reasons you may want to try it:

  • Packaging. If you're curious about packaging but have yet to try creating a package yourself, pkgsrc is a relatively simple system to use, especially if you're already familiar with Makefiles and build systems like GNU Autotools.
  • Generic. If you use multiple operating systems or distributions, then you probably encounter a package manager for each system. You can use pkgsrc across disparate systems so that when you package an application for one, you've packaged it for all of them.
  • Flexible. In many packaging systems, it's not always obvious how to choose a binary package or a source package. With pkgsrc, the distinction is clear, both methods of installing are equally as easy, and both resolve dependencies for you.

How to install pkgsrc

Whether you're on BSD, Linux, Illumos, Solaris, or MacOS, the installation process is basically the same:

  1. Use CVS to check out the pkgsrc tree
  2. Bootstrap the pkgsrc system
  3. Install packages

Use CVS to check out the pkgsrc tree

Before Git, before Subversion, there was CVS. You don't have to know much about CVS to do a checkout of its code—if you're used to Git, then think of checkout as clone. When you perform a CVS checkout of pkgsrc, you're downloading "recipes" detailing how each package is to be built. It's a lot of files, but they're small because you're not actually pulling the source code for each package, just the build infrastructure and Makefiles required to build on it demand. Using CVS makes it easy for you to update your pkgsrc checkout when a new one is released.

The pkgsrc docs recommend keeping your tree in the /usr directory, so you must use sudo (or become root) to use this command:

$ cd /usr
$ sudo cvs -q -z2 \
  -d anoncvs@anoncvs.NetBSD.org:/cvsroot \
  checkout -r pkgsrc-2019Q3 -P pkgsrc

As I'm writing, the latest release is 2019Q3. Check the news section of pkgsrc.org's homepage or the NetBSD documentation to determine the latest release version.

Bootstrap pkgsrc

After the pkgsrc tree has copied to your computer, you have a /usr/pkgsrc directory filled with build scripts. Before you can use them, you must bootstrap pkgsrc so that you have easy access to the relevant commands you need to build and install the software.

The way you bootstrap pkgsrc depends on the OS you're on.

For NetBSD, you can just use the bundled bootstrapper:

# cd pkgsrc/bootstrap
# ./bootstrap

On other systems, there are better ways with some customized features included, provided by Joyent. To find out the exact command to run, visit pkgsrc.joyent.com. For example, on Linux (Fedora, Debian, Slackware, and so on):

$ curl -O \
  https://pkgsrc.joyent.com/packages/Linux/el7/bootstrap/bootstrap-trunk-x86_64-20170127.tar.gz
$ BOOTSTRAP_SHA="eb0d6911489579ca893f67f8a528ecd02137d43a"

Even though the path suggests that the included files are for RHEL 7, the binaries tend to be compatible with all but the most cutting-edge Linux distributions. And should you find a binary incompatible with the distribution you're on, you have the option to build from source.

Verify the SHA1 checksum:

$ echo "${BOOTSTRAP_SHA}" bootstrap-trunk*gz > check-shasum
sha1sum -c check-shasum

You can also verify the PGP signature:

$ curl -O \
https://pkgsrc.joyent.com/packages/Linux/el7/bootstrap/bootstrap-trunk-x86_64-20170127.tar.gz.asc
curl -sS https://pkgsrc.joyent.com/pgp/56AAACAF.asc | gpg --import
gpg --verify ${BOOTSTRAP_TAR}{.asc,}

Once you're confident that you have the right bootstrap kit, install it to /usr/pkg:

sudo tar -zxpf ${BOOTSTRAP_TAR} -C /

This provides you with the usual pkgsrc commands. Add these locations to your PATH:

$ echo "PATH=/usr/pkg/sbin:/usr/pkg/bin:$PATH" >> ~/.bashrc
$ echo "MANPATH=/usr/pkg/man:$MANPATH" >> ~/.bashrc

If you'd rather use pkgsrc without relying on Joyent's builds, you can just run the bootstrap script you got with the pkgsrc tree. Read the relevant README file in the bootstrap directory before running it for important system-specific notes.

Bootstrapping pkgsrc on NetBSD

How to install software with pkgsrc

Installing a precompiled binary (as you would with DNF or Apt) with pkgsrc is easy. The command for binary installs is pgkin, which has its own dedicated site at pkgin.net. The process ought to feel pretty familiar to anyone who's used Linux.

To search for the tmux package:

$ pkgin search tmux

To install the tmux package:

$ sudo pkgin install tmux

The pkgin command's aim is to mimic the behavior of typical Linux package managers, so there are options to list available packages, to query available packages to find what provides a specific executable, and so on.

How to build from source code with pkgsrc

The real power of pkgsrc, though, is the ease of building a package from source. You downloaded all 20,000+ build scripts in the first setup step, and you can access those by navigating into your pkgsrc tree directly.

For example, to build tcsh from source, first, locate the build script:

$ find /usr/pkgsrc -type d -name "tcsh"
/usr/pkgsrc/shells/tcsh

Next, change into the source directory:

$ cd /usr/pgksrc/shells/tcsh

The build script directory contains a number of files to help the application build on your system, but notably, it contains the DESCR file, which contains a description of the software, as well as the Makefile that triggers the build.

$ ls 
CVS    DESCR     Makefile
PLIST  distinfo  patches
$ cat DESCR
TCSH is an extended C-shell with many useful features like
filename completion, history editing, etc.
$

When you're ready, build, and install:

$ sudo bmake install

The pkgsrc system uses the bmake command (provided by the pkgsrc checkout in the first step), so be sure to use bmake (and not make out of habit).

If you're building for several systems, you can create a package instead of installing right away:

$ cd /usr/pgksrc/shells/tcsh
$ sudo bmake package
[...]
=> Creating binary package in /usr/pkgsrc/packages/All/tcsh-X.Y.Z.tgz

The packages that pkgsrc creates are standard tarballs, but they can be installed conveniently with pkg_add:

$ sudo pkg_add /usr/pkgsrc/packages/All/tcsh-X.Y.Z.tgz
tcsh-X.Y.Z: adding /usr/pkg/bin/tcsh to /etc/shells
$ tcsh
localhost% 

The pkgtools collection from pkgsrc provides the pkg_add, pkg_info, pkg_admin, pkg_create, and pkg_delete commands to help manage packages you build and maintain on your system.

Pkgsrc for easy management

The pkgsrc system offers a direct, hands-on approach to package management. If you're looking for a package manager that stays out of your way and invites customization, give pkgsrc a try on whatever Unix or Unix-like OS you're running.

What to read next
Tags
Seth Kenlon
Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time.

2 Comments

A very informative article on pkgsrc. Thanks!

Poor NetBSD. Too bad it wasn't able to climb the Distrowatch charts with some good solid corporate "community" backing a la Red Hat/CentOS/Fedora, Canonical/Ubuntu/Debian, Suse et al. What a travesty because I perceive the BSDs as more authentic to the Unix philosophy than its sometimes undisciplined cousin, Linux.

Look no further than that SystemD thingy which would never have been created in the BSD world, and how Linux appears to be diverging because of it.

It's a damn shame -- with NetBSD's vaunted portability, pkgsrc, & its free-spirited BSD license -- that more developers and users didn't gravitate towards this rock-solid OS. I've always visited http://netbsd.org/ over the years to keep an eye on the project's developments & to see what else "runs NetBSD". ;-)

Is it just me, or have others here on OpenSource.com ever fantasized -- just for a moment -- a world where NetBSD (or some other dominant BSD flavour with polish) runs flawlessly on low-end, mid-range & high-end workstations/laptops - using either LXQt, KDE, Xfce, Mate or Cinnamon desktop with pkgsrc behind the scenes running the distro's App Store?

I have! ;-)

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.