How to Install TensorFlow on Ubuntu
How to Install TensorFlow on Ubuntu
Prerequisites
An Ubuntu Linux system (we are using Ubuntu 24.04).
Access to a command line/terminal window.
A user account with sudo privileges.
Python 3 installed on the Ubuntu system (we are using Python 3.12).
Step 1: Install Python Development Packages
Before installing TensorFlow, set up the Python development environment. It
includes the following:
Python-dev. Provides header files for building Python extensions.
pip. The Python package manager for installing TensorFlow and other
Python packages.
Miniconda. A minimal installer for Conda. It helps create and manage
environments and simplifies GPU setup. Skip the Miniconda installation if
you already have Anaconda installed or prefer using the built-
in venv or virtualenv.
Follow the steps below to install the required packages:
1. Open the terminal window and update the package list:
sudo apt updateCopy
Wait for apt to finish the update.
2. Install python3-dev and python3-pip with the following command:
sudo apt install python3-dev python3-pipCopy
3. Download the Miniconda shell script using the wget command:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-
x86_64.shCopy
6. Choose the installation location. Press Enter to accept the suggested location.
When asked to proceed, press y and wait for the updates to complete. Otherwise,
the output shows that the latest conda version has been installed already.
2. Create an environment named tf2:
conda create --name tf2 python=3.12Copy
Press y to confirm the environment setup.
The command creates an isolated environment with Python 3.12. If you use a
different Python version, change the number to the version on your system. See
how you can check the Python version.
Step 3: Activate Conda Environment
Activate the environment with:
conda activate tf2Copy
The shell prefix changes to (tf2). Keep the environment active for the rest of the
installation process.
Step 3: Install TensorFlow
The following steps differ depending on whether you install TensorFlow for CPU
or GPU. The choice depends on the workload requirements and available
resources.
Option 1: Install TensorFlow For CPU
The tensorflow-cpu software package is simple to set up for beginners and
supports CPU-only workloads. To install the package, type the following
command:
pip install tensorflow-cpuCopy
The installation downloads and sets up all the required dependencies.
Note: Specify the TensorFlow version to install an older build, for example: pip
install tensorflow-cpu==[version].
Alternatively, install the third-party tensorflow package:
pip install tensorflowCopy
The package is built, maintained, tested, and released by AWS.
Option 2: Install TensorFlow For GPU
If using TensorFlow for GPU-based machine learning workloads, the setup
requires an NVIDIA CUDA-enabled GPU with the correct Nvidia driver
installed (version >= 525.60.13).
Follow the steps below to install TensorFlow for GPU:
1. Update the pip package manager:
pip install --upgrade pipCopy
Conclusion
This guide showed how to install TensorFlow on Ubuntu 24.04. With the desired
TensorFlow version (CPU and GPU support) installed on your system, you can
now move on to developing your machine learning models.