Prerequisites and Overview
This guide covers the installation of TensorFlow versions 1.15 and 2.16.1 using the conda package manager. The procedures are consistent across Windows 10 and Ubuntu 22.04 LTS. It is updated as of March 2024. CPU and GPU configurations are detailed.
System and Software Requirements
1. Conda Installation
Install Anaconda or Miniconda. Anaconda is recommended for begniners as it includes Jupyter Notebook.
2. GPU-Specific Requirements (For TensorFlow with GPU Support)
For leveraging GPU acceleration, compatible versions of CUDA and cuDNN are necessary. It is advised to install these both system-wide and within the conda environment.
Recommended Versions: CUDA 11.8, cuDNN 8.6.
Installing TensorFlow 1.15 (CPU Version)
TensorFlow 1.x is largely deprecated. Version 1.15 is the final release and is provided here for legacy compatibility. GPU support for TF1 requires older CUDA versions and is not covered.
Step 1: Create a Python 3.6 Environment
conda create --name tf1_env python=3.6
Step 2: Activate the Environment
conda activate tf1_env
Step 3: Install TensorFlow 1.15
pip install tensorflow==1.15.0 -i https://pypi.tuna.tsinghua.edu.cn/simple/
Step 4: Configure Jupyter Notebook Kernel
pip install ipykernel -i https://pypi.tuna.tsinghua.edu.cn/simple/
python -m ipykernel install --user --name tf1_env --display-name "Python (TF1.15)"
Step 5: Verification
Launch Jupyter Notebook, create a new notebook with the 'Python (TF1.15)' kernel, and run:
import tensorflow as tf
print("TensorFlow Version:", tf.__version__)
Installing TensorFlow 2.16.1 (CPU Version)
TensorFlow 2.16.1 is used for demonstration. Refer to the official compatibility table for supported Python versions.
Step 1: Create a Python 3.10 Environment
conda create --name tf2_cpu python=3.10
conda activate tf2_cpu
Step 2: Install TensorFlow for CPU
pip install tensorflow-cpu==2.16.1 -i https://pypi.tuna.tsinghua.edu.cn/simple/
Step 3: Configure Jupyter Notebook Kernel
pip install ipykernel -i https://pypi.tuna.tsinghua.edu.cn/simple/
python -m ipykernel install --user --name tf2_cpu --display-name "Python (TF2 CPU)"
Step 4: Verification
import tensorflow as tf
print("TensorFlow Version:", tf.__version__)
print("Available Devices:", tf.config.list_logical_devices())
Installing TensorFlow 2.16.1 (GPU Version - Linux)
Note: Starting from TensorFlow 2.12, the separate tensorflow-gpu package was deprecated. GPU support is now included in the main package. Windows installation for newer versions may require WSL and is more complex; using an older version (pre-2.12) on native Windows is simpler.
Step 1: Create a Python 3.10 Environment
conda create --name tf2_gpu python=3.10
conda activate tf2_gpu
Step 2: Install TensorFlow with CUDA Extras
python -m pip install tensorflow[and-cuda] -i https://pypi.tuna.tsinghua.edu.cn/simple/
Step 3: Configure Jupyter Notebook Kernel
pip install ipykernel -i https://pypi.tuna.tsinghua.edu.cn/simple/
python -m ipykernel install --user --name tf2_gpu --display-name "Python (TF2 GPU)"
Step 4: Enstall CUDA Toolkit and cuDNN within Conda Environment (Optional)
This step is optional if compatible versions are already installed system-wide. To install within the conda environment:
conda config --add channels conda-forge
conda install cudatoolkit=12.3
pip install nvidia-cudnn-cu12==8.9.6.50 -i https://pypi.tuna.tsinghua.edu.cn/simple/
To search for available CUDA toolkit versions in conda channels:
conda search cudatoolkit
Step 5: Verification
import tensorflow as tf
gpu_devices = tf.config.list_physical_devices('GPU')
print("GPU Devices:", gpu_devices)
Troubleshooting Common Issues
1. NUMA Node Read Error
Error: successful NUMA node read from SysFS had negative value (-1)...
Solution: Identify your NVIDIA GPU and set its NUMA node.
lspci | grep -i nvidia
# Note the device ID (e.g., 0000:21:00.0)
cd /sys/bus/pci/devices/ && ls
# Find the corresponding directory
echo 0 | sudo tee -a /sys/bus/pci/devices/0000:21:00.0/numa_node
2. "Cannot dlopen some GPU libraries" Error
This typically indicates a cuDNN installation issue. Ensure cuDNN is installed correctly within the conda environment as shown in Step 4 of the GPU installation section.
3. Automating Library Path Configuration for GPU
To automatically set necessary environment variables upon activating the conda environment, create activation scripts.
mkdir -p $CONDA_PREFIX/etc/conda/activate.d
echo 'CUDNN_PATH=$(dirname $(python -c "import nvidia.cudnn;print(nvidia.cudnn.__file__)"))' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
echo 'export LD_LIBRARY_PATH=$CONDA_PREFIX/lib/:$CUDNN_PATH/lib:$LD_LIBRARY_PATH' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
source $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
4. "TF-TRT Warning: Could not find TensorRT"
If you require TensorRT optimizations, install it separately.
pip install tensorrt
5. CPU Optimization with AVX2
TensorFlow may log a message suggesting enabling AVX2 for CPU performence improvements. This requires CPU support and enabling the feature in the system BIOS. Note that enabling AVX2 can increase power consumption.