How to Install TensorFlow? [Windows, Linux and MacOS]

If you are a beginner and don’t know how to install TensorFlow, I have explained the step-by-step procedure to install TensorFlow for three different platforms: Windows, Linux, and MacOS.

How to Install TensorFlow

To install Tensorflow on your computer or systems. First, ensure you have installed Python version 3.9 to 3.11, pip version 19.0 or higher for Windows and Linux, and 20.3 or higher for macOS.

If you haven’t installed Python, visit this Python download and installation tutorial.

I will show you how to install Tensorflow on Windows, Linux, and MacOS platforms. Let’s start installing Tensorflow.

There are two versions of TensorFlow that you can install on your system: Tensorflow-CPU and Tensorflow-GPU.

Tensorflow-CPU only uses the CPU resources for computation, whereas Tensorflow-GPU uses the GPU resources. Here, I will cover only the Tensorflow-CPU; to know how to install Tensorflow-GPU, follow this link.

How To Install Tensorflow

Install Tensorflow on Windows

To install tensorflow on Windows, open the command prompt and create a virtual environment using the command below.

python -m venv tensorflow_env

After creating the environment ‘tensorflow_env’, activate this environment using the command below.

tensorflow_env\Scripts\activate

After activating the environment, install the latest version of tensorflow using the command below.

pip install tensorflow
Install Tensorflow on Windows

If you see something like this in the above image, tensorflow is downloaded and installed on your system without any error.

To verify the installation of Tensorflow from your Windows, first open the Python shell using the command below.

python

Then, import the TensorFlow library by giving the alias name tf using the command below.

import tensorflow as tf

After that, to check the installed Tensorflow, run the command tf.__version__ as shown in the command below.

print(tf.__version__)
Verifying Installation of Tensorflow on Windows

When you run the command, print(tf.__version__), the version number of TensorFlow is returned. As you can see in the above output, the version is 2.15.0. If you see that version of Tensorflow like that, you have successfully installed Tensorflow on Windows.

READ:  Python Scipy Eigenvalues [7 Useful Examples]

Moving on to the next step, let’s see how to install it on Linux.

Install Tensorflow on Linux

To install on Linux, first, ensure you have installed Python; if not, install it. also, some of the Linux distributions have Python pre-installed. So here, I will use the Ubuntu operation and show you how to install TensorFlow.

Ensure you have installed the latest version of Ubuntu (64-bit) or version 16.04 or higher; these steps can also be used in other distributions of Linux, but Tensorflow officially supports Ubuntu.

Next, before installing, check the version of Python in your terminal using the command below.

python3.11 --version

After that, create a new environment. You need to install the ‘venv’ package, and then you can create an environment; use the command below to install that package.

sudo apt install python3.11-venv
Install Tensorflow on Linux Checkin Python Version and Installing Venv Package

Then, close and open the terminal again. Create a virtual environment ‘tensorflow_env’ using the command below.

python3.11 -m venv tensorflow_env

Now, activate the environment you created using the command below.

source tensorflow_env/bin/activate

After activating environment ‘tensorflow_env’, install the latest version of Tensorflow using the command below.

pip install tensorflow
Installing Tensorflow on Linux (Ubuntu)

To verify the installation of Tensorflow on Linux (Ubuntu), activate the Python shell by typing Python3.11 in your terminal and hitting enter from your keyboard.

python3.11

After entering into the Python shell, import the Tensorflow library using the command below.

import tensorflow as tf

Then check the version of TensorFlow you installed just now; use the command tf.__version__ as shown below.

print(tf__version__)
Checking Version of Installed Tensorflow on Linux (Ubuntu)

As you can see, the latest version of the TensorFlow is 2.15.0. Also it indicates that you have successfully installed Tensorflow on the Linux system (Ubuntu).

READ:  Python program to print the duplicate elements of an array [4 Methods]

Remember you can follow all the above steps for other Linux distros to install Tensorflow. Again, I want to say that before installing TensorFlow, check whether you have installed Python or not, and also check the package that allows you to create an environment for your project, such as Venv.

If you don’t want to install TensorFlow in a specific virtual environment such as tensorflow_env that you have created, then simply after checking the version of Python, run the command pip install tensorflow, and this will install TensorFlow in your current directory.

Finally, let’s look at how to install TensorFlow on MacOS.

Install Tensorflow on MacOS

Again, before installing TensorFlow, ensure you have installed the Python and the virtual environment package to create an environment.

To check the python on the MacOS, run the command below.

python3.11 --version

To install the virtual environment package ‘virtualenv’, use the below command.

brew install virtualenv

Now, create a new environment, ‘tensorflow_env, ‘ using the command below in your terminal.

virtualenv tensorflow_env

Activate the created environment using the command below.

source tensorflow_env/bin/activate

After activating the environment, install the TensorFlow using the command below.

pip install tensorflow

To verify the installation of Python, open the terminal or command prompt, then type the command below to open the Python shell.

python3.11

Then, import the tensorflow library using the command below.

import tensorflow as tf

After checking the version of installed TensorFlow using the function tf.__version__ as shown below.

print(tf.__version__)

If you see the version, it’s something like 2.15.0; you have installed TensorFlow successfully on your macOS system.

READ:  Python Tkinter Scrollbar - How to use

Conclusion

In this tutorial, I have covered how to install TensorFlow on different platforms using the command pip install TensorFlow, especially since you have installed the CPU version of TensorFlow.

Additionally, you learned how to install TensorFlow on a specific platform like MacOS, Windows and Linux (Ubuntu). you also learned how to check the tensorflow version using the tf.__version__ function.

You may like to read: