An open-source machine learning software library, TensorFlow is used to train neural networks. Expressed in the form of stateful dataflow graphs, each node in the graph represents the operations performed by neural networks on multi-dimensional arrays. These multi-dimensional arrays are commonly known as “tensors,” hence the name TensorFlow.
In this tutorial, you’ll install TensorFlow in a Python virtual environment with virtualenv
. This approach isolates the TensorFlow installation and gets things up and running quickly. Once you complete the installation, you’ll validate your installation by importing Tensorflow to ensure you have no errors.
Before you begin this tutorial, you’ll need the following:
One Ubuntu 20.04 server with at least 4GB of RAM set up by following the Ubuntu 20.04 initial server setup guide, including a sudo non-root user and a firewall.
Python 3.8 or higher and virtualenv
installed. Follow How To Install Python 3 on Ubuntu 20.04 to configure Python and virtualenv
.
In this step, we’ll create a virtual environment in order to install TensorFlow into it without compromising our other programming projects. If you already have a clean programming environment set up, feel free to skip this step.
First, create a project directory. We’ll call it tf-demo
for demonstration purposes, but choose a directory name that is meaningful to you:
- mkdir ~/tf-demo
Navigate to your newly created tf-demo
directory:
- cd ~/tf-demo
Then create a new virtual environment called tensorflow-dev
, for instance. Run the following command to create the environment:
- python3 -m venv tensorflow-dev
This creates a new tensorflow-dev
directory which will contain all of the packages that you install while this environment is activated. It also includes pip
and a standalone version of Python.
Now activate your virtual environment:
- source tensorflow-dev/bin/activate
Once activated, your terminal prompt will reflect that you are in the virtual environment:
(tensorflow-dev)username@hostname:~/tf-demo $
At this point you can install TensorFlow in your virtual environment.
When installing TensorFlow, we want to make sure we are installing and upgrading to the newest version available in PyPi.
Therefore, we’ll be using the following command syntax with pip:
- pip install --upgrade tensorflow
Once you press ENTER
, TensorFlow will install, and you should receive output that indicates that the install along with any dependent packages was successful.
Output...
Successfully installed absl-py-0.7.1 astor-0.7.1 gast-0.2.2 grpcio-1.19.0 h5py-2.9.0 keras-applications-1.0.7 keras-preprocessing-1.0.9 markdown-3.0.1 mock-2.0.0 numpy-1.16.2 pbr-5.1.3 protobuf-3.7.0 setuptools-40.8.0 tensorboard-1.13.1 tensorflow-1.13.1 tensorflow-estimator-1.13.0 termcolor-1.1.0 werkzeug-0.15.0 wheel-0.33.1
...
Successfully installed bleach-1.5.0 enum34-1.1.6 html5lib-0.9999999 markdown-2.6.9 numpy-1.13.3 protobuf-3.5.0.post1 setuptools-38.2.3 six-1.11.0 tensorflow-1.4.0 tensorflow-tensorboard-0.4.0rc3 werkzeug-0.12.2 wheel-0.30.0
You can deactivate your virtual environment at any time by using the following command:
- deactivate
To reactivate the environment later, navigate to your project directory and run source tensorflow-dev/bin/activate
.
Now that you have installed TensorFlow, let’s make sure the TensorFlow installation works.
To validate the installation of TensorFlow, we are going to ensure that we can import the TensorFlow package.
- python
The following prompt will appear on your terminal:
>>>
This is the prompt for the Python interpreter, and it indicates that it’s ready for you to start entering some Python statements.
First, type this line to import the TensorFlow package and make it available as the local variable tf
. Press ENTER
after typing in the line of code:
- import tensorflow as tf
As long as you have received no errors, you have installed TensorFlow successfully. If you have received an error, you should ensure that your server is powerful enough to handle TensorFlow. You may need to resize your server, making sure it has at least 4GB of memory.
In this tutorial, you have installed TensorFlow in a Python virtual environment and validated that TensorFlow works by importing it.
TensorFlow’s programmer’s guide provides a useful resource and reference for TensorFlow development. You can also explore Kaggle, a competitive environment for practical application of machine learning concepts that pit you against other machine learning, data science, and statistics enthusiasts.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
if get an error after typing in “import tensorflow as tf”. Unable to figure it out
">>> import tensorflow as tf 2024-01-14 19:30:18.202052: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-01-14 19:30:18.202267: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-01-14 19:30:18.204603: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered 2024-01-14 19:30:18.214772: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. 2024-01-14 19:30:19.617811: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT "
preliminary step is to install latest nvidia drivers and cuda which happens at https://developer.nvidia.com/cuda-downloads
interestingly if you install from this download it will give you matching nvidia drivers, namely when you choose cuda 11.6 it install that and nvidia driver 510 which is super … thanks for this tutorial