How to use nvidia optimus graphics card in Ubuntu

If you have a nvidia optimus graphics card and you want use it, probably you couldn't.

To work with this graphics card you'll have to give several steps.

Step 1. Install private nvidia drivers

You can see how you can do it here

Step 2. Activate nvidia optimus graphics card in real time with command nvidia-smi from command line ( tool can be used to set the compute mode for devices. Documentation for nvidia-smi can be obtained by passing a -h option to it. See information here)

$ sudo nvidia-smi

Step 3. Create a init script /etc/init.d/nvidia-smi to create nvidia devices always that your computer start

#!/bin/bash

/sbin/modprobe nvidia-current

if [ "$?" -eq 0 ]; then

  # Count the number of NVIDIA controllers found.
  N3D=`lspci | grep -i NVIDIA | grep "3D controller" | wc -l`
  NVGA=`lspci | grep -i NVIDIA | grep "VGA compatible controller" | wc -l`
  N=`expr $N3D + $NVGA - 1`

  for i in `seq 0 $N`; do

    mknod -m 666 /dev/nvidia$i c 195 $i;

  done

  mknod -m 666 /dev/nvidiactl c 195 255

else
  exit 1
fi

nvidia-smi --loop-continuously --interval=60 --filename=/var/log/nvidia-smi.log &

# Put compute mode rules to Normal mode for VGA compatible controller 
# nvidia-smi -g 0 -c 2

#for i in `seq 0 $N`; do
  # To allow to compute in both nvidia graphics card
  # Put compute mode rules to exclusive mode for 3D controller
#  nvidia-smi -g $N -c 1
#done

# Put compute mode rules to exclusive mode for 3D controller and enable ecc
nvidia-smi -g 0 -c 1 -e 1
# Put compute mode rules to exclusive mode for 3D controller and disable ecc
nvidia-smi -g 1 -c 1 -e 0 

Script copied from here

Step 4. Give correct permissions to this script

$ sudo chmod 755 /etc/init.d/nvidia-smi

Step 5. Activate it when computer start

$ sudo update-rc.d nvidia-smi default

After, if you want to remove it from runlevels

$ sudo update-rc.d -f nvidia-smi remove