How To Install TensorFlow on Manjaro
In this tutorial, we will show you how to install TensorFlow on Manjaro. TensorFlow, an open-source machine learning framework developed by Google, has revolutionized the field of artificial intelligence (AI). Its versatility and robustness make it a popular choice among researchers and developers alike. Manjaro, a user-friendly and powerful Linux distribution based on Arch Linux, provides an ideal environment for running TensorFlow.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo’ to the commands to get root privileges. I will show you the step-by-step installation of TensorFlow on a Manjaro Linux.
Prerequisites
- A server or desktop running one of the following operating systems: Manjaro, and other Arch-based distributions.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A stable internet connection is crucial for downloading and installing packages. Verify your connection before proceeding.
- Access to a Manjaro Linux system with a non-root sudo user or root user.
Install TensorFlow on Manjaro
Step 1. Update Your System.
Before installing any new software, it’s a good practice to update your package database. This ensures that you’re installing the latest version of the software and that all dependencies are up to date.
To update the package database, run the following command in the terminal:
sudo pacman -Syu
Step 2. Installing Necessary Development Tools.
TensorFlow requires certain development tools and libraries to be installed on your system. Install the GCC compiler and Python packages by running:
sudo pacman -S base-devel python python-pip
Step 3. Setting Up a Virtual Environment.
Using a virtual environment helps keep your TensorFlow installation isolated from other Python packages. To create a virtual environment, follow these steps:
Install the virtualenv package:
sudo pip install virtualenv
Create a new virtual environment:
virtualenv --system-site-packages -p python3 ~/tensorflow-env
Activate the virtual environment:
source ~/tensorflow-env/bin/activate
Step 4. Installing CUDA and cuDNN (for GPU Support)
If you have a compatible NVIDIA GPU and want to leverage its power for accelerated TensorFlow computations, you need to install CUDA and cuDNN.
- Installing CUDA Toolkit
First, add the CUDA repository to your system:
sudo pacman -S cuda
Install the CUDA packages:
sudo pacman -S cuda-toolkit
- Installing cuDNN
Download the cuDNN library from the NVIDIA website (registration required).
Extract the downloaded archive and copy the files to the CUDA directory:
sudo cp -P cuda/include/cudnn*.h /opt/cuda/include sudo cp -P cuda/lib64/libcudnn* /opt/cuda/lib64 sudo chmod a+r /opt/cuda/include/cudnn*.h /opt/cuda/lib64/libcudnn*
Step 5. Installing TensorFlow.
With the prerequisites in place, you can now install TensorFlow on your Manjaro system.
For a CPU-only version of TensorFlow, run:
pip install tensorflow
If you have a GPU and have installed CUDA and cuDNN, install the GPU-enabled version:
pip install tensorflow-gpu
To verify that TensorFlow is installed correctly, open a Python shell and run:
import tensorflow as tf print(tf.__version__)
If TensorFlow is installed properly, it will print the version number without any errors.
Step 6. Configuring TensorFlow.
To ensure that TensorFlow can find the necessary libraries and tools, set up the following environment variables in your ~/.bashrc
file:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/cuda/lib64 export PATH=$PATH:/opt/cuda/bin
Step 7. Testing Your TensorFlow Installation.
Create a new Python file named test_tensorflow.py
with the following content:
import tensorflow as tf mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10) ]) predictions = model(x_train[:1]).numpy() tf.nn.softmax(predictions).numpy() loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) loss_fn(y_train[:1], predictions).numpy() model.compile(optimizer='adam', loss=loss_fn, metrics=['accuracy']) model.fit(x_train, y_train, epochs=5) model.evaluate(x_test, y_test, verbose=2)
Run the script using:
python test_tensorflow.py
If TensorFlow is installed correctly, the script will train a simple model on the MNIST dataset and display the training progress and evaluation results.
Congratulations! You have successfully installed TensorFlow. Thanks for using this tutorial to install the latest version of TensorFlow on the Manjaro system. For additional help or useful information, we recommend you check the official TensorFlow website.