TensorFlow is one of the most popular and influential open-source libraries for machine learning and deep learning, developed by Google. It provides a flexible ecosystem of tools, libraries, and community resources, allowing developers to build and deploy machine learning models quickly and efficiently. TensorFlow supports a wide range of applications, from simple linear regression models to complex neural networks for natural language processing, computer vision, etc. Its versatility makes it a top choice for beginners exploring artificial intelligence and professionals building large-scale AI, machine learning, or deep learning solutions.
Running TensorFlow on Ubuntu is popular among developers because it ensures a robust and stable development environment. This guide simplifies the installation process, with key steps to start with TensorFlow and create a TensorFlow project environment on Ubuntu.
Prerequisites
- A Virtual Machine (such as the ones provided by NodeShift) with at least:
- 1 vCPUs
- 4 GB RAM
- 10 GB SSD
- Ubuntu 22.04 VM
Note: The prerequisites for this are highly variable across use cases. A high-end configuration could be used for a large-scale deployment.
Step-by-step process to install TensorFlow on Ubuntu
For this tutorial, we’ll use a CPU-powered Virtual Machine by NodeShift, which provides high-compute Virtual Machines at a very affordable cost on a scale that meets GDPR, SOC2, and ISO27001 requirements. It also offers an intuitive and user-friendly interface, making it easier for beginners to get started with Cloud deployments. However, feel free to use any cloud provider you choose and follow the same steps for the rest of the tutorial.
Step 1: Setting up a NodeShift Account
Visit app.nodeshift.com and create an account by filling in basic details, or continue signing up with your Google/GitHub account.
If you already have an account, login straight to your dashboard.
Step 2: Create a Compute Node (CPU Virtual Machine)
After accessing your account, you should see a dashboard (see image), now:
- Navigate to the menu on the left side.
- Click on the Compute Nodes option.
- Click on Start to start creating your very first compute node.
These Compute nodes are CPU-powered virtual machines by NodeShift. These nodes are highly customizable and let you control different environmental configurations, such as vCPUs, RAM, and storage, according to your needs.
Step 3: Select configuration for VM
- The first option you see is the Reliability dropdown. This option lets you choose the uptime guarantee level you seek for your VM (e.g., 99.9%).
- Next, select a geographical region from the Region dropdown where you want to launch your VM (e.g., United States).
- Most importantly, select the correct specifications for your VM according to your workload requirements by sliding the bars for each option.
Step 4: Choose VM Configuration and Image
- After selecting your required configuration options, you’ll see the available VMs in your region and as per (or very close to) your configuration. In our case, we’ll choose a ‘2vCPUs/4GB RAM/80GB SSD’ Compute node.
- Next, you’ll need to choose an image for your Virtual Machine. For the scope of this tutorial, we’ll select Ubuntu.
Step 5: Choose the Billing cycle and Authentication Method
- Two billing cycle options are available: Hourly, ideal for short-term usage, offering pay-as-you-go flexibility, and Monthly for long-term projects with a consistent usage rate and potentially lower cost.
- Next, you’ll need to select an authentication method. Two methods are available: Password and SSH Key. We recommend using SSH keys, as they are a more secure option. To create one, head over to our official documentation.
Step 6: Finalize Details and Create Deployment
Finally, you can also add a VPC (Virtual Private Cloud), which provides an isolated section to launch your cloud resources (Virtual machine, storage, etc.) in a secure, private environment. We’re keeping this option as the default for now, but feel free to create a VPC according to your needs.
Also, you can deploy multiple nodes at once using the Quantity option.
That’s it! You are now ready to deploy the node. Finalize the configuration summary; if it looks good, go ahead and click Create to deploy the node.
Step 7: Connect to active Compute Node using SSH
As soon as you create the node, it will be deployed in a few seconds or a minute. Once deployed, you will see a status Running in green, meaning that our Compute node is ready to use!
Once your node shows this status, follow the below steps to connect to the running VM via SSH:
- Open your terminal and run the below SSH command:
(replace root
with your username and paste the IP of your VM in place of ip
after copying it from the dashboard)
ssh root@ip
2. In some cases, your terminal may take your consent before connecting. Enter ‘yes’.
3. A prompt will request a password. Type the SSH password, and you should be connected.
Output:
Step 8: Install Python3 libraries
- First, update the Ubuntu package source-list to the latest versions.
apt update
Output:
2. Confirm if python3
is installed in the system.
python3 -V
Output:
3. Install Python virtual environment.
To separate your TensorFlow workspace from your working directory, it is recommended to install and create a Python virtual environment (venv
).
apt install python3-venv python3-dev -y
Output:
Step 9: Create a project environment
The next step is to create a separate project directory to set up a Python virtual environment where you can store all your TensorFlow files and work on a project that requires TensorFlow.
- Create a new project directory.
mkdir tf_project
2. Move inside the project.
cd tf_project
3. Create a python virtual environment.
(replace <VENV_NAME>
with the name of your environment)
python3 -m venv <VENV_NAME>
4. Activate the virtual environment.
source <VENV_NAME>/bin/activate
Output:
Whenever you want to work in the virtual environment, you must activate it using the above command. Once your work is done, you can exit and return to your local environment.
Step 10: Install TensorFlow
- Finally, run the below command to install TensorFlow in the project environment.
pip3 install --upgrade TensorFlow
Output:
2. Confirm the installation.
python3 -m pip show TensorFlow
Output:
As you may see, TensorFlow is successfully installed. Just a side note, TensorFlow will only be accessible inside this particular virtual environment, to make it globally accessible, you’ll need to install it in local working directory.
In case you want to exit the environment, run “deactivate
“.
Output:
Conclusion
TensorFlow’s flexibility and power make it an essential tool for developers working on machine learning and AI projects. Setting it up on Ubuntu can be a solid setup for experimentation and deployment, and following the steps in this guide will help you do this quickly. You can deploy TensorFlow on both CPUs and GPUs, and NodeShift affordably provides both of these powerful computing units with an intuitive interface so you can quickly deploy the VM and start your development right away.