ubuntu下安装CUDA

本文详细介绍了如何在Ubuntu系统上安装CUDA3.0,包括安装所需的显卡驱动、toolkit和SDK,设置环境变量,配置GCC版本,以及解决安装过程中可能出现的问题。
部署运行你感兴趣的模型镜像

   经过本人三天的奋战,终于在自己机器上完成了CUDA3.0的安装,本人的安装的版本应该是普适的,可以根据自己情况对比安装。

cuda 3.0不支持gcc 4.4或以上的版本,建议gcc 4.3或以下。

  http://archive.ubuntu.com/ubuntu/pool/universe/g/gcc-4.3/提供了安装包。一般要安装五个文件:

 

gcc-3.4-base_3.4.6-6ubuntu3_i386.deb、
gcc-3.4_3.4.6-6ubuntu3_i386.deb、
cpp-3.4_3.4.6-6ubuntu3_i386.deb、
g++-3.4_3.4.6-6ubuntu3_i386.deb、
libstdc++6-dev_3.4.6-6ubuntu3_i386.deb

(上面是gcc 3.4安装文件,可以根据自己需要去下载其他版本,其他版本也类似这五个文件)

顺序安装这五个文件,最后两个文件会出现相互依赖的错误,利用sudo dpkg --force-depends -i xxx.deb强制安装

 

 

一、安装显卡驱动

下面我们来安装驱动

1) 载掉原来的驱动(如果有):sudo nvidia-installer --uninstall

(2) 关闭gdm:sudo /etc/init.d/gdm stop

(3) 屏幕黑了之后,使用alt+F1进入终端

(4) 进入下载目录,运行安装: sudo sh devdriver_3.1_linux_32_256.40.run

(6) 点击 “Accept”接受 LICENSE,等待进度条完成后,运行startx回到可视化桌面

 

中间可能会出现以下问题

"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."

解救方法如下:

I'm running the 195.36.24 driver and it's running smoothly.
Getting it to work was the difficult part 'cause the installation procedure differs from the previous versions.
Here's what I did to install the binary drivers from nvidia's site.

1) Make sure you downloaded the correct driver from nvidia's site.

2) remove current nvidia packages:
(if you have enabled nvidia in system->administration->hardware drivers, then disable them first and possibly reboot)
sudo apt-get purge nvidia-*

3) Create the file "/etc/modprobe.d/nvidia-graphics-drivers.conf" with the following contents

blacklist vga16fb
blacklist nouveau
blacklist lbm
-nouveau
blacklist nvidia
-173
blacklist nvidia
-96
blacklist nvidia
-current
blacklist nvidiafb



4) create or adjust the /etx/X11/xorg.conf file to:

Section "Device"
       
Identifier       "Device0"
       
Driver           "nvidia"
       
VendorName       "NVIDIA Corporation"
EndSection



5) reboot

6) it will tell you that it can't start X ( low graphics mode ) , then choose a text based console login
(if it does start in X, just press ctrl-F1, log yourself in and call "sudo service gdm stop"

7) run the nvidia installer and when it asks to run nvidia-x-config, answer yes

8) start X: sudo service gdm start

择自:http://forums.nvidia.com/index.php?showtopic=167422

 

二、安装toolkit

(1) 进入下载目录,运行安装: sudo sh cudatoolkit_3.1_linux_32_ubuntu9.10.run

(2) 回车,安装至默认位置 /usr/local/cuda

三、安装sdk

(1) 进入下载目录,运行安装: sh gpucomputingsdk_3.1_linux.run(建议还不要用管理员安装)

(2) 回车,安装至默认位置 ~/NVIDIA_GPU_Computing_SDK

 

四、设置环境变量

运行sudo gedit ~/.bashrc, 在打开的文件最后加入下面脚本:

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib:$LD_LIBRARY_PATH

五、配置GCC

因为CUDA是使用GCC4.3编译的,所以必须下载GCC4.3。

sudo apt-get install g++-4.3 gcc-4.3

下面是为了方便在多个GCC版本间切换

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 44 --slave /usr/bin/g++ g++ /usr/bin/g++-4.4 --slave /usr/bin/gcov gcov /usr/bin/gcov-4.4
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 43 --slave /usr/bin/g++ g++ /usr/bin/g++-4.3 --slave /usr/bin/gcov gcov /usr/bin/gcov-4.3

显示gcc版本
sudo update-alternatives --display gcc
修改gcc版本
sudo update-alternatives --config gcc 运行这行后选择4.3版的gcc

六、编译sdk example

进入sdk安装目录:

cd ~/NVIDIA_GPU_Computing_SDK/C

运行make编译。

你可能会遇到缺少以下包:
1.缺少glu.h
sudo apt-get install freeglut3-dev
2.gcc: error trying to exec ‘cc1plus’
## 如果安装gcc4.3却没有安装g++4.3可能会出现这个错误,安装g++4.3即可.
sudo apt-get install g++-4.3
3./usr/bin/ld: cannot find -lparamgl
## /usr/bin/ld: cannot find -lrendergl
## these are built as part of the CUDA SDK
## cd to your NVIDIA_GPU_Computing_SDK/C
sudo make
4.inline function ‘void* memset(void*, int, size_t)’ cannot be declared weak
如果是这个错误,可能是使用了gcc/g++ 4.4,重新安装gcc 4.3 and g++4.3即可,方法上面有.
5.CUDA version is insufficient for CUDART version.
重新安装新的显卡驱动程序.
你可以使用”find /usr/lib | grep cuda.so“来查看cuda的版本.
6.cudaSafeCall() Runtime API error : no CUDA-capable device is available.
要不是你的电脑上根本没有支持cuda的显卡,要么是你没有正确安装驱动程序.

7.出现”cannot find lGL”错误
sudo rm /usr/lib/libGL.so
cd /usr/lib
sudo ln -s libGL.so.256.40 libGL.so ;其中256.40是我安装的驱动版本,可能每台电脑会有不同
还有一种版本
 要安装如下的库:apt-get install freeglut3-dev libXi-dev and libXmu-dev 或者 libglut3-dev 
      cuda 2.3使用 g++ 4.3编译; 
 
    @ubuntu:~$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 44  --slave /usr/bin/g++ g++ /usr/bin/g++-4.4 --slave /usr/bin/gcov gcov /usr/bin/gcov-4.4 
    
    @ubuntu:~$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 43 --slave /usr/bin/g++ g++ /usr/bin/g++-4.3 --slave /usr/bin/gcov gcov /usr/bin/gcov-4.3 
 
    @ubuntu:~$ sudo update-alternatives --display gcc 
 
    @ubuntu:~$ sudo update-alternatives --config gcc  

     [b]环境变量的设置: 

    export PATH=/usr/local/cuda/bin:$PATH 
    export LD_LIBRARY_PATH=/usr/local/cuda/lib:$LD_LIBRARY_PATH 

    把以上两条命令写到~/.bashrc中。 
    注:ubuntu中环境变量写在.bashrc或.profile文件中。 

     [c] Modifying ld.so.conf: (this file located in the /etc derectory)

     把     <CUDA_INSTALL_PATH>/cuda/lib    (32bit)
         或者     <CUDA_INSTALL_PATH>/cuda/lib64  (64bit)
     写入ld.so.conf文件中 .


[4]bulid the SDK
        
    @ubuntu:~$ cd [to your NVIDIA_GPU_Computing_SDK/C]
    @ubuntu:~$ make

        -release :     input "make"
    -debug   :     input "make dbg=1"
    -emurelease:    input "make emu=1"
    -emudebug:    input "make emu=1 dbg=1"

        or 
       @ubuntu:~$ cd [to your NVIDIA_GPU_Computing_SDK/C]/src/[project name]
           @ubuntu:~$ make


[5]execute example project

    @ubuntu:~$ cd [to your NVIDIA_GPU_Computing_SDK/C]/src/deviceQuery
    @ubuntu:~$ make
    @ubuntu:~$ cd [to your NVIDIA_GPU_Computing_SDK/C]/bin/linux/release
    @ubuntu:~$ ./deviceQuery

---------------result-----------------
./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

There is 1 device supporting CUDA

Device 0: "GeForce GTS 250"
  CUDA Driver Version:                           3.10
  CUDA Runtime Version:                          3.10
  CUDA Capability Major revision number:         1
  CUDA Capability Minor revision number:         1
  Total amount of global memory:                 536674304 bytes
  Number of multiprocessors:                     16
  Number of cores:                               128
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       16384 bytes
  Total number of registers available per block: 8192
  Warp size:                                     32
  Maximum number of threads per block:           512
  Maximum sizes of each dimension of a block:    512 x 512 x 64
  Maximum sizes of each dimension of a grid:     65535 x 65535 x 1
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             256 bytes
  Clock rate:                                    1.62 GHz
  Concurrent copy and execution:                 Yes
  Run time limit on kernels:                     No
  Integrated:                                    No
  Support host page-locked memory mapping:       No
  Compute mode:                                  Default (multiple host threads can use this device simultaneously)
  Concurrent kernel execution:                   No
  Device has ECC support enabled:                No

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 3.10, CUDA Runtime Version = 3.10, NumDevs = 1, Device = GeForce GTS 250


PASSED

Press <Enter> to Quit...
-----------------------------deviceQuery end----------------------------------

    @ubuntu:~$ cd [to your NVIDIA_GPU_Computing_SDK/C]/src/bandwidthTest
    @ubuntu:~$ make
    @ubuntu:~$ cd [to your NVIDIA_GPU_Computing_SDK/C]/bin/linux/release
    @ubuntu:~$ ./bandwidthTest

----------------reslut--------------------------------
[bandwidthTest]
./bandwidthTest Starting...

Running on...

 Device 0: GeForce GTS 250
 Quick Mode

 Host to Device Bandwidth, 1 Device(s), Paged memory
   Transfer Size (Bytes)    Bandwidth(MB/s)
   33554432            1470.6

 Device to Host Bandwidth, 1 Device(s), Paged memory
   Transfer Size (Bytes)    Bandwidth(MB/s)
   33554432            1308.4

 Device to Device Bandwidth, 1 Device(s)
   Transfer Size (Bytes)    Bandwidth(MB/s)
   33554432            51895.2


[bandwidthTest] - Test results:
PASSED


Press <Enter> to Quit...
-----------------------------------------------------------

OVER! good luck !
    
[参考信息]
[01]http://www.docin.com/p-44550472.html (CUDA在linux系统上安装指南)
[02]http://www.devbaldwin.com/blog/2010/03/15/setting-up-cuda-on-ubuntu-9-10/
[03]http://www.linuxidc.com/Linux/2010-02/24329p3.htm (Ubuntu下CUDA安装)
[04]http://forums.nvidia.com/index.php?showtopic=167422

增加gcc3.4和gcc 4.4可选项

$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 40

$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-3.4 30

切换版本到gcc-3.4

$ sudo update-alternatives --config gcc

现有 2 个可选项,它们都提供了“gcc”

<    选择        可选项-----------------------------------------------         

1    /usr/bin/gcc-3.4*+       

2    /usr/bin/gcc-4.2

要维持缺省值[*],按回车键,或者键入选择的编号:1

使用“/usr/bin/gcc-3.4”来提供“gcc”。

至此gcc3.4安装成功

可以利用gcc --version来查看版本

整完后,会发现,系统找不到g++了,所以还得自己安装一下g++:

sudo apt-get install g++

您可能感兴趣的与本文相关的镜像

PyTorch 2.7

PyTorch 2.7

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

Ubuntu系统上安装CUDA通常涉及几个主要步骤:确认硬件兼容性、准备环境以及通过合适的源或运行文件来完成安装。 ### 准备工作 1. **验证GPU支持** 需要先确保您的机器上有NVIDIA GPU,并且已正确识别。可以通过如下命令检查: ```bash lspci | grep -i nvidia ``` 2. **查看当前驱动版本和支持的CUDA工具集** 使用`nvidia-smi`可以显示有关显卡及其驱动的信息,同时也列出了该驱动所支持的最大CUDA Toolkit版本[^3]。 ```bash nvidia-smi ``` 3. **选择适合系统的CUDA版本** 不同的操作系统发行版可能推荐不同的CUDA版本。比如对于较新的Ubuntu 22.04 LTS来说,官方提供了针对CUDA 12.5的支持方案[^2];而如果是在旧一点的Ubuntu 18.04环境下,则更倾向于采用像CUDA 10.0这样的稳定长期维护版本[^1]。 ### 安装方法一:使用APT仓库(适用于最新Linux发行版) 此方法特别适合那些希望快速部署并且不需要特定历史版本用户的场景。 ```bash # 下载并配置存储库元数据 wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600 # 获取本地deb包并注册到dpkg数据库中 wget https://developer.download.nvidia.com/compute/cuda/12.5.0/local_installers/cuda-repo-ubuntu2204-12-5-local_12.5.0-555.42.02-1_amd64.deb sudo dpkg -i cuda-repo-ubuntu2204-12-5-local_12.5.0-555.42.02-1_amd64.deb # 导入GPG密钥环至指定目录 sudo cp /var/cuda-repo-ubuntu2204-12-5-local/cuda-*-keyring.gpg /usr/share/keyrings/ # 更新软件列表后开始正式安装所需组件 sudo apt-get update sudo apt-get -y install cuda-toolkit-12-5 ``` ### 安装方法二:手动下载.run文件进行自定义安装 这种方法允许更加灵活地控制整个流程,包括能够跳过某些不必要的部分如图形界面开发套件等。 ```bash # 替换URL为你想要的具体版本链接 wget https://developer.download.nvidia.com/compute/cuda/11.4.4/local_installers/cuda_11.4.4_470.82.01_linux.run chmod +x cuda_*.run ./cuda_*.run --extract=`pwd` # 或者直接 ./cuda_*.run 开始交互式安装向导 ``` ### 后续操作 一旦基础架构搭建完毕,请记得调整PATH变量以便全局访问编译器及其他实用程序: ```bash export PATH=/usr/local/cuda/bin${PATH:+:${PATH}} export LD_LIBRARY_PATH=/usr/local/cuda/lib64\ ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} ``` 建议将这两行加入`.bashrc`或者相应的shell初始化脚本里以实现持久化设置。 最后测试一下是否成功激活了新环境: ```bash nvcc --version # 应显示出对应版本号 ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值