FreeBSD with NVidia drivers

There have been various issues with FreeBSD-11.2 and later with the NVidia drivers. There is, at time of writing, no mention of this in /usr/ports/UPDATING. This was first written around the time of 11.2 so there will be some dated references, but the instructions still work with 12.x This is taken from various posts on FreeBSD forums and my own experience.

Firstly, if upgrading from 11.1, remove the NVida drivers and any reference to them in /boot/loader.conf or rc.conf. We will reinstall them once 11.2 is in place.

At the present time, the kernel used for packages also has to work with FreeBSD-11.1. It will be going EOL (End Of Life) at the end of September, so hopefully, as of October, one can use packages for nvidia-drivers, but at present, you must build from ports.

In addition, as seen in this thread, forums user nihr43 mentions that they had to pkg lock nvidia drive (and also virtualbox) to keep pkg from updating those two programs and again breaking the desktop.

The Nvidia driver port requires having the kernel source code in place, so you must first install FreeBSD source code. This can be done with
svnlite co https://svn.freebsd.org/base/releng/11.2 /usr/src

This will put the source code into /usr/src.

While this still works in December, 2020, FreeBSD is switching to git. So you may need git to install the source code. Currently, the most detailed instructions, as of December 2020, seem to be the Werner Losh page.

You must next figure out if your NVidia card uses the legacy nvidia-driver-340 or the regular nvidia-driver. You can find a (not necessarily THE) model of your card with pciconf -lv. This will give some sort of indication of what card you have, though it might not agree with NVidia's. Here is their listing of legacy cards. See if the model you get from pciconf matches or seems similar to one there. If so, you probably want to install the x11/nvidia-driver-340 driver. Regardless, install the driver from the port, using your favorite ports management tool. With portmaster (assuming you are installing the latest driver)
portmaster x11/nvidia-driver

If using pkg, then
pkg install -y nvidia-driver

If you have a really old NVidia card, you may even need the nvidia-driver-304.

Once the port is installed, you can view the pkg-msg with
pkg info -D nvidia-driver

This will tell you to add, if it's the newer nvidia driver nvidia-modeset_load="YES"

to /boot/loader.conf. If it's the legacy driver, then add nvidia_load="YES" to loader.conf. You can, if you prefer, add
kld_list="nvidia-modeset"

or, for the legacy, kld_list="nvidia" to /etc/rc.conf

The pkg info says that linux_enable="YES" in rc.conf is needed, but I've found, if not using any Linux applications, that that's not necessary. Depending upon your use case, you may be using some other Linux applications though, and it doesn't seem to add much overhead.

Lately I've been doing it with sysrc. The commands are
sysrc -f /boot/loader.conf nvidia-modset_load="YES"

for /boot/loader.conf, and
sysrc kld_list+=nvidia-modeset

if you prefer to put it in for /etc/rc.conf.

It is usually necessary to create a /usr/local/etc/X11/xorg.conf.d file. In there make a file. Its name must end with .conf but otherwise, any name you choose is fine. Putting 00 at the start of the name means that it will be read before any other files, such as synaptic settings, that you might add to the directory later. So, create, for example, 00nvidia.conf which should read
Section "Device"
        Identifier "NVIDIA Card"
        VendorName "NVIDIA Corporation"
        Driver "nvidia"
EndSection

After this, especially if added to /boot/loader.conf, you may need to reboot to load the module. (This can probably be skipped, but as I've only used the Nvidia on workstations, not servers, I've never bothered checking. You can first try with kldload nvidia-modeset)

. At this point if it still fails, saying no screens found, I've usually been able to fix it by installing the nvidia-xconfig package (this can be installed with pkg install) and then running nvidia-xconfig. This will create a /etc/X11/xorg.conf file that should work with your NVidia card. You can probably remove large portions of it, but I've not experimented.

The only other issue I ever ran into was on only one of about 4 machines. X still wouldn't start and in that case, it turned out that the nvidia-xconfig hadn't created a BusID entry in the xorg.conf file, and this particular machine (though not some others) required it for X to start.

I was able to get the BusID entry from starting X, and then, even though it failed, looking at the /var/log/Xorg.0.log. You can also get it from pciconf -lv. It will show something like
vgapci0@pci0:1:0:0:  

above the listing. In this case, you want an entry of
BusID    "PCI:1:0:0"

In other words, leave off the first 0 and last colon.

At present, early August, 2018, these are the steps necessary to get an NVidia card running on FreeBSD-11.2-RELEASE