๐Ÿ‘ป OS & Tools/Ubuntu

Ubuntu 22.04์— nvidia driver, cuda, cuDNN, torch ์„ค์น˜ํ•˜๊ธฐ (2024 ver)

๋ณต๋งŒ 2024. 11. 17. 16:10

์„œ๋ฒ„ ์„ธํŒ…์€ ํ•ญ์ƒ ํž˜๋“ค๋‹ค..

 

๋ชฉ์ฐจ

 

0. ubuntu ๋ฒ„์ „ ํ™•์ธ

1. nvidia driver ์„ค์น˜

2. cuda ์„ค์น˜

3. cuDNN ์„ค์น˜

4. torch ์„ค์น˜

 

 

0. ubuntu ๋ฒ„์ „ ํ™•์ธ

์ผ๋‹จ ์‚ฌ์šฉ์ค‘์ธ ubuntu์˜ ๋ฒ„์ „์„ ํ™•์ธํ•ด์ค€๋‹ค.

lsb_release -a

 

 

1. nvidia driver ์„ค์น˜

1-1. ์™ธ๋ถ€์ €์žฅ์†Œ ์ถ”๊ฐ€

 

nvidia driver์„ ํฌํ•จํ•˜๋Š” ์™ธ๋ถ€์ €์žฅ์†Œ(PPA)๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ํ•ด๋‹น ๋‚ด์šฉ์„ ํŒจํ‚ค์ง€ ๋ฆฌ์ŠคํŠธ์— ์—…๋ฐ์ดํŠธํ•ด์ค€๋‹ค.

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

 

 

1-2. ์„ค์น˜ ๊ฐ€๋Šฅํ•œ ๋“œ๋ผ์ด๋ฒ„ ๋ชฉ๋ก ํ™•์ธ

 

ubuntu-drivers๋ฅผ ์ด์šฉํ•ด ์„ค์น˜ ๊ฐ€๋Šฅํ•œ nvidia driver ๋ชฉ๋ก์„ ํ™•์ธํ•œ๋‹ค.

ubuntu-drivers devices

 

๊ทผ๋ฐ ๋‚˜๋Š” ubuntu-drivers๊ฐ€ ์•ˆ๋–ด๋‹ค. apt install๋กœ๋„ ์•ˆ๋˜์—ˆ์Œ. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ..

 

 

1-2-1. sources.list์— ๋‹ค์Œ ์„ค์ •์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

์ถœ์ฒ˜) https://velog.io/@johyonghoon/ubuntu-Unable-to-locate-package-ubuntu-driver-common-%EC%97%90%EB%9F%AC-%ED%95%B4%EA%B2%B0

vi /etc/apt/sources.list

 

์ตœํ•˜๋‹จ์— ๋‹ค์Œ ๋‚ด์šฉ์„ ์ถ”๊ฐ€

deb http://archive.ubuntu.com/ubuntu bionic main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu bionic-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu bionic-updates main restricted universe multiverse

 

์œ„ ๋‚ด์šฉ์„ ๋ฐ˜์˜

sudo apt update
sudo apt install -y ubuntu-drivers-common

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ubuntu-drivers๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๋œฌ๋‹ค.

 

 

1-3. nvidia driver ์„ค์น˜

 

๋‹ค์‹œ 1-2๋กœ ๋Œ์•„๊ฐ€ nvidia driver ๋ชฉ๋ก์„ ํ™•์ธํ•˜๊ณ .. ๋‚˜๋Š” recommended ๋ฒ„์ „์ธ 565 ๋ฒ„์ „์„ ์„ค์น˜ํ–ˆ๋‹ค.

sudo apt install nvidia-driver-565

 

nvidia-driver๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๊น”๋ฆฌ๋ฉด nvidia-smi๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‚œ ์•ˆ๋˜์„œ ์žฌ๋ถ€ํŒ… ํ–ˆ๋”๋‹ˆ ๋๋‹ค.

sudo reboot

...

nvidia-smi

 

 

2. cuda ์„ค์น˜

์ด์ œ cuda๋ฅผ ๊น”์•„์•ผ ํ•˜๋Š”๋ฐ, ๋‚œ torch๋ฅผ ์„ค์น˜ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋งž๋Š” ๋ฒ„์ „์˜ cuda๋ฅผ ๊น”์•„์•ผ ํ•œ๋‹ค.

 

2-1. torch ์„ค์น˜ ํŽ˜์ด์ง€์—์„œ ์„ค์น˜ํ•ด์•ผ ํ•˜๋Š” cuda์˜ ๋ฒ„์ „์„ ํ™•์ธํ•œ๋‹ค.

 

https://pytorch.org/get-started/locally/

 

 

์‚ฌ์šฉํ•  pytorch ๋ฒ„์ „, os์˜ ์ข…๋ฅ˜, ์„ค์น˜์— ์ด์šฉํ•  ํŒจํ‚ค์ง€ (coda, pip ๋“ฑ)์„ ์„ ํƒํ•˜๋ฉด ์–ด๋–ค ๋ฒ„์ „์˜ cuda๋ฅผ ๊น”์•„์•ผ ํ•˜๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค.

 

nvidia-smi๋ฅผ ์„ค์น˜ํ•˜๋ฉด ์ƒ๋‹จ์— cuda version์ด ๋œจ๋Š”๋ฐ, ์ด๊ฑด ์„ค์น˜๋œ cuda toolkit์˜ ๋ฒ„์ „์ด ์•„๋‹ˆ๋ผ, ์„ค์น˜๋œ nvidia driver๊ฐ€ ์ง€์›ํ•˜๋ฉด ์ตœ๋Œ€ cuda ๋ฒ„์ „์ด๋‹ค.

 

๋‚˜๋Š” 12.7๋กœ ๋– ์„œ 12.4๋ฅผ ์„ค์น˜ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

 

2-2. cuda ์„ค์น˜ํ•˜๊ธฐ

 

cuda ์„ค์น˜ ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ ํŽ˜์ด์ง€๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

https://developer.nvidia.com/cuda-toolkit-archive

 

 

๋‹ค ์„ ํƒํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์น˜ ๋ช…๋ น์–ด๊ฐ€ ๋‚˜์˜จ๋‹ค. ํ•˜๋‚˜์”ฉ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰ํ•ด์ค€๋‹ค.

 

 

 

2-2-1. pubkey์ถ”๊ฐ€

 

์œ„์˜ ๋ช…๋ น์–ด์—์„œ sudo apt-get update๋ฅผ ํ–ˆ๋”๋‹ˆ pubkey๊ฐ€ ์—†๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ..

pubkey๋ฅผ ๋“ฑ๋กํ•ด์คฌ๋‹ค.

์ถœ์ฒ˜: https://miiingo.tistory.com/363

 

sudo gpg --keyserver keyserver.ubuntu.com --recv-keys <PUBKEY>

 

ํ„ฐ๋ฏธ๋„์— ๋‚˜์˜จ pubkey๋ฅผ ๋“ฑ๋กํ•ด์คฌ๋”๋‹ˆ ๋„˜์–ด๊ฐ”๋‹ค.

 

 

2-3. ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ถ”๊ฐ€

 

cuda path๋ฅผ ํ™˜๊ฒฝ๋ณ€์ˆ˜์— ์ถ”๊ฐ€ํ•ด์ฃผ์ž.

vi ~/.bashrc

 

๋งจ ์•„๋ž˜์— ๋‹ค์Œ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

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

 

์œ„ ๋‚ด์šฉ์„ ๋ฐ˜์˜

source ~/.bashrc

 

 

2-4. cuda ์„ค์น˜ ํ™•์ธ

 

cuda๊ฐ€ ์ž˜ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ์„ ์‹คํ–‰ํ•œ๋‹ค.

nvcc -V

 

์„ค์น˜๊ฐ€ ์•ˆ๋˜์—ˆ๋‹จ๋‹ค.. ์„ค์น˜๋ฅผ ํ•ด์ฃผ์ž

sudo apt install nvidia-cuda-tookit

 

๊ตฟ

 

 

3. cuDNN ์„ค์น˜

 

์•„๋ž˜ ๋งํฌ์—์„œ ์„œ๋ฒ„ ์„ค์ •๊ฐ’์— ๋งž๊ฒŒ ์„ ํƒํ•œ๋‹ค. cuda ์„ค์น˜์™€ ๋˜‘๊ฐ™๋‹ค. ํšŒ์›๊ฐ€์ž…์„ ํ•ด์•ผ ํ•œ๋‹ค.

https://developer.nvidia.com/cudnn-downloads

 

tensorflow ํ˜ธํ™˜์„ฑ ํ‘œ ๋„ ๋ดค๋Š”๋ฐ ๋”ฑํžˆ ํ˜ธํ™˜์„ฑ ์ด์Šˆ๋Š” ์—†๋Š”๊ฒƒ ๊ฐ™๋‹ค.

 

 

๋‚œ cuda 12๋กœ ์„ค์น˜ํ–ˆ์œผ๋‹ˆ๊นŒ ๋งˆ์ง€๋ง‰ ์ปค๋งจ๋“œ๋ฅผ cudnn-cuda-12๋กœ ์„ค์น˜ํ–ˆ๋‹ค.

 

 

4. torch ์„ค์น˜

 

4-1. torch ์„ค์น˜

 

๋‹ค์‹œ pytorch ์„ค์น˜ํŽ˜์ด์ง€๋กœ ๊ฐ€์„œ ์„œ๋ฒ„ ์„ค์ •๊ณผ cuda ๋ฒ„์ „ ๋“ฑ์„ ์„ ํƒํ•œ๋‹ค.

https://pytorch.org/get-started/locally/

 

 

 

4-2. ์„ค์น˜ ํ™•์ธ

 

cuda๋กœ ์ž˜ ๊ฐ€๋‚˜ ํ™•์ธํ•ด๋ณด์ž.

import torch

torch.cuda.is_available()

x = torch.zeros((3, 3))
x.to("cuda")

 

์™„๋ฃŒ

๋ฐ˜์‘ํ˜•