본문 바로가기
Etc

[Ubuntu] CUDA 설치 & Multi CUDA 설치

by xoft 2024. 4. 28.

본 글에서는 CUDA 설치 방법에 대해 다룹니다. 특히 여러 오픈소스를 실행해야하는 경우, CUDA를 여러개 설치해야 하는 경우가 많이 발생하기에 Multi CUDA설치 방법까지 같이 정리했습니다.

 

1. 설치 가능한 CUDA버전 확인

pytorch 버전 호환 설치글의 '2. CUDA 버전 호환 체크'를 보고 보유중인 GPU에서 설치 할 수 있는 최신 CUDA버전을 확인 합니다.

 

 

2. 설치 확인

에러가 발생 할 수도 있습니다. 현 시점에서 에러는 상관없습니다.

 

$ nvidia-smi         # 그래픽 드라이버 설치 확인
$ nvcc --version     # cuda버전 확인
$ ls -l /usr/local   # 설치된 cuda 버전 확인

 

 

3. 기존 CUDA 제거

선택 사항입니다. 2번에서 에러가 발생하거나, 현재 설치된 CUDA버전이 낮을 경우 실행해주시면 됩니다.

Sudo apt-get purge nvidia* # apt로 설치된 dirver제거. ※ 주의 nvidia-docker가 제거 될 수 있음. 이 경우 $apt list nvidia* 결과 보고 하나씩 제거
Sudo apt-get autoremove    # nvidia* 패키지 의존성에 의해 설치된 다른 패키지도 제거
Sudo apt-get autoclean     # 오래됬거나 불완전한 패키지 제거
Sudo rm -rf /usr/local/cuda* # CUDA 제거

 

 

4. CUDA 설치

우선, (1을 참조하여) 설치 가능한 가장 높은 CUDA버전 확인 후, 필요한 CUDA 버전을 link 에서 다운로드 합니다. 

저는 제 GPU가 지원하는 가장 높은 버전인 CUDA 12.4와 필요로 하는 CUDA 11.8를 다운받았습니다.

 

높은 버전 12.4를 설치합니다. 하나만 설치하실 분은 12.4설치 과정만 따라하시면 됩니다.

 

installation instruction에 따라 명령어를 입력합니다. 가이드 외에 추가로 권한 설정이 필요로 합니다.

$ wget https://developer.download.nvidia.com/compute/cuda/12.4.0/local_installers/cuda_12.4.0_550.54.14_linux.runsudo 
$ chmod 777 cuda_12.4.0_550.54.14_linux.run
$ sh cuda_12.4.0_550.54.14_linux.run

마지막 명령어에서 많은 에러가 발생합니다. 제가 알고 있는 에러에 관해 글 하단에 정리해두었습니다.

조금만 기다리면 아래 메시지가 뜹니다.

accept 입력해줍니다.

저같은 경우엔 Demo suite, Documentation은 불필요해서 설치 제외했습니다. 여기서 CUDA 12.4가 필요 없는 경우, Driver만 설치해주면 됩니다. 아래와 같이 표시되면 제대로 설치된 것 입니다.

nvidia-smi 명령어로 설치가 됬는지 확인해줍니다.

 

(Multi CUDA 설치하실 분은) 동일하게 11.8을 설치합니다.

$ wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run 
$ sudo chmod 777 cuda_11.8.0_520.61.05_linux.run
$ sudo sh cuda_11.8.0_520.61.05_linux.run

 

최신 버전과 다르게 설치해야하는 부분은 여기입니다. Driver는 최신버전으로 설치되었으므로, CUDA Toolkit만 설치해줍니다.

다음단계에서 update-alternatives로 CUDA 버전을 선택하게 만들 것이므로, No를 선택합니다. (이미지 캡쳐 잘못됬네요.)

 

nvidia-smi를 입력하면 동일하게 CUDA 12.4이라고 표시됩니다. CUDA 11.8을 실행하는데 큰 문제는 없습니다. 고질적인 표시 문제라고 합니다(link).

현재 적용된 CUDA 버전을 확인하기 위해선 nvcc -V 또는 nvcc --version를 입력하면 됩니다. nvcc 명령어가 안된다면, 연결이 필요합니다.

$ nvcc -V
Command 'nvcc' not found, but can be installed with:
sudo apt install nvidia-cuda-toolkit
이라고 표시되면 'sudo apt install nvidia-cuda-toolkit'를 하지 말고

$ vi ~/.bashrc
가장 아랫줄에 2줄 추가
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

$ source ~/.bashrc
$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:33:58_PDT_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.31833905_0

 

 

5. CUDA 버전 변경/Switching

update-alternatives를 통해 cuda버전을 여러개 선택 할 수 있게 합니다.

--install로 버전을 등록하고, --config로 선택 할 수 있게 합니다.

$ sudo update-alternatives --install /usr/local/cuda cuda /usr/local/cuda-11.8 118 
update-alternatives: using /usr/local/cuda-11.8 to provide /usr/local/cuda (cuda) in auto mode
$ sudo update-alternatives --install /usr/local/cuda cuda /usr/local/cuda-12.4 124
update-alternatives: using /usr/local/cuda-12.4 to provide /usr/local/cuda (cuda) in auto mode

$ sudo update-alternatives --config cuda
There are 2 choices for the alternative cuda (providing /usr/local/cuda).

  Selection    Path                  Priority   Status
------------------------------------------------------------
* 0            /usr/local/cuda-12.4   124       auto mode
  1            /usr/local/cuda-11.8   118       manual mode
  2            /usr/local/cuda-12.4   124       manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

$ ls -l /usr/local
..
lrwxrwxrwx  1 root root   22  4월 28 01:03 cuda -> /etc/alternatives/cuda
drwxr-xr-x 16 root root 4096  4월  4 02:28 cuda-11.8
drwxr-xr-x 16 root root 4096  4월  4 01:59 cuda-12.4
...

/usr/local/cuda가 /etc/alternatives/cuda에 연결된 것을 볼 수 있습니다.

 

 

 

6. Cuda toolkit 설치 에러 관련

그 외 설치 에러 관련한 내용입니다.

에러1) gcc 관련 Error

예시1) Failed to verify gcc version.
예시2) [ERROR]: unsupported compiler version.

cuda별 지원하는 gcc가 다릅니다 (출처 : link).

gcc version 에러가 발생하면, 다른 버전이 있는지 확인합니다.

$ sudo update-alternatives --config gcc
$ sudo update-alternatives --config g++

 

설치된 gcc가 없다면, 아래명령어로 설치해줍니다. gcc도 Multi로 설치해줍니다. (출처 : link)

$ sudo apt update                    # apt 패키지 목록/버전 업데이트
$ sudo apt install build-essential   # gcc설치에 필요한 패키지 설치
$ sudo apt-get install manpages-dev  # gcc설치에 필요한 패키지 설치

$ sudo apt install gcc-11 g++-11     # 필요한 gcc 버전으로 설치
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11   # gcc변경 가능하도록 등록
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 11   # gcc변경 가능하도록 등록

 

 

 

에러2) Installation failed. See log at /var/log/cuda-installer.log for details.

$ cat /var/log/cuda-installer.log
[INFO]: Driver not installed.
[INFO]: Checking compiler version...
[INFO]: gcc location: /usr/bin/gcc

[INFO]: gcc version: gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) 

[INFO]: Initializing menu
[INFO]: nvidia-fs.setKOVersion(2.19.6)
[INFO]: Setup complete
[INFO]: Installing: Driver
[INFO]: Installing: 550.54.14
[INFO]: Executing NVIDIA-Linux-x86_64-550.54.14.run --ui=none --no-questions --accept-license --disable-nouveau --no-cc-version-check --install-libglvnd  2>&1
[INFO]: Finished with code: 256
[ERROR]: Install of driver component failed. Consult the driver log at /var/log/nvidia-installer.log for more details.
[ERROR]: Install of 550.54.14 failed, quitting

$ cat /var/log/nvidia-installer.log
  ...
   *** Failed CC version check. ***
  ...

*** Failed CC version check. ***

제가 해결한 방법은 2번 방법입니다. log 읽고 적절한 방법 선택하시길 바랍니다.

해결방안1) link

해결방안2) gcc 버전을 update. 아래 방법을 사용해서, 언제든지 gcc를 다른버전으로 바꿀 수 있게합니다. 명령어를 입력해보시면 마지막 2줄은 select하는 구문임을 알 수 있습니다.

$ sudo apt install gcc-12 g++-12 -y
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 110
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-12 120
$ sudo update-alternatives --config gcc
$ sudo update-alternatives --config g++

 

 

 

에러 3) ERROR : Nouveal kernel driver

ERROR: The Nouveau kernel driver is currently in use by your system.  This driver is incompatible with the NVIDIA driver, and must be disabled before proceeding.  Please consult the NVIDIA driver README and your Linux distribution's documentation for details on how to correctly disable the Nouveau kernel driver.
-> For some distributions, Nouveau can be disabled by adding a file in the modprobe configuration directory.  Would you like nvidia-installer to attempt to create this modprobe file for you? (Answer: No)
ERROR: Installation has failed.  Please see the file '/var/log/nvidia-installer.log' for details.  You may find suggestions on fixing installation problems in the README available on the Linux driver download page at www.nvidia.com.

 

해결 방안1) link

해결 방안2) link

 

 

댓글