幽谷奇峰 | 燕雀鸣幽谷,鸿鹄掠奇峰

Ubuntu12.04 x64系统手动编译安装NVIDIA官方驱动


我的系统环境是:Ubuntu 12.04 64位版,显卡为NVIDIA Geforce 7300 GT。

首先,请确认linux-restricted-modules和linux-restricted-modules-common这两个模块没被安装,如果安装过请卸载掉。

手动编译安装NVIDIA官方驱动

1) 下载适合自己显卡的官方驱动

Nvidia中文官网是 http://www.nvidia.cn/page/home.html

下载自己显卡对应的驱动,放到/home/用户名/目录下,我下载的是 NVIDIA-Linux-x86_64-304.43.run

2) 卸载之前安装过的NVIDIA受限驱动包

1
2
$ sudo apt-get purge nvidia-*
$ sudo apt-get autoremove

3) 安装编译依赖

1
$ sudo apt-get install build-essential pkg-config xserver-xorg-dev linux-headers-`uname -r`

4) 屏蔽掉开源驱动

编辑blacklist.conf文件,

1
$ sudo gedit /etc/modprobe.d/blacklist.conf

在文件尾部添加以下几行:

1
2
3
4
5
blacklist nouveau
blacklist vga16fb
blacklist rivafb
blacklist nvidiafb
blacklist rivatv

5) 重启电脑

1
$ sudo reboot

6) 关闭图形环境

进入登录界面后,按"Ctrl-Alt-F1",进入控制台,运行以下命令关闭图形环境

1
2
3
$ sudo /etc/init.d/kdm stop  #适用于Kubuntu
$ sudo /etc/init.d/gdm stop  #适用于Ubuntu
$ sudo stop lightdm          #适用于Ubuntu11.10 and later

7) 开始安装

进入之前下载的驱动安装文件所在目录,运行:

1
$ sudo sh NVIDIA-Linux-x86_64-304.43.run

安装过程中:

如果提示有旧驱动,询问是否删除旧驱动,选Yes;

如果提示缺少某某模块(modules),询问是否上网下载,选No;

如果提示编译模块,询问是否进行编译,选Ok;

如果提示将要修改Xorg.conf,询问是否允许,选Yes;

如果出现以下错误:

1
2
3
4
5
6
7
8
ERROR: Unable to load the kernel module 'nvidia.ko'.  This happens most
       frequently when this kernel module was built against the wrong or
       improperly configured kernel sources, with a version of gcc that differs
       from the one used to build the target kernel, or if a driver such as
       rivafb/nvidiafb is present and prevents the NVIDIA kernel module from
       obtaining ownership of the NVIDIA graphics device(s), or NVIDIA GPU
       installed in this system is not supported by this NVIDIA Linux graphics
       driver release.

就试试以下命令:

1
$ sudo sh NVIDIA-Linux-x86_64-304.43.run -k $(uname -r)

8) 重启图形环境

1
2
3
$ sudo /etc/init.d/kdm restart    #适用于Kubuntu
$ sudo /etc/init.d/gdm restart    #适用于Ubuntu
$ sudo start lightdm        #适用于Ubuntu11.10 and later

内核升级后自动安装NVIDIA驱动

这样手动安装的NVIDIA官方驱动,以后系统内核每更新一次,都要再重新安装一次。为了省去这个麻烦,我们可以做到让内核升级后自动安装NVIDIA驱动。

首先确定驱动正常工作后, 必需先重启一次。

然后,把你使用的驱动安装文件放到/home/username下,并生成链接。例如:

1
2
$ sudo mv NVIDIA-Linux-x86_64-304.43.run /home/username
$ sudo ln -s /home/username/NVIDIA-Linux-x86_64-304.43.run /home/username/nvidia-driver

这样做的目的是当你更换所用的驱动时,只需要删除原来的链接后再指定新的链接即可,不需要改变我们将使用的脚本。

将以下内容写入自动安装NVIDIA驱动的脚本update-nvidia:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
# Set this to the exact path of the nvidia driver you plan to use
# It is recommended to use a symlink here so that this script doesn't
# have to be modified when you change driver versions.
DRIVER=/home/username/nvidia-driver

# Build new driver if it doesn't exist
if [ -e /lib/modules/$1/kernel/drivers/video/nvidia.ko ] ; then
    echo "NVIDIA driver already exists for this kernel." >&2
else
    echo "Building NVIDIA driver for kernel $1" >&2
    sh $DRIVER -K -k $1 -s -n 2>1 > /dev/null
    if [ -e /lib/modules/$1/kernel/drivers/video/nvidia.ko ] ; then
        echo "   SUCCESS: Driver installed for kernel $1" >&2
    else
        echo "   FAILURE: See /var/log/nvidia-installer.log" >&2
    fi
fi

exit 0

基本上,原理是检查新安装的内核是否安装了正确的NVIDIA驱动,如果没有,脚本将自动为新内核安装驱动模块。

安装该脚本:

1
2
$ sudo mkdir -p /etc/kernel/postinst.d
$ sudo install update-nvidia /etc/kernel/postinst.d

这样,以后内核升级后就会自动安装NVIDIA驱动, 不用再每更新一次内核就重装一次驱动。

使用开源nouveau驱动

出于某些原因,你可能想用回系统安装之初的开源驱动。

首先,确保开源驱动没被卸载,否则,运行以下命令重新安装。

1
$ sudo aptitude install xserver-xorg-video-nouveau

其次,编辑/etc/modprobe.d/blacklist.conf,去掉屏蔽nouveau的行

然后,就有两种方法了:

1.1 不使用xorg.conf

1
$ sudo mv /etc/X11/xorg.conf /etc/X11/xorg.conf.bak

1.2 使用xorg.conf

由于官方驱动和nouveau驱动的xorg.conf现在是兼容的, 只需把xorg.conf中的

1
Driver         "nvidia"

改成:

1
Driver        "nouveau"

例如:

1
2
3
4
5
6
Section "Device"
    Identifier     "Device0"
#   Driver         "nvidia"
    Driver         "nouveau"
    VendorName     "NVIDIA Corporation"
EndSection

参考资料

  1. http://forum.ubuntu.org.cn/viewtopic.php?f=42&t=141431

本作品由 Yysfire 创作,采用知识共享许可协议进行许可。转载时请在显著位置标明本文永久链接:
http://yysfire.github.io/linux/build-and-install-official-NVIDIA-driver-manually-on-ubuntu-12.04.html


相关文章


最后修改
2012-10-07 15:32
发表时间
2012-09-10 14:05
本文标签
Linux 18 NVIDIA 2 Ubuntu 9
关注我

侧栏导航