Ubuntu 18.04下TensorFlow 2.2 GPU安装失败求助及规范步骤需求
Hey there, I’ve been through this exact issue with mismatched CUDA libraries and GPU detection in TensorFlow 2.2 on Ubuntu 18.04. Let’s walk through a strict, version-aligned setup from a fresh server—this will eliminate those libcublas.so errors and ensure TensorFlow can talk to your GPU properly.
1. Update System & Install Prerequisites
First, get your server up to date and install essential build tools:
sudo apt update && sudo apt upgrade -y sudo apt install -y build-essential cmake git unzip zip zlib1g-dev
2. Install NVIDIA GPU Driver (Compatible with CUDA 10.1)
TensorFlow 2.2 relies on CUDA 10.1, which requires NVIDIA driver version 418.x. We’ll use the official PPA for a hassle-free install:
# Add NVIDIA graphics drivers repo sudo add-apt-repository ppa:graphics-drivers/ppa sudo apt update # Install the required driver version sudo apt install -y nvidia-driver-418 # Reboot to apply the driver (critical step!) sudo reboot
After rebooting, verify the driver works with:
nvidia-smi
You should see your GPU details and driver version (418.x) listed.
3. Install CUDA Toolkit 10.1 (Exact Version for TF 2.2)
Do NOT install the latest CUDA version—TF 2.2 only supports 10.1. Follow these steps:
# Download and set up CUDA 10.1 repo wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600 wget http://developer.download.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda-repo-ubuntu1804-10-1-local-10.1.243-418.87.00_1.0-1_amd64.deb sudo dpkg -i cuda-repo-ubuntu1804-10-1-local-10.1.243-418.87.00_1.0-1_amd64.deb sudo apt-key add /var/cuda-repo-10-1-local-10.1.243-418.87.00/7fa2af80.pub sudo apt update # Install CUDA 10.1 (skip the driver since we already installed it) sudo apt install -y cuda-toolkit-10.1
Set CUDA Environment Variables
Add these to your shell profile to ensure system-wide access to CUDA libraries:
# Append to ~/.bashrc echo 'export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}}' >> ~/.bashrc echo 'export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}' >> ~/.bashrc # Apply changes immediately source ~/.bashrc
Verify CUDA installation with:
nvcc --version
You should see release 10.1 in the output.
4. Install cuDNN 7.6.5 (Compatible with CUDA 10.1 & TF 2.2)
cuDNN is required for GPU-accelerated TensorFlow operations. We need version 7.6.5 for this setup:
# Download cuDNN packages (matches CUDA 10.1) wget https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/libcudnn7_7.6.5.32-1+cuda10.1_amd64.deb wget https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/libcudnn7-dev_7.6.5.32-1+cuda10.1_amd64.deb # Install the packages sudo dpkg -i libcudnn7_7.6.5.32-1+cuda10.1_amd64.deb sudo dpkg -i libcudnn7-dev_7.6.5.32-1+cuda10.1_amd64.deb # Update system library cache sudo ldconfig
5. Set Up Python Virtual Environment (Avoid Dependency Conflicts)
Using a virtual environment prevents conflicts with system Python packages:
# Install Python 3.7 (TF 2.2 works best with Python 3.6-3.8) sudo apt install -y python3.7 python3.7-dev python3-pip # Install virtualenv sudo pip3 install virtualenv # Create a dedicated environment for TF 2.2 GPU virtualenv -p python3.7 tf22-gpu-env # Activate the environment (you'll need to run this every time you work with TF) source tf22-gpu-env/bin/activate
6. Install TensorFlow 2.2 GPU
Now install the exact GPU version of TensorFlow:
pip install tensorflow-gpu==2.2.0
7. Verify GPU Support
Run this quick test to confirm TensorFlow can detect and use your GPU:
python -c " import tensorflow as tf print('TensorFlow Version:', tf.__version__) print('GPU Available:', tf.test.is_gpu_available()) print('Detected GPUs:', tf.config.list_physical_devices('GPU')) "
If everything works, you’ll see GPU Available: True and a list of your GPU devices.
内容的提问来源于stack exchange,提问作者Basj

