TensorFlow is a popular open-source machine-learning library developed by Google. It allows you to create deep learning models with ease and has become a go-to library for data scientists and developers. In this tutorial, we will guide you through the process of how to install TensorFlow on AlmaLinux, a popular CentOS alternative. if your using another Linux distribution Ubuntu check this blog How to Install TensorFlow on Ubuntu
How to Install TensorFlow on AlmaLinux
Prerequisites
Before we begin, make sure you have the following installed on your AlmaLinux system:
- Python 3.6 or higher: TensorFlow requires Python 3.6 or higher to function properly.
- pip: pip is the package installer for Python, and you’ll need it to install TensorFlow.
If you don’t have these installed, follow our guides on how to install Python on AlmaLinux and how to install pip on AlmaLinux.
Update Your System
Before installing TensorFlow, it’s a good idea to update your system. Open a terminal and run the following command:
sudo dnf update
This command updates all installed packages on your system to their latest versions.
Install TensorFlow
Now that your system is up-to-date, you can install TensorFlow. Run the following command in your terminal:
pip install --user tensorflow
This command installs TensorFlow in your user directory, making it available for your user only. If you want to install TensorFlow system-wide, remove the --user
flag:
sudo pip install tensorflow
Verify TensorFlow Installation
To verify that TensorFlow has been installed successfully, open a Python shell and import the TensorFlow library:
import tensorflow as tf
If there are no errors, the installation was successful. You can check the TensorFlow version by running the following command in the Python shell:
print(tf.__version__)
This should output the installed version of TensorFlow.
Test TensorFlow
Let’s test TensorFlow by creating a simple linear regression model. Create a new Python file named linear_regression.py
and add the following code:
import tensorflow as tf
import numpy as np
# Generate random training data
X_train = np.random.rand(100).astype(np.float32)
y_train = 3 * X_train + 2 + np.random.normal(0, 0.1, 100)
# Define the linear regression model
model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=[1])])
# Compile the model
model.compile(optimizer='adam', loss='mse')
# Train the model
model.fit(X_train, y_train, epochs=100)
# Test the model
X_test = np.random.rand(10).astype(np.float32)
y_test = 3 * X_test + 2
y_pred = model.predict(X_test)
print("Predicted values:", y_pred)
print("True values:", y_test)
Save the file and run it using the command:
python linear_regression.py
If TensorFlow is working correctly, you should see the training progress and the predicted values for the test dataset.
Conclusion
Congratulations! You’ve successfully installed TensorFlow on AlmaLinux. You can now start building machine learning models and deploying web applications with TensorFlow. If you’re new to TensorFlow, there are plenty of resources available to help you get started, such as the official TensorFlow tutorials