To get started with TensorFlow, you need to get it up and running on your machine. In this tutorial, you will see how to set up TensorFlow on your machine. It involves a careful step of knowing how to create a virtual environment, activating the virtual environment, and finally installing Tensorflow in the virtual environment. By the end of this tutorial, you will discover how to carry out these procedures on your PC irrespective of the operating system you are using or the Python version you have installed.
Before we go any further, let’s see the requirement for installing TensorFlow on your PC.
Requirements for Installing TensorFlow
The TensorFlow flow library is supported in Python 2.7 and Python 3.3 and above. Tensorflow can be run using your machine’s CPU or GPU. Running Tensorflow on dedicated GPUs have proven to produce a faster result and is recommended for large projects especially projects with image datasets.
If you want to install the GPU version of TensorFlow, Cuda Toolkit 7.5 and cuDNN v5 have proven to work very well. You could use lower versions of Cuda Toolkit and cuDNN as well. Cuda Toolkit greater than version 7.0 and cuDNN greater than version 3 will also work. But using these versions will require you to install from sources. Let’s now how the different methods to install TensorFlow.
Methods for Installing TensorFlow
TensorFlow can be installed using different methods.
- Using pip installation: Using pip to install TensorFlow is great but may most likely upgrade other python packages on your PC. Some of which, may not be compatible with your previously run programs. This may mess up the python programs on your PC.
- Using virtualenv installation: You can install TensorFlow by creating a virtual environment. This involves installing TensorFlow in a new and dedicated directory. This way, the existing python program on your OC would not be affected.
- Using Anaconda installation: If you have Anaconda installed on your PC, you can install TensorFlow in its environment. This also does not affect existing Python programs in your PC
- Using Docker Installation: You can also run TensorFlow in a docker container, which will be separated from the other programs in your machine.
- Installing TensorFlow from sources: You could also install TensorFlow by building a pip which you can install using pip.
In this tutorial, we will be focusing on setting up TensorFlow using Anaconda Installation. Let’s dive right into it.
Installing TensorFlow with Anaconda
With Anaconda, you can very quickly get TensorFlow up and running on your machine, enabling you to kickstart your machine learning and deep learning process. The installation process will be broken into 5 steps. For easy understanding, we will split the installation procedure into two categories: How to install TensorFlow for Windows OS and then for Linux/Mac/Ubuntu OS
For Windows OS
Step 1: Download and install Anaconda on your PC
Since we are using Anaconda to install TensorFlow, you must have Anaconda installed on your PC. You can visit their website and click on the download menu to download the setup file.
After you have successfully downloaded the file, open the .exe file, and click on the Next button until the installation process is complete.
Step 2: Open the Anaconda PowerShell Prompt from the Windows Start menu.
Step 3: The next thing you need to do is to create a TensorFlow environment. The Tensorflow environment is the Python environment where the Python script, TensorFlow library, and its dependencies will be installed You can name it ‘tensorflow’. Some others use ‘tf’ Note that you are at liberty to use any name you desire. Let’s say you wish to name your environment, ‘tensorflow’, go ahead and type this code on your Anaconda PowerShell Prompt to create the environment.
conda create --name tensorflow python=3.7
The python version to use is the python version on your machine. Here, I have a Python 3.7 installed on my machine. Type ‘y’ to begin the download of TensorFlow and the necessary dependencies.
Once the download is complete, it means you have successfully created your TensorFlow environment. The next thing is to activate it.
Step 4: Active your TensorFlow environment.
To activate your newly created TensorFlow environment, type the line of code below.
conda activate tensorflow
Step 5: Install TensorFlow in the activated TensorFlow environment
The final step is to install the TensorFlow environment has been activated, you can now install TensorFlow using conda.
For the CPU version, type…
conda install --upgrade tensorflow
For the GPU, version type…
conda install --upgrade tensorflow-gpu
You can also use pip to install TensorFlow in the created TensorFlow environment. For the CPU version, type
pip install --upgrade tensorflow
For the GPU version, type…
pip install --upgrade tensorflow-gpu
For Ubuntu, Linux or Mac OS
Step 1: Download Anaconda
Visit the Anaconda official website and click on the download menu to download the setup file for your specified OS. You can simply use this link
Step 2: Open the terminal window with the default bash shell.
Step 3: You’d need to create a TensorFlow environment so it does not affect existing python programs on your machine. You may wish to name it ‘tensorflow’, ‘tf’, or any other name you find suitable. Now, go ahead and type this code on your shell. Use any of the lines depending on the version of Python you have installed on your PC
# Python 2.7 $ conda create -n tensorflow python=2.7 # Python 3.4 $ conda create -n tensorflow python=3.4 # Python 3.5 $ conda create -n tensorflow python=3.5 # Python 3.6 $ conda create -n tensorflow python=3.6 # Python 3.7 $ conda create -n tensorflow python=3.7
If you didn’t name your environment ’tensorflow’, make sure to replace the ‘tensorflow’ in the above code with the name of your environment.
Step 4: Activate the TensorFlow environment. You may wish to use the pip package to do this or the conda package.
$ source activate tensorflow (tensorflow)$ # Your prompt should change # Linux/Mac OS X, Python 2.7/3.4/3.5, CPU only: (tensorflow)$ conda install -c conda-forge tensorflow
$ source activate tensorflow (tensorflow)$ # depending on your computer specs and your installation goal, select from any of the lines below. # Ubuntu/Linux 64-bit, CPU only, Python 2.7 (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp27-none-linux_x86_64.whl # Ubuntu/Linux 64-bit, GPU enabled, Python 2.7 # Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp27-none-linux_x86_64.whl # Mac OS X, CPU only, Python 2.7: (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.10.0-py2-none-any.whl # Mac OS X, GPU enabled, Python 2.7: (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-0.10.0-py2-none-any.whl # Ubuntu/Linux 64-bit, CPU only, Python 3.4 (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp34-cp34m-linux_x86_64.whl # Ubuntu/Linux 64-bit, GPU enabled, Python 3.4 # Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp34-cp34m-linux_x86_64.whl # Ubuntu/Linux 64-bit, CPU only, Python 3.5 (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp35-cp35m-linux_x86_64.whl # Ubuntu/Linux 64-bit, GPU enabled, Python 3.5 # Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp35-cp35m-linux_x86_64.whl # Mac OS X, CPU only, Python 3.4 or 3.5: (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.10.0-py3-none-any.whl # Mac OS X, GPU enabled, Python 3.4 or 3.5: (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-0.10.0-py3-none-any.whl
The above code downloads the binary file with which TensorFlow will be upgraded.
Step 5: Install TensorFlow in the TensorFlow environmentNow, we can proceed to the final step of installing TensorFlow. When using pip for the installation, you may want to prevent flags about easy_install by using the –ignore-installed flag. This ignores other packages or their dependencies are already installed on your machine. This way, activating the environment and installing TensorFlow does not delete or overwrite an existing file on your machine.
# Python 2 (tensorflow)$ pip install --ignore-installed --upgrade $TF_BINARY_URL # Python 3 (tensorflow)$ pip3 install --ignore-installed --upgrade $TF_BINARY_URL
And you are good to go! If you have any questions, feel free to drop them in the comment section. I’d try my best to answer them.