WSL2에 CUDA 사용하도록 Tensorflow 설치하는 방법을 다룹니다.
2022. 11. 26 최초작성
2023. 2. 12 tensorflow-gpu 대신에 tensorflow 설치
2023. 4. 3 gpu 사용하기 위한 확인사항 추가
2024. 1. 9
2024. 2. 4 텐서플로우 버전 선택 정리
2024. 3. 11 tensorflow[and-cuda] 설치시 발생한 문제 해결방법 추가
2024. 3. 13
2024. 3. 14
2024. 3. 16 NVIDIA 그래픽 카드 드라이버 설치 방법 분리
2025. 5. 3 검토
Tensorflow에서 GPU를 사용하려면 다음 버전 이상으로 윈도우를 업데이트 해야 합니다. 윈도우 11은 아래 언급한 버전보다 상위 버전인것으로 보입니다.
Windows 10 19044 이상(64비트). 이는 Windows 10 버전 21H2, 2021년 11월 업데이트에 해당합니다.
( https://www.tensorflow.org/install/pip?hl=ko#windows-wsl2 )
WSL 설치
WSL2를 설치하는 방법은 다음 링크를 참고하세요.
WSL2를 설치하여 Ubuntu 사용하는 방법
https://webnautes.com/wsl2reul-seolcihayeo-ubuntu-sayonghaneun-bangbeob/
Miniconda 개발 환경 설치
다음 포스트를 참고하여 Miniconda 개발환경을 설치하세요. Ubuntu 설명을 따라하면 됩니다. 파이썬 프로젝트 별로 패키지를 따로 관리할 수 있습니다. 주의할 점은 Miniconda를 설치 후, WSL 창을 새로 열어야 conda 명령이 가능해집니다.
Visual Studio Code와 Miniconda를 사용한 Python 개발 환경 만들기( Windows, Ubuntu, WSL2)
NVIDIA 그래픽 카드 드라이버 설치
다음 포스트를 참고하여 최신 버전 NVIDIA 그래픽 카드 드라이버를 설치하는 것을 권장합니다. 최신 버전 Tensorflow에서 최신 버전 CUDA를 요구하는 경우에 최신 버전의 NVIDIA 그래픽 카드 드라이버 설치가 필요하기 때문입니다.
특별한 경우를 제외하고는 CUDA를 따로 설치할 필요는 없습니다.
Windows에 NVIDIA 그래픽 카드 드라이버 설치하기
https://webnautes.com/windowse-nvidia-geuraepig-kadeu-deuraibeo-seolcihagi/
Tensorflow 설치
tensorflow 2.14 이상을 사용할거라면 방법 1을 사용하고 tensorflow 2.13 이하를 사용할 거라면 방법 2를 진행하세요.
NVIDIA 사이트에서 다운로드 하는 속도가 느린 것으로 예상되어(보통 느립니다.) 방법1에만 miniconda를 사용하여 진행하는 방법을 추가했습니다. 방법2 진행시에는 방법1을 참고하세요 .
conda를 사용하는 방법은 다음 포스트도 참고하세요.
miniconda/anaconda에서 conda 사용하는 방법
https://webnautes.com/miniconda-anacondaeseo-conda-sayonghaneun-bangbeob/
방법 1. CUDA 설치 필요없는 경우
먼저 간단히 설명하고 뒤에서 miniconda로 진행하는 방법을 설명합니다.
tensorflow[and-cuda] 패키지를 설치하면 CUDA를 따로 설치할 필요가 없습니다. Tensorflow[and-cuda] 2.14의 경우엔 CUDA 11 파이썬 패키지가 같이 설치되며 Tensorflow[and-cuda] 2.15의 경우엔 CUDA 12 파이썬 패키지가 같이 설치되었습니다.
Tensorflow[and-cuda] 2.14 이하에서는 CUDA 11, Tensorflow[and-cuda] 2.15 이상에서는 CUDA 12가 사용되는 것으로 보입니다.
각각 다음 명령으로 설치 가능합니다.
$ pip install tensorflow[and-cuda]==2.15
$ pip install tensorflow[and-cuda]==2.14
Tensorflow 2.13 이하 버전에서는 Tensorflow[and-cuda]를 지원하지 않기 때문에 설치하는 도중 다음과 같은 메시지가 보입니다.
WARNING: tensorflow 2.13.0 does not provide the extra 'and-cuda'
다음 명령으로 확인시 CUDA 같이 설치되는 tensorflow[and-cuda] 패키지 버전 리스트와 다르니 유의하세요.
$ pip install tensorflow[and-cuda]==
ERROR: Could not find a version that satisfies the requirement tensorflow== (from versions: 2.12.0rc0, 2.12.0rc1, 2.12.0, 2.12.1, 2.13.0rc0, 2.13.0rc1, 2.13.0rc2, 2.13.0, 2.13.1, 2.14.0rc0, 2.14.0rc1, 2.14.0, 2.14.1, 2.15.0rc0, 2.15.0rc1, 2.15.0, 2.15.0.post1)
ERROR: No matching distribution found for tensorflow==
다음 명령으로 Tensorflow에서 GPU(CUDA)를 사용할 수 있는지 확인합니다.
$ python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
명령 실행결과 맨 마지막에 다음처럼 출력되어야 GPU(CUDA)를 사용할 수 있는 상태입니다.
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
GPU(CUDA)를 사용할 수 없다면 다음처럼 출력됩니다.
[ ]
이제 miniconda를 사용하여 진행해봅니다.
(base) webnautes@webnautes-laptop:~$ conda create -n tensorflow python=3.12
가상환경을 활성화합니다.
(base) webnautes@webnautes-laptop:~$ conda activate tensorflow
(tensorflow) webnautes@webnautes-laptop:~$
CUDA가 포함된 Tensorflow 패키지를 설치합니다. 글 작성 시점(2025. 5. 3)에는 CUDA 12.5, cudnn 9.3, Tensorflow 2.19가 설치됩니다.
(tensorflow) webnautes@webnautes-laptop:~$ pip install tensorflow[and-cuda]
Tensorflow에서 GPU(CUDA)를 사용할 수 있는지 확인합니다.
2025-05-03 12:40:10.642044: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2025-05-03 12:40:10.659901: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:467] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1746243610.674880 834 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1746243610.679392 834 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
W0000 00:00:1746243610.691370 834 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1746243610.691434 834 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1746243610.691440 834 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1746243610.691443 834 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
2025-05-03 12:40:10.695358: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI AVX512_BF16 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
실행 결과 마지막에 [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')] 처럼 보이면 Tensorflow에서 GPU를 사용할 수 있습니다.
문제가 있다면 다음처럼 tensorflow 버전을 지정하여 설치를 시도해봐야 합니다.
$ pip install tensorflow[and-cuda]==2.15
방법 2. CUDA 설치 필요한 경우
이 경우 기존 Tensorflow 패키지 이름으로 설치를 진행하며 따로 설치되어있는 CUDA를 사용합니다.
https://www.tensorflow.org/install/source#gpu 에 따르면 GPU를 사용하는 Tensorflow 2.12, Tensorflow 2.13을 설치하려면 CUDA는 11.8, cuDNN은 8.6이어야 합니다.
다음 포스트를 참고하여 WSL2에 CUDA를 설치하세요.
WSL2에 CUDA 설치하는 방법
https://webnautes.com/wsl2e-cuda-seolcihaneun-bangbeob/
이제 파이썬 가상환경을 생성한후 tensorflow를 설치합니다.
(base) webnautes@webnautes-laptop:~$ conda create -n tensorflow python=3.12
가상환경을 활성화합니다.
(base) webnautes@webnautes-laptop:~$ conda activate tensorflow
(tensorflow) webnautes@webnautes-laptop:~$
tensorflow 패키지를 설치합니다.
$ pip install tensorflow
Tensorflow에서 GPU(CUDA)를 사용할 수 있는지 확인합니다.
$ python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
2025-05-03 14:35:14.177259: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2025-05-03 14:35:14.194227: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:467] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1746250514.208495 501 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1746250514.212912 501 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
W0000 00:00:1746250514.225724 501 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1746250514.225770 501 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1746250514.225775 501 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1746250514.225779 501 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
2025-05-03 14:35:14.229968: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI AVX512_BF16 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
명령 실행결과 맨 마지막에 다음처럼 출력되어야 GPU(CUDA)를 사용할 수 있는 상태입니다.
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
GPU(CUDA)를 사용할 수 없다면 다음처럼 출력됩니다.
[ ]
Member discussion