The FreeBSD operating systems offers Ports Collection as a simple way to install various applications. Each port contains any patches necessary to make the original application source code compile and run on FreeBSD based system. In this tutorial, I am going to give the commands for installing and managing ports collection on FreeBSD 10.x and 11.x.
How to get the ports collection
Before using ports, you must first obtain the Ports Collection /usr/ports directory. The portsnap command allows you to install ports collection over the Internet. To grab collection, enter:
# portsnap fetch
Sample outputs:
Fig.01: How To Obtain and Install and Ports on FreeBSD
# portsnap extract
Sample outputs:
/usr/ports/.arcconfig /usr/ports/.gitattributes /usr/ports/.gitignore /usr/ports/CHANGES /usr/ports/CONTRIBUTING.md /usr/ports/COPYRIGHT /usr/ports/GIDs /usr/ports/Keywords/desktop-file-utils.ucl /usr/ports/Keywords/fc.ucl /usr/ports/Keywords/fcfontsdir.ucl /usr/ports/Keywords/fmtutil.ucl /usr/ports/Keywords/fontsdir.ucl /usr/ports/Keywords/glib-schemas.ucl ..... .. ... /usr/ports/x11/xvkbd/ /usr/ports/x11/xvmcinfo/ /usr/ports/x11/xvt/ /usr/ports/x11/xwatchwin/ /usr/ports/x11/xwd/ /usr/ports/x11/xwininfo/ /usr/ports/x11/xwinwrap/ /usr/ports/x11/xwit/ /usr/ports/x11/xwud/ /usr/ports/x11/xxkb/ /usr/ports/x11/xzoom/ /usr/ports/x11/yad/ /usr/ports/x11/yakuake-kde4/ /usr/ports/x11/yalias/ /usr/ports/x11/yeahconsole/ /usr/ports/x11/yelp/ /usr/ports/x11/zenity/ Building new INDEX files... done.
Type the above command for the first time only. Another option is to type the following first time only:
# portsnap fetch extract update
If you already have a populated /usr/ports and you are just updating, run the following command instead:
# portsnap fetch update
How do I search for apps or ports on FreeBSD?
Before you can install any ports you need to know what you want, and what the application is called. If you know the exact name of the port, you can use the whereis command as follows:
# whereis apache22
# whereis php5
# whereis php7
# whereis lsof
Sample outputs:
apache22: /usr/ports/www/apache22
You can also use the Ports Collection’s built-in search mechanism. To use the search feature, you will need to be in the /usr/ports directory. Once in that directory, run make search name=program-name where program-name is the name of the program you want to find. For example, if you were looking for apache, enter:
# cd /usr/ports
# make search name=apache
# make search name=apache | more
# make search name=php7 | grep mysql
Sample outputs:
Port: php70-mysqli-7.0.15 Path: /usr/ports/databases/php70-mysqli Info: The mysqli shared extension for php Port: php70-pdo_mysql-7.0.15 Path: /usr/ports/databases/php70-pdo_mysql Info: The pdo_mysql shared extension for php Port: php71-mysqli-7.1.1 Path: /usr/ports/databases/php71-mysqli Info: The mysqli shared extension for php Port: php71-pdo_mysql-7.1.1 Path: /usr/ports/databases/php71-pdo_mysql Info: The pdo_mysql shared extension for php
The following will perform searches for port names, comments, descriptions and dependencies and can be used to find ports which relate to a particular subject if you do not know the name of the program you are looking for.
# cd /usr/ports
# make search key=string
# make search key=php5
Another trick is as follows to find out zsh port:
$ echo /usr/ports/*/*zsh*
Sample outputs:
/usr/ports/shells/zsh /usr/ports/shells/zsh-navigation-tools /usr/ports/textproc/zsh-syntax-highlighting
Say hello to psearch
The psearch command searches the FreeBSD ports for given string or pattern. You can install it as follows:
# pkg install psearch
OR
# cd /usr/ports/ports-mgmt/psearch && make install clean
Once installed search ports as follows:
$ psearch {name-here}
$ psearch lsof
Sample outputs:
sysutils/lsof Lists information about open files (similar to fstat(1)) sysutils/p5-Unix-Lsof Unix::Lsof -- a wrapper to the Unix lsof utility
To show long description for any match pass the -l option:
$ psearch -l apache24
www/apache24 Version 2.4.x of Apache web server The Apache HTTP Server Project is an effort to develop and maintain an open-source HTTP server for various modern desktop and server operating systems, such as UNIX and Windows NT. The goal of this project is to provide a secure, efficient and extensible server which provides HTTP services in sync with the current HTTP standards. The 2.x branch of Apache Web Server includes several improvements like threading, use of APR, native IPv6 and SSL support, and many more. WWW: http://httpd.apache.org/
To find out version pass the -n option:
$ psearch -n apache24
apache24-2.4.25_1 Version 2.4.x of Apache web server
How do I install the port called apache22?
To install the Apache22 port, enter:
# cd /usr/ports/www/apache22/
# make install clean
How do I remove the port called apache22?
To delete and remove the port named apache22, enter:
# cd /usr/ports/www/apache22/
# make deinstall
To remove configuration files as well:
# make rmconfig
How do I update installed ports?
Keeping your applications up-to-date is very important. You need to use the portmaster. First install it:
# cd /usr/ports/ports-mgmt/portmaster
# make install clean
OR
# pkg install portmaster
Okay, update the ports tree:
# portsnap fetch update
Read the /usr/ports/UPDATING
Before you do anything read the /usr/ports/UPDATING file. It has last minute information and workaround that might affect your system:
# vi /usr/ports/UPDATING
Next, you should list all installed ports by category, and search for updates:
# portmaster -L
Sample outputs:
===>>> Root ports (No dependencies, not depended on) ===>>> dialog4ports-0.1.6 ===>>> firstboot-freebsd-update-1.2 ===>>> firstboot-growfs-1.0 ===>>> firstboot-pkgs-1.4 ===>>> pkg-1.9.4_1 ===>>> portmaster-3.17.9_4 ===>>> psearch-2.0.2 ===>>> 7 root ports ===>>> Trunk ports (No dependencies, are depended on) ===>>> ca_root_nss-3.26 ===>>> New version available: ca_root_nss-3.28.1 ===>>> flock-2.19 ===>>> New version available: flock-2.29_1 .... .. ... ===>>> php70-zip-7.0.15 ===>>> 11 leaf ports ===>>> 59 total installed ports ===>>> 10 have new versions available
A better option is to run the following command to see list of installed ports which are out of date and need to be upgraded ASAP:
# pkg version -l "<"
OR
# pkg version -vIL=
Sample outputs:
ca_root_nss-3.26 < needs updating (index has 3.28.1) curl-7.50.3 < needs updating (index has 7.52.1_1) flock-2.19 < needs updating (index has 2.29_1) gettext-runtime-0.19.8.1 < needs updating (index has 0.19.8.1_1) indexinfo-0.2.4 < needs updating (index has 0.2.6) mariadb101-client-10.1.20_1 < needs updating (index has 10.1.21) mariadb101-server-10.1.20_1 < needs updating (index has 10.1.21) python-2.7_2,2 < needs updating (index has 2.7_3,2) python27-2.7.12 < needs updating (index has 2.7.13_1) sudo-1.8.17p1 < needs updating (index has 1.8.19p2)
Finally you can upgrade all ports using portmaster as follows:
# portmaster -a
If you want to clean distfile, enter:
# portmaster -ad
You can upgrade only sudo port:
# portmaster sudo
References
- man pages – portmaster, portsnap, pkg
- Using the Ports Collection
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
The output of the make search key=whatever can be quite overwhelming.
To crop it a bit use quicksearch instead of search.
Getting the ports collection for the first time, the 3 steps (fetch, extract & update) can be combine into “portsnap fetch extract update”.
To search ports you can use “psearch”, which will give a short summary next to the loation.
You’ll have to install first from ports:
Great tutorial for ex Linux/systemd user ;) thank you.
Very nice documentation. Thanks!
To read a file you’re not going to change, use less/more
less /usr/ports/UPDATING