TensorFlow is a powerful open-source machine learning framework developed by Google. It allows me to build and train deep learning models efficiently. In this guide, I will walk through the steps to install TensorFlow on Ubuntu 24.04, including setting up a virtual environment and enabling GPU support if needed.
How to Install TensorFlow on Ubuntu 24.04
Before installing TensorFlow, I need to update my system packages to ensure compatibility.
sudo apt update && sudo apt upgrade -y
This ensures that all installed packages are up to date.
TensorFlow requires Python 3.6 or later. Ubuntu 24.04 comes with Python 3, but I need to make sure it's installed along with pip (Python's package manager).
sudo apt install python3 python3-pip -y
To verify the installation, I can check the Python version:
python3 --version
Using a virtual environment helps me manage dependencies and avoid conflicts with system-wide packages.
Install the virtual environment package:
sudo apt install python3-venv -y
Create a virtual environment named tensorflow_env
:
python3 -m venv tensorflow_env
Activate the virtual environment:
source tensorflow_env/bin/activate
Once activated, the terminal prompt will change, indicating that I'm working within the virtual environment.
To prevent issues during installation, I should upgrade the pip to the latest version:
pip install --upgrade pip
With the virtual environment active, I can now install TensorFlow using pip:
pip install tensorflow
To verify the installation, I can run the following command:
python -c "import tensorflow as tf; print(tf.__version__)"
If TensorFlow is installed correctly, this command will display the installed version number.
I need to install additional software if I have an NVIDIA GPU and want to accelerate TensorFlow computations.
Check my GPU model:
lspci | grep -i nvidia
Install NVIDIA drivers:
sudo apt install nvidia-driver-535 -y
Install CUDA Toolkit:
sudo apt install nvidia-cuda-toolkit -y
Download and install the cuDNN library from the NVIDIA website and follow the installation instructions.
Once my system is set up with CUDA and cuDNN, I can install the GPU-enabled version of TensorFlow:
pip install tensorflow-gpu
To verify GPU support, I can run:
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
If TensorFlow detects my GPU, it will be listed in the output.
You might also like: