How To Install Ports on FreeBSD 10 and 11

How do I install ports in FreeBSD operating systems using the command line options?

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

Fig.01: How To Obtain and Install and Ports on FreeBSD

To extract the snapshot into /usr/ports, enter:
# portsnap extract
Sample outputs:

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
# 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.

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

# 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 "<"
# 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-           <   needs updating (index has
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


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 6 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • me Apr 6, 2011 @ 5:20

    The output of the make search key=whatever can be quite overwhelming.
    To crop it a bit use quicksearch instead of search.

  • PsyberMonkey Apr 30, 2011 @ 11:25

    Getting the ports collection for the first time, the 3 steps (fetch, extract & update) can be combine into “portsnap fetch extract update”.

  • Makimoto Jul 29, 2011 @ 12:01

    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:

    cd /usr/ports/ports-mgmt/psearch; make install clean
  • E_Cooking Apr 13, 2014 @ 13:44

    Great tutorial for ex Linux/systemd user ;) thank you.

  • Tony Oct 26, 2016 @ 3:13

    Very nice documentation. Thanks!

  • Jimmie Jan 27, 2017 @ 0:43

    To read a file you’re not going to change, use less/more

    less /usr/ports/UPDATING

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum