Step-by-Step Ubuntu AI Setup: Build and Run Your First Model

This guide walks you through the full process of preparing an Ubuntu 24.04 system, installing Python, Git, and a virtual environment, adding TensorFlow, Keras, and PyTorch, and finally coding, training, and evaluating a simple MNIST classifier to demonstrate a working AI model.

Ubuntu
Ubuntu
Ubuntu
Step-by-Step Ubuntu AI Setup: Build and Run Your First Model

System Requirements and Preparation

According to research, an AI development environment on Ubuntu 24.04 LTS should meet the following minimum specifications:

Operating system: Ubuntu 24.04 LTS or newer

CPU: 64‑bit, at least 2 cores (e.g., Intel Core i5 or AMD Ryzen 5)

Memory: minimum 4 GB (8 GB recommended for intensive models)

Storage: at least 10 GB free (SSD recommended for performance)

GPU: optional, NVIDIA with ≥4 GB VRAM for TensorFlow or PyTorch acceleration

These requirements ensure the system can handle AI tasks, especially GPU‑accelerated deep‑learning workloads.

Setting Up the AI Development Environment

1. Install Python

Python is the most popular language for AI. Ubuntu usually ships with Python 3; verify the version: python3 --version If Python is not installed, update the package list and install it:

sudo apt update
sudo apt install python3

Install pip to manage Python packages:

sudo apt install python3-pip

2. Install Git

Git is used for version control of AI project code. Install it with: sudo apt install git Verify the installation: git --version The command should display a version string such as git version 2.x.x, confirming success.

3. Set Up a Virtual Environment

A virtual environment isolates project dependencies. Install the python3-venv package: sudo apt install python3-venv Create a project directory and activate the environment:

mkdir my_ai_project
cd my_ai_project
python3 -m venv venv
source venv/bin/activate

After activation, the prompt shows (venv), indicating the environment is active.

4. Install AI Libraries

Within the virtual environment, install the core AI libraries:

pip3 install tensorflow
pip3 install keras
pip3 install torch

These libraries—TensorFlow, Keras, and PyTorch—form the backbone of machine‑learning and deep‑learning development. Research shows they install cleanly on Ubuntu and are suitable for beginners.

5. Build and Run the First AI Model

To give readers a hands‑on experience, the article provides a simple TensorFlow/Keras script that classifies the MNIST handwritten‑digit dataset, a classic introductory task.

Create the Python file: sudo nano first_ai_model.py Add the following code:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# Load MNIST dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Preprocess: normalize pixel values
x_train = x_train / 255.0
x_test = x_test / 255.0

# Build neural network model
model = keras.Sequential()
model.add(layers.Flatten(input_shape=(28, 28)))  # flatten 28x28 images
model.add(layers.Dense(128, activation='relu'))  # hidden layer with 128 units
model.add(layers.Dense(10, activation='softmax'))  # output layer for 10 classes

# Compile model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train model for 5 epochs
model.fit(x_train, y_train, epochs=5)

# Evaluate model
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_acc * 100:.2f}%')

Save the file and run it: python3 first_ai_model.py After training, the script prints the test accuracy, typically around 97 %–98 %, confirming that the model works.

Conclusion and Extensions

Congratulations! You have set up an AI development environment on Ubuntu and built a working model. The guide suggests next steps such as exploring convolutional neural networks (CNNs) or enabling GPU acceleration to speed up training. Research indicates that Ubuntu’s open‑source ecosystem provides strong support for AI development, suitable for both beginners and enterprise users.

Summary of Steps

Install Python : sudo apt install python3, verify with python3 --version Install Git : sudo apt install git, verify with git --version Set up virtual environment : install python3-venv, create with python3 -m venv venv, activate with source venv/bin/activate Install AI libraries : pip3 install tensorflow, pip3 install keras, pip3 install torch Build and run first model : create first_ai_model.py with the MNIST example and execute

python3 first_ai_model.py
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TensorFlowAI developmentPyTorchMNISTUbuntu
Ubuntu
Written by

Ubuntu

Focused on Ubuntu/Linux tech sharing, offering the latest news, practical tools, beginner tutorials, and problem solutions. Connecting open-source enthusiasts to build a Linux learning community. Join our QQ group or channel for discussion!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.