Tensorflow, an open-source software library, has become an indispensable tool for implementing machine learning, artificial intelligence methodologies, and deep neural networks. It allows developers to create and deploy robust deep learning models like Stable Diffusion and Large Language Models (LLMs). This article provides a detailed guide to installing Tensorflow in Ubuntu 22.04, catering to both GPU and CPU-only system configurations.
Prerequisites
Before diving into the installation process, ensure the following prerequisites are met:
- Deployment of a fresh Ubuntu 22.04 Server on Shape.host.
- Access the server as a non-root user with sudo privileges via SSH.
- Installation of the latest Python3 version with the command:
$ sudo apt install python3
.
Tensorflow Installation on a GPU System
For those operating a GPU system, the installation process for Tensorflow varies depending on whether you choose Python package or Conda environment for the installation.
Installing Tensorflow using Pip
The recommended method of installing Tensorflow on a GPU system is through Pip. Ensure that the required CUDA and CUDNN versions are installed to activate the software package on your GPU server.
For instance, for Tensorflow version 2.13, the required packages are:
- CUDA Toolkit: 11.8
- cuDNN: 8.6.0
Once the necessary packages are installed, update the pip Python package manager using the command: $ pip install --upgrade pip
.
Install Tensorflow with the command: $ python3 -m pip install tensorflow==2.13.*
.
Installing Tensorflow using Conda
Conda, an environment package manager, conveniently installs applications alongside necessary dependencies. To install Tensorflow, select a source Conda channel and install the latest tensorflow-gpu version.
To activate your Conda environment, use the command: $ conda activate env1
.
Install the Tensorflow GPU package with the command: $ conda install tensorflow-gpu=2.12.1 -c conda-forge -y
.
This command installs Tensorflow version 2.12.1 from the Conda-forge channel along with CUDA Toolkit 11.8 and cuDNN 8.8.
Tensorflow requires CUDA and cuDNN packages for GPU compatibility. Although the Conda installation process installs all necessary dependencies, they are not fully-fledged packages. For example, the cudatoolkit package, installed by Conda as a Tensorflow dependency, doesn’t allow the use of the nvcc compiler. If your use case requires these packages, install CUDA Toolkit and cuDNN within your Conda environment.
Tensorflow Installation on a CPU-Only System
If your server doesn’t have an attached GPU interface, install Tensorflow using the non-GPU package.
Installing Tensorflow using Pip
To install the latest Tensorflow package on a CPU-only system using Pip, use the command: $ python3 -m pip install tensorflow==2.13.*
.
Installing Tensorflow using Conda
To install Tensorflow on a CPU-only system using Conda, run the following command in your environment: $ conda install tensorflow=2.12.0 -y
.
This command installs the CPU-only tensorflow package without any GPU dependent packages.
Testing the Installation
To verify the successful installation of Tensorflow, perform sample computation tasks using Python.
Testing on a GPU System
Import the Tensorflow package and print the list of available GPU devices using the command: $ python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
.
To verify that Tensorflow can perform a tensor-based operation using random numbers, use the command: $ python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([100, 100])))"
.
Testing on a CPU System
Verify that Tensorflow can perform a basic tensor-based computation using the command: $ python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([100, 100])))"
.
This command executes a tensor-based operation using random numbers.
Conclusion
This article has guided you through installing Tensorflow on an Ubuntu 22.04 server, exploring both GPU-based and CPU-only systems. You can further explore these resources for more information:
In the ever-evolving world of technology, tools like Tensorflow have become essential. Equipped with this knowledge, you can now leverage the power of Tensorflow on your Ubuntu 22.04 server. For reliable, efficient, and secure cloud hosting solutions, consider Shape.host’s Linux SSD VPS services to power your innovations.