How to install TensorFlow 2 in Anaconda?
TensorFlow has been a very hot framework in the past few years. With its latest version out, you must be wondering how you can install TensorFlow 2 on your machine and get it running.
In this tutorial, we’ll show you how to create a new environment, install TensorFlow, upgrade to its latest version and then add a new kernel to Jupyter.
Why Anaconda to Install TensorFlow 2?
The good thing about Anaconda is that NumPy and pandas, along with many other libraries, come automatically with it. That’s a strong plus because there’s no need to install the main packages separately as with some other software for programming in Python.
If you don’t have Anaconda installed, you can refer to our tutorial on installing the Anaconda package.
How to check our current environments?
First, please open Anaconda Prompt. Most probably, the easiest way to reach it is by searching for it in your Start menu.
Type ‘conda info –envs’ like this and you will get a list of all the environments you have created before:
As you can see, we have 4 different environments – the base one, Python 2, Python 3, and Python 3.7 with TF2 installed on it. If you have never created an environment before, you should only have the base environment.
But that should not concern you – after all, that’s why we are here.
How to create a new environment in Anaconda?
We have to write: “conda create –name”. Then we must include the name of the environment we want to create.
You can use Anaconda not only for Python but also for other programming languages. So, it’s always advisable to include ‘Python’ or simply ‘Py’ in the name. Once you’ve done that, you can add the version of Python. In our case, that would be Python 3.
A good idea is to add any other clarification you see fit when you have more than one environment with Python 3 installed. We’ll add: ‘TF2.0’ so that we know that TensorFlow 2 is installed there. Now, all of our widely used packages, including TensorFlow 1, are in the ‘python3’ environment. However, we’ll be using this ‘py3-TF2.0’ environment only when we need TensorFlow 2, so we will include that in the name.
Finally, we must finish the line with the language we want installed and its version, so: ‘python=3’.
We get the package specifications and all that’s about to be installed.
Our new environment has been created!
Note that this process may take a couple of minutes.
How do we activate an environment?
Time to enter or activate the environment, so we can manage it. So, we should write:
‘conda activate’ and the name of the environment. So, in our case, ‘conda activate py3-TF2.0’.
At the beginning of the line, we see an indication that we are in fact in the new environment. Note that it is completely empty – the only packages it contains are the default ones that come with Anaconda. Everything you’ve installed before won’t be included.
And now we are ready to install TensorFlow.
We can simply type:
Now, to ensure there we’ve got the latest version of TensorFlow installed, we highly recommend that you also upgrade the currently installed TensorFlow version. The proper command is:
pip install –upgrade tensorflow
Finally, we must make sure we see the kernel in Jupyter once we start it. There are different ways to go about that but the easiest one is to go back to your base environment and install two packages: “nb_conda_kernels” and “ipykernel”.
To go back to the base environment we must deactivate the current environment by writing: ‘conda deactivate py3-TF2.0’
Now you will be in the base environment.
To install ‘ipykernel’, please write: ‘pip install ipykernel’
Then to install ‘nb_conda_kernels’, please write: ‘conda install nb_conda_kernels’
These two lines should be sufficient for you to see your newly installed kernel in Jupyter.
To make sure everything is working, open Jupyter, select ‘Kernel -> Change kernel’ from the ribbon and choose our preferred kernel.
We prefer the newly created kernel: Py3-TF2.0. Once you choose it you will be all set!
And there you have it. Enjoy deep learning in TensorFlow 2!
Ready to take the next step towards a career in data science?
Check out the complete Data Science Program today. Start with the fundamentals with our Statistics, Maths, and Excel courses. Build up step-by-step practical experience with SQL, Python, R, and Tableau… And develop in-demand competencies with Machine Learning, Deep Learning in TensorFlow 2, Credit Risk Modeling, Time Series Analysis, and Customer Analytics in Python. If you still aren’t sure you want to turn your interest in data science into a full-scale career, we also offer a free preview version of the Data Science Program. You’ll receive 12 hours of beginner to advanced content for free. It’s a great way to see if the program is right for you.