【FreeBSD】FreeBSD 包管理

Posted by 西维蜀黍 on 2021-03-04, Last Modified on 2021-09-21

Packages and Ports: Adding Software in FreeBSD

FreeBSD provides two methods for installing applications: binary packages and compiled ports. Each method has its own benefits:

Binary Packages

  • Faster installation as compared to compiling large applications.
  • Does not require an understanding of how to compile software.
  • No need to install a compiler.

Ports

  • Ability to customize installation options.
  • Custom patches can be applied.

If an application installation does not require any customization, installing the package is sufficient. Compile the port instead whenever an application requires customization of the default options. If needed, a custom package can be compiled from ports using make package.

Packages

Packages are pre-compiled applications, the FreeBSD equivalents of .deb files on Debian/Ubuntu based systems and .rpm files on Red Hat/Fedora based systems. Packages are installed using pkg. For example, the following command installs Apache 2.4:

$ pkg install apache24

Note that before this, you need to jail first

Here is how you would install the curl package on FreeBSD:

$ pkg install curl

For example, let’s try to search for Apache web server:

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

https://www.vultr.com/docs/how-to-use-the-pkg-package-manager-on-freebsd-12

Ports

The FreeBSD Ports Collection is a framework of Makefiles and patches specifically customized for installing applications from source on FreeBSD. When installing a port, the system will fetch the source code, apply any required patches, compile the code, and install the application and any required dependencies.

The Ports Collection, sometimes referred to as the ports tree, can be installed to /usr/ports using portsnap(8) (if running FreeBSD 11.4 or 12.1) or Subversion (if running FreeBSD-CURRENT). Detailed instructions for installing the Ports Collection can be found in section 5.5 of the FreeBSD Handbook.

Install Ports Collections

The recommended way of installing Ports Collection on FreeBSD is to use Portsnap. Portsnap is a fast and user-friendly tool for retrieving the Ports Collection. It connects to a FreeBSD site, verifies the secure key, and downloads a new copy of the Ports Collection.

To begin with, run system update and upgrade

$ pkg update
$ pkg upgrade -f

Then, download a compressed snapshot of the Ports Collection. The snapshot will be stored under /var/db/portsnap, by:

$ portsnap fetch

Once the fetching completes, extract Ports Collection snapshot to /usr/ports. This can only be done if the snapshot was being fetched for the first time.

$ portsnap extract

If the Ports Collection snapshot has been downloaded before, you can simply update it by running the commands below;

$ portsnap fetch
$ portsnap update

Before you can compile a software/application, here in called a port, ensure that you have GNU make (gmake)utility installed. To verify if gmake is installed;

# If installed, will print /usr/local/bin/gmake
$ which gmake

Installing GNU make utility

If gmake is not installed, see below on how to install.

$ pkg install gmake

Install Software Using Ports Collection

Next, navigate to the specific sub-directory of the software/port to be installed. For example, in this guide, we are going to demonstrate how to install neofetch, command-line tool for fetching system information.

$ cd /usr/ports/sysutils/neofetch

Next, compile the port or software as shown below;

$ make install

After the installation, run make clean to remove temporary files that were created during the installation to save space.

$ make clean

A benefit of using ports to install software is the ability to customize the installation options. This example specifies that the mod_ldap module should also be installed:

# cd /usr/ports/www/apache24
# make WITH_LDAP="YES" install clean

Uninstalling Software using Ports Collection

To uninstall any software or port installed via the Ports Collection on FreeBSD 12, simply navigate to the port subdirectory and run make deinstall. For example, to remove neofetch installed above;

$ make deinstall

Reference