Best Steps To Install TensorFlow on Ubuntu 22.04 – OrcaCore

Posted on

Best Steps To Install TensorFlow on Ubuntu 22.04 - OrcaCore

Best Steps To Install TensorFlow on Ubuntu 22.04 – OrcaCore

In this guide, brought to you by Orcacore, you will learn How To Install TensorFlow on Ubuntu 22.04. TensorFlow empowers developers to construct dataflow graphs—structures that illustrate how data traverses a graph, or a sequence of processing nodes. Each node within the graph signifies a mathematical operation, and each connection or edge linking nodes represents a multidimensional data array, or tensor.

TensorFlow applications are versatile and can be executed on virtually any target environment: a local computer, a cloud-based cluster, iOS and Android devices, CPUs, or GPUs.

To successfully follow this guide, you must be logged into your server as a non-root user with sudo privileges. You can achieve this by referring to our guide on Initial Server Setup with Ubuntu 22.04.

1. Install Python 3 and Pip For TensorFlow Setup

Since we aim to install TensorFlow within a Python virtual environment, we first need to ensure that Python is installed on our server.

Ubuntu 22.04 comes pre-installed with Python 3 by default. To verify its presence, check your Python version using the following command:

python3 --version
**Output**
Python 3.10.4

If Python 3 is not installed on your server, you can install it using the following command:

sudo apt install python3

Next, install pip on Ubuntu 22.04 with the command below:

sudo apt install python3-pip

Confirm the installation by verifying its version:

pip --version
Install Python 3 and Pip For TensorFlow Setup

2. Install Python virtual environment on Ubuntu 22.04

The recommended method for creating virtual environments involves using the venv module, which is part of the python3-venv package.

Begin by updating your local package index with the following command:

sudo apt update

Then, install the Python virtual environment using this command:

sudo apt install python3-venv

3. Create a virtual environment on Ubuntu 22.04

Now, you need to create and activate a Python virtual environment.

First, create a new directory for your TensorFlow project and navigate into it. We’ll name it tf_project in this example.

# mkdir tf_project
# cd tf_project

From within your TensorFlow project directory, execute the following command to create the virtual environment:

python3 -m venv venv

This command generates a directory named venv, containing a copy of the Python binary, the Pip package manager, the standard Python library, and other essential files.

Activate the virtual environment using the command below:

source venv/bin/activate

Upon activation, the virtual environment’s bin directory will be prepended to the system’s $PATH variable. Furthermore, the shell prompt will change to display the name of the currently active virtual environment, which in this case is (venv).

TensorFlow requires pip version 19 or higher. Upgrade pip to the latest version by running the following command:

(venv) $ sudo pip install --upgrade pip
 Create a virtual environment  for Installing TensorFlow

4. TensorFlow Setup with Python Pip

You are now ready to install TensorFlow on your server. Execute the following command:

(venv) $ sudo pip install --upgrade tensorflow

This command will install TensorFlow along with all its dependencies.

Note: Within the virtual environment, you can use pip instead of pip3 and python instead of python3.

Verify the installation using the following command:

(venv) $ python -c 'import tensorflow as tf; print(tf.__version__)'
**Output**
2.11.0

For more in-depth information, visit the TensorFlow Learning Page.

Conclusion

You have successfully learned How To Install TensorFlow on Ubuntu 22.04. TensorFlow on Ubuntu 22.04 is widely used for machine learning, deep learning, and AI model development, offering robust tools for data analysis and neural network training. This guide provides a clear pathway to Install TensorFlow on Ubuntu 22.04.

Hope you enjoyed it. You may also like these articles:

How To Set up PiVPN on Ubuntu 22.04

How To Install Joomla on Ubuntu 22.04

Install Wireshark on Ubuntu 22.04

Alternative Solutions for Installing TensorFlow on Ubuntu 22.04

While the pip method within a virtual environment is the most common and recommended approach, other methods exist to Install TensorFlow on Ubuntu 22.04. Let’s explore two alternative solutions: using Conda and Docker.

1. Installing TensorFlow with Conda

Conda is an open-source package, dependency, and environment management system. It’s particularly useful when dealing with complex dependencies, making it a suitable alternative to pip and venv.

Explanation:

Conda allows you to create isolated environments, similar to venv, but it also manages non-Python dependencies, such as libraries written in C or C++, which are often required by scientific computing packages like TensorFlow. This can simplify the installation process, especially when dealing with hardware acceleration like CUDA for GPU support. By using Conda, the complexities of installing the correct versions of CUDA and cuDNN can be handled much more gracefully.

Steps:

  1. Install Anaconda or Miniconda: If you don’t have Conda installed, you can download and install either Anaconda (which includes a large suite of data science packages) or Miniconda (a minimal installer with only Conda and its dependencies). For this example, we’ll assume you’re using Miniconda. Follow the instructions on the Anaconda website for downloading and installing Miniconda for Linux.

  2. Create a Conda Environment: Open your terminal and create a new Conda environment:

    conda create --name tf_conda python=3.9

    Replace 3.9 with your desired Python version.

  3. Activate the Conda Environment:

    conda activate tf_conda
  4. Install TensorFlow:

    conda install -c conda-forge tensorflow

    The -c conda-forge flag specifies that we want to install TensorFlow from the conda-forge channel, which often provides more up-to-date packages and better dependency resolution.

  5. Verify the Installation:

    python -c 'import tensorflow as tf; print(tf.__version__)'

    This will print the TensorFlow version if the installation was successful.

Code Example (Conda Environment Creation and Activation):

# Create a new Conda environment named 'tf_conda' with Python 3.9
conda create --name tf_conda python=3.9

# Activate the newly created environment
conda activate tf_conda

#Install tensorflow using conda forge
conda install -c conda-forge tensorflow

Advantages of Conda:

  • Handles non-Python dependencies: Simplifies the installation of libraries with external dependencies.
  • Reproducible environments: Easier to create and share environments that are consistent across different machines.
  • Package Management: Manages and resolves package dependencies effectively.

Disadvantages of Conda:

  • Larger footprint: Conda environments can be larger than venv environments due to the inclusion of more packages and dependencies.
  • Learning Curve: Conda has its own set of commands and conventions, which may require a learning curve for users familiar with pip.

2. Installing TensorFlow with Docker

Docker is a platform for developing, shipping, and running applications inside containers. A container is a standardized unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.

Explanation:

Docker provides a fully isolated environment for running TensorFlow. This eliminates dependency conflicts and ensures that your TensorFlow application will run consistently regardless of the underlying operating system. It’s particularly useful for deployment and collaboration, as it allows you to package your entire application and its dependencies into a single, portable container.

Steps:

  1. Install Docker: If you don’t have Docker installed, follow the instructions on the Docker website for installing Docker Desktop on Ubuntu.

  2. Pull the TensorFlow Docker Image: Docker Hub provides pre-built TensorFlow images. Pull the desired image. For example, to pull the latest TensorFlow image with GPU support:

    docker pull tensorflow/tensorflow:latest-gpu

    To pull the latest TensorFlow image with CPU support:

     docker pull tensorflow/tensorflow:latest
  3. Run the Docker Container:

    To run the container with GPU support:

    docker run --gpus all -it tensorflow/tensorflow:latest-gpu bash

    To run the container with CPU support:

    docker run -it tensorflow/tensorflow:latest bash

    The -it flags provide an interactive terminal within the container. --gpus all specifies that the container should have access to all available GPUs.

  4. Verify the Installation (Inside the Container): Once inside the container, you can verify the TensorFlow installation:

    python -c 'import tensorflow as tf; print(tf.__version__)'

Code Example (Docker Pull and Run):

# Pull the latest TensorFlow image with GPU support
docker pull tensorflow/tensorflow:latest-gpu

# Run the Docker container with GPU support, providing an interactive terminal
docker run --gpus all -it tensorflow/tensorflow:latest-gpu bash

Advantages of Docker:

  • Isolation: Provides a completely isolated environment, eliminating dependency conflicts.
  • Reproducibility: Ensures that your application runs consistently across different environments.
  • Deployment: Simplifies deployment by packaging your application and its dependencies into a single container.
  • Portability: Docker containers can be moved between different machines and cloud platforms.

Disadvantages of Docker:

  • Overhead: Docker adds some overhead compared to native installations due to the virtualization layer.
  • Learning Curve: Docker has its own set of concepts and commands, which may require a learning curve.
  • Resource Intensive: Running Docker containers can be resource-intensive, especially when using GPU acceleration.

In summary, while the initial guide focuses on using pip and venv to Install TensorFlow on Ubuntu 22.04, Conda and Docker offer viable alternatives with their own strengths and weaknesses. Conda is excellent for managing complex dependencies, while Docker excels at providing isolated and reproducible environments for deployment. Choosing the right method depends on your specific needs and priorities.

Leave a Reply

Your email address will not be published. Required fields are marked *