Introduction
With the growing popularity of building AI-based tools among developers, Python has emerged as one of the best programming languages for AI due to its simplicity, readability, and extensive libraries like TensorFlow, PyTorch, and scikit-learn. These libraries provide powerful tools for machine learning, data analysis, and neural networks, making Python the best choice for AI and machine learning projects.
Given Python's central role in AI, it's important to learn how to run Python scripts effectively. This tutorial is designed to help you build a foundation for more advanced AI programming by running simple Python scripts on an Ubuntu machine.
Prerequisites
- A server running Ubuntu with a non-root user with sudo privileges and an enabled firewall. For instructions on how to set this up, please select your distribution from this list and follow our Getting Started with a Server guide. Please make sure you are running a supported version of Ubuntu.
- Introduction to the Linux command line.
- Before you begin, run sudo apt-get update in the Ubuntu terminal to ensure that your system has the latest versions and security updates for the software available from the repositories configured on your system.
These instructions are valid for the latest Ubuntu releases: Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04. If you are using Ubuntu version <= 18.04, we recommend upgrading to the latest version as Ubuntu no longer supports these versions. This set of instructions will help you upgrade your Ubuntu version.
Step 1 – Setting up the Python environment
Ubuntu 24.04 comes with Python 3 by default. Open a terminal and run the following command to double-check that Python 3 is installed:
python3 --version
If Python 3 is already installed on your machine, this command will return the current version of Python 3. If it is not installed, you can run the following command and get the Python 3 installation:
sudo apt install python3
Next, you need to install the pip package installer on your system:
sudo apt install python3-pip
Step 2 – Create a Python script
The next step is to write the Python code you want to run. To create a new script, go to the directory of your choice:
cd ~/path-to-your-script-directory
Once you are in the directory, you need to create a new file. In the terminal, run the following command:
nano demo_ai.py
This will open a blank text editor. Write your logic here or copy the code below:
from sklearn.tree import DecisionTreeClassifier
import numpy as np
import random
# Generate sample data
x = np.array([[i] for i in range(1, 21)]) # Numbers 1 to 20
y = np.array([i % 2 for i in range(1, 21)]) # 0 for even, 1 for odd
# Create and train the model
model = DecisionTreeClassifier()
model.fit(x, y)
# Function to predict if a number is odd or even
def predict_odd_even(number):
prediction = model.predict([[number]])
return "Odd" if prediction[0] == 1 else "Even"
if __name__ == "__main__":
num = random.randint(0, 20)
result = predict_odd_even(num)
print(f"The number {num} is an {result} number.")This script creates a simple decision tree classifier using the scikit-learn library. It trains the model to distinguish between odd and even numbers based on randomly generated sample data. It then makes a prediction for the given number based on its learning.
Save and exit the text editor.
Step 3 – Install the required packages
In this step, you install the packages you used in the script above.
The first package you need to install is NumPy. You used this library to create a dataset to train the machine learning model.
Starting with Python 3.11 and Pip 22.3, there is a new PEP 668 that states the marking of Python base environments as “externally managed.” This is why simply running pip3 scikit-learn numpy or similar numpy installation commands will throw an error: Externally managed environment.
To successfully install and use numpy, you need to create a virtual environment that isolates your Python packages from the system environment. This is important because it keeps dependencies required by different projects separate and prevents potential conflicts between package versions.
First, create the virtualenv by running:
sudo apt install python3-venv
Now use this tool to create a virtual environment in your working directory.
python3 -m venv python-envThe next step is to activate this virtual environment by running the activation script.
source python-env/bin/activate
When running, you will notice a terminal prompt that the prefix of your virtual environment name is as follows:
Output
(python-env) ubuntu@user:Now install the required packages by running:
pip install scikit-learn numpy
The random module is part of the Python standard library, so you don't need to install it separately. It comes with Python and can be used directly without any additional installation.
Step 4 – Run the Python script
Now that you have all the required packages, you can run your Python script by running the following command in your working directory:
python3 demo_ai.py
After successful execution, you will see the desired output.
Output
(python-env) ubuntu@user:~/scripts/python demo_ai.py
The number 5 is an Odd number.
(python-env) ubuntu@user:~/scripts/python demo_ai.py
The number 17 is an Odd number.Step 5 [Optional] – Make the script executable
Script execution allows you to run it directly without having to explicitly call Python by typing python3. This makes your script run faster and more conveniently.
Open your Python script using a text editor.
nano demo_ai.py
At the top of the file, add a shebang, #!, which tells the system what interpreter to use when executing the script. Add the following line before your code:
#!/usr/bin/env python3
Save and close the file.
Now, make this script executable so that it can be run like any other program or command in your terminal.
chmod +x demo_ai.py
Upon successful execution, you will immediately see the control. From now on, you can run your script as follows:
./demo_ai.py
Result
Running Python scripts on an Ubuntu machine is a simple process. By understanding how to run Python scripts, you can begin to explore the powerful tools that Python offers, including those essential for artificial intelligence development.









