Compared to another Operating System such as Linux and Windows; FreeBSD has different and unique package management schemes. The Ports Collection, can be found under /usr/ports, contains the Makefiles for a vast amount of software available to configure and compile.
Unlike package management such as apt, yum, yast, and other package management for linux which download the compiled-binary file, ports collection let you download the source code, configure in your way, and compile it. You just configure some aspects before compilation begin and Ports will do the rest for you. You don’t need to worry about dependency, the Ports will take it for you.
In this article we will discuss about how to use Ports Collection: install, find, and remove.
Getting the ports collection
If you did not choose to install the ports collection during the FreeBSD installation then you will need to get it through CVSup. Do install CVSup with pkg_add command (as you don’t have Ports Collection yet) by
pkg_add -r cvsup-without-gui
Make sure you have empty /usr/ports if you run CVSup for first time. And then run CVSup for downloading the ports as described at previous articles.
Another alternative would be using portsnap. Do installation of portsnap by
pkg_add -r portsnap
Now, download / fetch the compressed snapshot to /var/db.
If you are running portsnap for first time, then extract the snapshot info to /usr/ports.
If you have already populate /usr/ports then you need only update it.
If you don’t have enough time to manually download ports collection (or other reason which make you can’t download) and have FreeBSD installer CD, you can use the CD to install ports. Make sure you have the correct version before you proceed. Insert the CD and then enter the following commands:
mount /cdrom cd /cdrom/`uname -r`/ports ./install.sh
Finding a Port
FreeBSD provides so many software and classified it to some categories such as net, www, database, security, etc. With so many ports it can be hard to find the one we are looking for. The easiest way to find a port is to use the ports directory on the FreeBSD website.
To search locally on our machine we can use the locate command if you know a word in the name of the package
locate <package> | grep distinfo
The “grep distinfo” filters out other files in the port directory so each port that comes up is only shown once.
Another method to find a package:
cd /usr/ports make search name=postfix
That command will return a list of all ports that have “postfix” i the name, including some other useful infos. If you need to dig deeper, use “key” instead of “name”
cd /usr/ports make search key=spam
This searches all COMMENT fields of the ports “pkg-description” files and returns the same info as with the “name” search. Make sure to use quotes if you want make more specific searches:
cd /usr/ports make search key="spam filter"
Files in the port directories
– This is the main file that tells where the files for the port are located and the options to build with. If you wish to install the port with certain options it is a good idea to look through this file and see what is available.
– Gives the version number of the package
– This is displayed after the port is installed giving information on how to finish setting it up, or security warnings. If you get the port installed and can’t figure out how to start it, or where the config files are you should look back at the message.
– A list of files in the package.
Installing a port
To install a port change to its directory and do a “make install”. For example, if we were to install the database server mysql 5.5 it would look like this
cd /usr/ports/database/mysql55-server make install
To delete the tar file that was downloaded to install mysql once the install was complete simply append the command “distclean” after install like this
cd /usr/ports/database/mysql55-server make install distclean
To clean out compiled files from the make use “make clean” in the port’s directory
If you wished to uninstall mysql you could do so with the following
cd /usr/ports/database/mysql55-server make deinstall
Many ports give a menu during the install asking you to choose certain options. If you wish to skip these menus “all” may be appended after “make install” to install the port more forcefully
make install all
Thus you are going to install default configuration for that ports.
Removing a saved configuration
Ports that give a menu with installation options after starting the make save your choices and this saved config needs to be manually removed if you want to rebuild the port later and want different options. To delete a saved config go to port’s directory and run
Viewing installed packages and removing them
A list of all installed packages on the machine can be viewed with “pkg_info”
Deleting a package can be done with “pkg_delete” listed by pkg_info above.
pkg_delete -f <package name>
The -f flag forces the package to be deinstalled even if other packages depend on it.
Cleaning up the ports
FreeBSD saves all files downloaded to install the ports to /usr/ports/distfiles. This is useful when you want to do reinstallation as you don’t need to waste your bandwidth and FreeBSD will pick the appropriate package from /usr/ports/distfiles. But if you don’t have much free space, you can clean the distfiles by simply delete it contents.
rm -r /usr/ports/distfiles/*
The above command will removes all of these files, but not the files created during the build. To get rid of these you must run
The -CD flags will remove all files in the work directories within the ports tree and any unreferenced distfiles.
Upgrading a port
A port can be upgraded with the port /usr/ports/ports-mgmt/portupgrade
portupgrade <package name>freebsd