如何在VirtualBox Ubuntu安装TensorFlow GPU版及搭建Anaconda环境,能否装CUDA/CuDNN
Hey there! Let's walk through setting up your deep learning environment step by step—since you're working with a Windows 10 host (GTX 1060) running Ubuntu in VirtualBox, using Anaconda with Python 3.6, we need to nail version compatibility and VirtualBox settings first.
First: Can you install CUDA & CuDNN in the Ubuntu VM?
Absolutely! But you need to prep your VirtualBox setup first to enable GPU support, then pick versions that play nicely with Python 3.6 and TensorFlow. Let's cover that setup first, then move to TensorFlow GPU installation.
Prerequisite: VirtualBox GPU Setup (Critical!)
Before installing any NVIDIA libraries, make sure your VM is configured to access the GPU:
- On your Windows host, install the VirtualBox Extension Pack (match it exactly to your VirtualBox version—mismatches break GPU acceleration).
- Open your Ubuntu VM settings:
- Go to Display > Screen, set "Video Memory" to the maximum allowed (usually 128MB or higher).
- Check the box for Enable 3D Acceleration.
- Allocate at least 8GB of RAM and 4+ CPU cores to the VM (deep learning needs resources!).
Step 1: Install CUDA Toolkit (Compatible with Python 3.6)
Python 3.6 is only supported up to TensorFlow 2.4.4, which requires CUDA 11.0 (newer CUDA versions drop Python 3.6 support). Here's how to install it:
- Open your Ubuntu terminal and update your system:
sudo apt update && sudo apt upgrade -y - Install required dependencies and the NVIDIA driver compatible with CUDA 11.0 (driver 450.x works with GTX 1060):
sudo apt install build-essential gcc libnvidia-common-450 libnvidia-gl-450 nvidia-driver-450 -y - Reboot your VM to apply the driver changes.
- Download the CUDA 11.0 deb package from NVIDIA's CUDA Archive (look for the Ubuntu 20.04 local installer for CUDA 11.0.3) — you can find this in NVIDIA's official archive section.
- Install the repo and CUDA:
sudo dpkg -i cuda-repo-ubuntu2004-11-0-local_11.0.3-450.51.06-1_amd64.deb sudo apt-key add /var/cuda-repo-ubuntu2004-11-0-local/7fa2af80.pub sudo apt update sudo apt install cuda-11-0 -y - Add CUDA to your system PATH (add these lines to
~/.bashrcor~/.zshrcdepending on your shell):export PATH=/usr/local/cuda-11.0/bin${PATH:+:${PATH}} export LD_LIBRARY_PATH=/usr/local/cuda-11.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} - Refresh your shell to apply changes:
source ~/.bashrc - Verify CUDA is installed:
You should see output showing CUDA 11.0.nvcc --version
Step 2: Install CuDNN (Compatible with CUDA 11.0)
TensorFlow 2.4.4 needs CuDNN 8.0.5 for CUDA 11.0:
- Download the CuDNN 8.0.5 deb files (runtime library and development files) from NVIDIA's CuDNN Archive, ensuring you pick the version compatible with CUDA 11.0. You'll need a free NVIDIA account to access these files.
- Install them in order via terminal:
sudo dpkg -i libcudnn8_8.0.5.39-1+cuda11.0_amd64.deb sudo dpkg -i libcudnn8-dev_8.0.5.39-1+cuda11.0_amd64.deb - Verify CuDNN installation:
You should see version 8.0.5 listed.cat /usr/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
Step 3: Install TensorFlow GPU via Anaconda
Now that CUDA and CuDNN are set up, let's install TensorFlow GPU in your Anaconda environment:
- If you don't have a dedicated environment yet, create one with Python 3.6:
conda create -n tf_gpu_env python=3.6 - Activate the environment:
conda activate tf_gpu_env - Install TensorFlow GPU 2.4.4 (the latest version compatible with Python 3.6 and our CUDA setup):
pip install tensorflow-gpu==2.4.4 - Verify TensorFlow can detect your GPU:
If everything works, you'll see your GTX 1060 listed as a physical GPU device.python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
Quick Notes to Keep in Mind
- VirtualBox's GPU support is limited compared to bare-metal or hypervisors like VMware—don't expect full GPU performance, but it's great for learning and small projects.
- If you hit driver issues, double-check that 3D acceleration is enabled in VirtualBox settings and that the NVIDIA driver in Ubuntu is properly installed.
- Stick to the version combinations we listed—mixing newer CUDA/TensorFlow versions will break compatibility with Python 3.6.
内容的提问来源于stack exchange,提问作者Zubair Khan

