Saturday 29 October 2016

How To Install Nvidia Drivers for hybrid Intel/Nvidia GPU in Ubuntu 16.04 LTS

TL;DR - if you are having issues getting Nvidia drivers to work in Ubuntu, particularly in a hybrid graphics setup, then check the following:
  1. Secure Boot is disabled during installation if you have a UEFI BIOS.
  2. nomodeset is not set in /etc/default/grub
~

I, like many others before me, have endured the pure pain involved with trying to get a hybrid graphics setup working in Ubuntu. Having searched for a solution myself, it is clear that the system in question impacts the solution massively. Here's my setup and what I did to get it working:

Setup:
  • MSI GT70 2OC (UK edition)
  • CPU: Intel Core i7-4700MQ Processor
  • GPU: Nvidia Geforce GTX 770M/3GB GDDR5 (hybrid with Intel Embedded)
  • OS: Ubuntu 16.04 LTS (Xenial Xerus)

 1. Find out what graphics card you've got:

Pull up a terminal session by pressing CTRL+ALT+T and run the following command:

sudo lshw -numeric -C display

My output looks as follows:


2. Find the latest supported driver


Now head over to https://www.nvidia.co.uk/Download/index.aspx?lang=en-uk and find the latest supported driver for your card (its worth noting here that at the time of writing, the latest available driver from Nvidia was 370, and this may or may not work with my GTX770M. I may test this when I'm bored):

 Make a note of the driver version that is displayed, but don't download it:


Instead, we are going to pull this straight from the Linux graphic drivers team repository.

3. Install the driver and Nvidia Prime


In your terminal, first add the repository:

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update

Once successfully added, install the driver and Nvidia Prime (used to jump between integrated Intel Graphics, and dedicated Nvidia graphics).

sudo apt-get install nvidia-367 nvidia-prime

 Once completed, reboot your laptop. Now this is where the fun can begin, so make sure you can access this blog from somewhere other than the laptop you're working on!


Troubleshooting


If at this point you are chucked back to the login prompt or you hit a black screen, do the following:


Workaround:


Hit CTRL+ALT+F1 to get to command-line. Or if this doesn't work, hold shift whilst you power up your laptop to get to the GRUB, then go into recovery mode.
Run the following command to activate the Intel graphics card:

sudo prime-select intel

Reboot and your problem should go away, however this doesn't fix the issue with the Nvidia GPU.

If this doesn't work, pull the nvidia drivers completely by running

sudo apt-get purge nvidia*

Reboot and your device will use the working nouveau open source driver.

Things to check:

Secure Boot:

If you have a modern laptop with a UEFI style BIOS, it may have something called secure boot. You can find information out about secure boot here. Essentially, you want to disable secure boot in your BIOS and get the Nvidia drivers working prior to re-enabling it (or leave it disabled altogether, that call is yours). So:

Run the following command in terminal to remove the Nvidia drivers:

sudo apt-get purge nvidia*

  Reboot your laptop.
Disable secure boot in your BIOS
Following the instructions to install the Nvidia drivers.
Reboot and check if you can login find after running prime-select nvidia
If you can, re-enable secure boot in your BIOS if you wish.
Check that you can still login with prime-select nvidia working.

nomodeset

If Secure Boot isn't your problem, or disabling it didn't work above, then you may need to get google searching. There are reams of different solutions out there for different models of system. If you have been trying to get the nvidia drivers working prior to landing here, then before you do any more searching,I have one more suggestion for you to try:

Whilst searching for a resolution to this (which turned out to be secure boot), one solution was to set the nomodeset parameter. So what is nomodeset? The answer from this page is:

The newest kernels have moved the video mode setting into the kernel. So all the programming of the hardware specific clock rates and registers on the video card happen in the kernel rather than in the X driver when the X server starts.. This makes it possible to have high resolution nice looking splash (boot) screens and flicker free transitions from boot splash to login screen. Unfortunately, on some cards this doesn't work properly and you end up with a black screen. Adding the nomodeset parameter instructs the kernel to not load video drivers and use BIOS modes instead until X is loaded.

So where to check? And trust me, its worth a check as I was banging my head on my desk for two days trying to find this:

Launch terminal and run the following command to edit the grub file (you can replace nano with a text editor of your choice eg. gedit, vi):

sudo nano /etc/default/grub

My grub file looked like this:


I remember setting nomodeset during the early days of troubleshooting my Nvidia woes, before I had found the Secure Boot solution. But having this set, and configuring prime-select nvidia would give me a login loop, presumably because the kernel was unable to load the nvidia drivers.

So edit your grub file to remove nomodeset and save the grub file (in nano this is CTRL+O, Enter, CTRL+X).

Importantly, you must run the following command for the changes to take effect:

sudo update-grub

Run prime-select nvidia to force the nvidia gpu to be used, then reboot and you may have resolved your issues like me!!


Hopefully this fixes your issues and gets your Nvidia GPU working in Ubuntu, or at the very least points you in the right direction. Once working, a couple of things to remember:

prime-select query will show you the selected graphics card to use

prime-select will activate the selected graphics card following a reboot

Having used Windows on my GT70 for 4 or so years before, I got quite used to Nvidia automatically jumping between integrated and dedicated graphics. I'm not sure if Nvidia Prime can do this, but if you take a look at Nvidia X Server Settings application, you will see something called "Application Profiles". I have a hunch that these may allow certain applications to run using a specific GPU, so in theory you could run prime-select intel normally, then have the Nvidia card jump in when you launch a game. I don't know if this works, there is very little information regarding it but I may have a play with this in the future.

There is also a project called Bumblebee that was created before Prime was available for jumping between integrated and dedicated graphics. This may be worth a look into. 

Wednesday 26 October 2016

When my bad memory worked out for the best

TL;DR: Blue screen of death is commonly caused by bad memory! Bad memory can manifest itself in lots of different ways, and is different between operating systems. I saw BSOD in Windows, and kernel panics only when copying large files in Ubuntu. Bad Memory prompted my move to Ubuntu as my main operating system, but it wasn't without woes!

~

About a week ago, my MSI GT70-2OC running Windows 10 started acting weirdly. I leave the laptop on 99% of the time running Universal Media Server to allow me to watch the media I have stored on it anywhere in my home. Usually on my smartphone whilst washing up, or on my living room TV using my PS4.

I first noticed that now and again UMS would be in-contactable. Physically checking my laptop I'd see that it was at the login screen. After being certain I wasn't going mad and rebooting or forgetting to login, I checked the event logs to find that quite regularly, the laptop was rebooting due to a bugcheck (more commonly known as the Blue Screen of Death!).

Checking the logs further I could see that Microsoft had released an October Driver Pack around the time that I started having problems. I'd had driver issues frequently since upgrading from Windows 8.1 to 10, as MSI no longer support my model, they haven't released official Windows 10 drivers. Unfortunately, Microsoft also auto-install driver updates. There are ways of stopping them from being installed, but this is on a per-update basis, so unless you are eagle-eyed there is a good chance you won't notice until the driver goes on, and in my case, the track pad stops working!

Presuming it was a driver issue, and because I had some time on my hands, I decided to bite the bullet and browse for the most user-friendly version of Linux available. I pulled out my 10 year old laptop (still going strong, also an MSI!) and began browsing the Internet. By this point, my GT70 was unusable, rebooting with a BSOD after every login attempt. After some browsing, I came across Ubuntu 16.04 LTS. Supposedly user-friendly, and the screenshots looked beautiful. I downloaded the ISO image, burned to disk and attempted the install. It failed on three separate attempts. So I grabbed the checksum, and compared the two and noticed the checksum of the ISO I burned to CD was different. This usually means that the ISO become corrupt during download, or when burning to the CD (moral of the story; time and effort can be saved by checking the checksum!).

So I went back to the Ubuntu download site, grabbed the ISO again and compared the checksum. It showed up OK, so presumably the disk burner on my 10 year old laptop is giving up the ghost a little and caused the issue. So, let's get with the times (who uses CDs anymore?) and go for the USB boot method. The process was surprisingly simple from this page, and it not only allows you to install Ubuntu, but also acts as a bootable version, which can be very useful if you lose access to your OS for whatever reason.

So, I booted from the USB and ran the install successfully. I chose to format my 2x RAIDED 128GB SSD's and install Ubuntu here, as I didn't want to lose my media on my 750GB mechanical. The process was a lot quicker from the USB, and I was soon booted into a beautiful Ubuntu desktop.


Now I started to hit some more issues, and it was already getting to 11pm. Firstly, the disk containing my media couldn't be mounted. An error message appeared to say that it contained hibernation data on it, and therefore could only be opened in read only mode. I did some digging and Windows 10 uses a feature called "Fast Startup", that essentially always keeps the OS in hibernation to give you quicker boot times. The workaround, of course, was to boot back into Windows, remove the hibernation file and disable Fast Startup. Impossible for me as I had gone the whole hog and nuked my Window setup!

I could mount the disk in read-only mode using the following command so at least I could still get to the data.

mount -t ntfs-3g -o ro /dev/sda3 /media/windows


So the best course of action would be to backup the data, wipe and re-partition the disk (this would also allow me to use the Linux friendly ext4 file system over Windows traditional NTFS). So I began copying off of my 600GB of media onto a USB hard disk I had lying around. Then I hit my next problem...

After around 5GB of transfer, my laptop would freeze completely, with just the Num Lock LED flashing. I had to hard power down the laptop and log back in but every time I tried to copy the files, to either the USB hard disk or my RAIDED SSD, I hit the same issue.

Alas, I called it a night there, to tackle another day. I had a working laptop, running a nice new OS, UMS was working so the wife and I could carry on watching Arrow and The Flash, and we had our boy's favourite "In the Night Garden" ready for when he wanted it. Only downsides were that I couldn't write to my 750GB disk, and I hadn't even begun to ponder whether my Nvidia Graphics Card was working to its full potential. Or so I thought...

So the next day, I did some research into the flashing Num Lock and freeze when copying files, and the most common answer tended to be dodgy RAM. At this point something clicked. When I had seen Blue screens in the past on Windows, it was almost always down to a dodgy RAM stick. Could it be that one of my RAM sticks were faulty, and it was actually this that was causing my problems all along?

Only one way to find out. I jumped onto my old laptop, went to http://www.memtest86.com/download.htm, and downloaded the bootable USB image. I followed the instructions at http://www.memtest86.com/technical.htm#win to create the USB stick and soon had booted my GT70 into memtest. I kept the default settings and started the test, and quickly the errors were in their thousands.



So I'm fairly certain I've got some bad RAM. Next, I wanted to find out which RAM stick was causing the issues. I have two 8GB sticks that came pre-installed. I wanted to run memtest with only one of the sticks installed to see if the errors dissappear. I found https://www.youtube.com/watch?v=QkL5q1-K15s and got to work taking my keyboard off and getting to my RAM. I quickly found the suspect RAM stick, took it out and began transferring my media off again. It worked perfectly!

Meanwhile, I headed over to uk.crucial.com and got myself a new 8GB stick of RAM. Once the transfer was finished, I used Gparted to wipe and repartition my 750GB using the ext4 file system. Copied everything back and the laptop was working perfectly.

There was one last thing for me to do - get my hybrid graphics working. But that deserves its own separate blog post.

So it was a torrid time, but I'm extremely happy with Ubuntu 16.04 LTS. Its a beautiful, slick and responsive operating system. It actually feels like its unlocked some hidden potential in my GT70, which is nearing 4 years old now.