Screen tearing on Ubuntu 18.04

Solution 1:

Try the instructions here. The quick fix worked well. It was night and day. All I had to do was copy and paste a command into the terminal, but the permanent didn't work. I couldn't make a xorg.conf file.

https://www.cmscritic.com/how-to-fix-nvidia-screen-tearing-in-xfce-mate-kde-lxde-and-others/#/

If you are running Xrce, LXDE, MATE or other desktop environments that are not running a compositor such as Compton and you have a reasonably new NVIDIA card, you are likely pulling your hair out trying to get rid of the awful screen tearing you are experiencing. This screen tearing typically occurs when you are running a proprietary NVIDIA driver under Linux and have one of the aforementioned (or any non-composited) desktop environments. Fortunately for all of us there is a fix, and I'm going to walk you through how to make that nasty NVIDIA screen tearing go away.

I'm not going to get into the specifics as to why this happens because frankly, I have no idea and I'm more interested in how to make it go away.

For me, the tearing is very obvious when moving windows back and forth or scrolling in Firefox.. to the point that it drives me (and I'm sure you as well or you wouldn’t be here) absolutely nuts. So why suffer any longer? Let's get rid of it, shall we?

To test out and see if the fix works properly for you, open up a terminal and execute the following command (just copy it from here and paste it in):

nvidia-settings --assign CurrentMetaMode="nvidia-auto-select +0+0 { ForceCompositionPipeline = On }"

Your screen may flicker for a second and you should then see a drastic improvement. Test out Firefox and moving windows. Did the fix work for you? If so, we’ll need to make it permanent as this is just a temporary fix for now but don't worry, I’ll show you how. If it didn’t work for you, you can try changing the line slightly to this:

nvidia-settings --assign CurrentMetaMode="nvidia-auto-select +0+0 { ForceFullCompositionPipeline = On }"

Note that in the line above, we just changed the ForceCompositionPipeline to ForeFullCompositionPipeline.

Once you've determined that this does indeed fix your issue, we'll need to make sure it's permanent. If it didn't work or caused an issue, simply reboot and you'll be back to your normal desktop.

Now that we've determined that our fix works, to resolve screen tearing once and for all, it's time to make the changes stick. First we need to make sure you have an xorg.conf present in the /etc/X11 directory. To find out, type the following command into a terminal and press Enter:

ls /etc/X11

If you see an xorg.conf file listed like below, proceed.

$ ls /etc/X11  
app-defaults             xinit             Xreset      Xsession.d  
default-display-manager  xkb               Xreset.d    Xsession.options  
fonts                    xorg.conf         Xresources  xsm  
rgb.txt                  xorg.conf.backup  Xsession

If not, skip this next part but keep reading; I've got you covered further down under What to do if you can't find an /etc/X11/xorg.conf.

For those with an xorg.conf listed, in the same terminal window, make a backup of the file by typing the following command and pressing Enter:

cp /etc/X11/xorg.conf /etc/X11/xorg.conf.backup

Now open the file with your favorite text editor (I'm a fan of nano text editor myself) as root and enter the following command:

sudo nano /etc/X11/xorg.conf

This will open up your editor, you will then want to add a line below based on which one worked for you under the Screen Section, so it ends up looking similar to this:

Section "Screen"
    Identifier     "Screen1"
    Device         "Device1"
    Monitor        "Monitor1"
    DefaultDepth    24
    Option         "Stereo" "0"
    Option "metamodes" "nvidia-auto-select +0+0 { ForceFullCompositionPipeline = On }"
    SubSection     "Display"
        Depth       24
    EndSubSection
EndSection

So, if of the two nvidia-settings lines above we tested, the one with ForceFullCompositionPipeline worked, use the example above, if ForceCompositionPipeline worked, simply use that instead in the example above.

Some of you may already have an Option "metamodes" in your /etc/X11/xorg.conf, like in the example below for dual monitor setup:

Option "metamodes" "VGA-0: 1152x864_60 +0+0, DVI-D-0: 1680x1050_60 +0+864"

If this is the case, simply change the line so that it look like this:

Option "metamodes" "VGA-0: 1152x864_60 +0+0, DVI-D-0: 1680x1050_60 +0+864 { ForceCompositionPipeline = On }"

Reboot and your tearing should be gone!

What do do if you can't find an /etc/X11/xorg.conf

Now, for those of you who, like me, did not have an /etc/X11/xorg.conf (most people running Ubuntu, for instance), you may be panicking thinking that you won't be able to apply this fix. Not to worry, I had the same issue, and I won't leave you hanging.

On my main machine, I’m running Linux Mint 18, but this should work with any distribution, I suspect. I did not have an xorg.conf, so what I did was launch the nvidia-settings tool and generated a new one by going to the X Server Display Configuration section and under file name enter /etc/X11/xorg.conf (uncheck merge with existing if it's checked as this is only useful in the instance of where you have one) and then click Save to X Configuration File.

enter image description here
Save to X Configuration File

This should generate one for you which you can then modify using the steps above.

Solution 2:

For NVIDIA Optimus you need to use prime sync.

  1. In a terminal create the /etc/modprobe.d/zz-nvidia-modeset.conf file:

    sudo nano /etc/modprobe.d/zz-nvidia-modeset.conf
    
  2. Insert this line:

    options nvidia_drm modeset=1
    
  3. Press Ctrl+O, Enter, Ctrl+X to save the file and exit.

  4. Run this command:

    sudo update-initramfs -u
    
  5. Reboot.

To check if everything works as expected after rebooting, run this:

xrandr --verbose

Scroll up and look at the top result area. You should see:

PRIME Synchronization: 1 
        supported: 0, 1

See this example:

xrandr results

Solution 3:

I had screen tearing in both GPUs, and making 20-intel.conf file in /etc/X11/xorg.conf.d as shown here solved the problem in the Intel card, but did not let me change to the NVIDIA one.

Screen Tearing in NVIDIA

Follow this guide

Screen Tearing INTEL

Here I implemented the guide I just mentioned above, with the prime-select script. Resuming the following code, the edited prime-select, makes 20-intel.conf and writes

Section "Device"
    Identifier "Intel Graphics"
    Driver "intel"
    Option "TearFree" "true"
EndSection

to the file in /etc/X11/xorg.conf.d when Intel is chosen, and removes the /etc/X11/xorg.conf.d directory when NVIDIA is chosen.

Edit (make a backup file first by copying and renaming the script to prime-select.bak) the prime-select script in /usr/bin in the line 100 where it should be written:

def enable_profile(self, profile):
        current_profile = self._get_profile()

        if profile == current_profile:
            # No need to do anything if we're already using the desired
            # profile
            sys.stdout.write('Info: the %s profile is already set\n' % (profile))
            return True

        sys.stdout.write('Info: selecting the %s profile\n' % (profile))

        self._backup_grub_config()

        if profile == 'nvidia':
            # Always allow enabling nvidia
            # (No need to check if nvidia is available)
            self._enable_nvidia()
        else:
            # Make sure that the installed packages support PRIME
            #if not self._supports_prime():
            #    sys.stderr.write('Error: the installed packages do not support PRIME\n')
            #    return False
            self._disable_nvidia()

        # Write the settings to the config file
        self._write_profile(profile)

        return True

And change it with the following code (be careful with the indentation, it is very important in python):

def enable_profile(self, profile):
        current_profile = self._get_profile()

        if profile == current_profile:
            # No need to do anything if we're already using the desired
            # profile
            sys.stdout.write('Info: the %s profile is already set\n' % (profile))
            return True

        sys.stdout.write('Info: selecting the %s profile\n' % (profile))

        self._backup_grub_config()

        if profile == 'nvidia':
            # Always allow enabling nvidia
            # (No need to check if nvidia is available)
            # Here starts the edited part
            try:
                os.remove("/etc/X11/xorg.conf.d/20-intel.conf")
                os.rmdir("/etc/X11/xorg.conf.d/")
            except:
                sys.stderr.write("Error removing the config files\n") 
           # Here ends the edited part
            self._enable_nvidia()


        else:
            # Make sure that the installed packages support PRIME
            #if not self._supports_prime():
            #    sys.stderr.write('Error: the installed packages do not support PRIME\n')
            #    return False
            # Here starts the edited part
            try:
                os.mkdir("/etc/X11/xorg.conf.d/")
                f=open("/etc/X11/xorg.conf.d/20-intel.conf", "w+")
         # If you want to add more options to the intel config file just edit the following line:
                f.write('Section "Device"\n\tIdentifier  "Intel Graphics"\n\tDriver\t"intel"\n\tOption\t"TearFree"\t"true"\n\tOption\t"DRI"\t"3"\nEndSection')
            except:
                sys.stderr.write("Error making conf files\n")
            # Here ends the edited part          
            self._disable_nvidia()

        # Write the settings to the config file
        self._write_profile(profile)

        return True

Now if you are in the Intel GPU change to the NVIDIA one and come back using

sudo prime-select nvidia

log out, log in and type again

sudo prime-select intel

and log in and log out.

If you are in NVIDIA just type:

sudo prime-select intel

If you don't know wich GPU is in use type:

sudo prime-select query

You can even change between GPUs from the GUI that offers NVIDIA, it does not affect anything.

Good Luck!