I already installed GPU TensorFlow from source for Python 2 (see this post), and now I would like to also install GPU TensorFlow for Python 3 on the same machine using Virtualenv.
Virtualenv is a tool to keep the dependencies required by different Python projects in separate places. The Virtualenv installation of TensorFlow will not override pre-existing version of the Python packages needed by TensorFlow. See here for a detailed introduction of how virtualenv works and some basic usage.
With Virtualenv the installation is as follows:
- Install pip and Virtualenv:
$ sudo apt-get update
$ sudo apt-get install python-pip python-dev python-virtualenv
- Create a Virtualenv environment in the directory for python 3
~/tensorflow-venv3
:
$ virtualenv --system-site-packages -p python3
~/tensorflow-venv3
#for python 2
$ virtualenv --system-site-packages -p python ~/tensorflow-venv
The --system-site-packages
Option
If you build with virtualenv --system-site-packages ENV
, your virtual environment will inherit packages from /usr/lib/python2.7/site-packages
(or wherever your global site-packages directory is).
This can be used if you have control over the global site-packages directory, and you want to depend on the packages there. If you want isolation from the global system, do not use this flag.
- Activate the virtual environment:
$ source ~/tensorflow-venv3/bin/activate # If using bash
(tensorflow-venv3)$ # Your prompt should change
- Install TensorFlow in the virtualenv for python 3:
Now, install TensorFlow just as you would for a regular Pip installation. First select the correct binary to install (from this page):
# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
(tensorflow-venv3)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.1-cp35-cp35m-linux_x86_64.whl
# Python 2
(tensorflow-venv2)$ pip install --upgrade $TF_BINARY_URL
# Python 3
(tensorflow-venv3)$ pip3 install --upgrade $TF_BINARY_URL
Or you can choose the .whl file you built from source by yourself. Like the one I built in the post GPU tensorflow installation from source
# Python 2
pip install /path to/the .whl file you built from source/tensorflow-0.12.1-cp27-cp27mu-linux_x86_64.whl # Python 3
pip3 install /path to/the .whl file you built from source/tensorflow-0.12.1-cp27-cp27mu-linux_x86_64.whl
Note that when I used the .whl file I built to intall tensorflow into the virtualenv, I met this error. So I ended up installing the binary file from this page.
pip3 install ~/tensorflow_pkg/tensorflow-0.12.1-cp27-cp27mu-linux_x86_64.whl
tensorflow-0.12.1-cp27-cp27mu-linux_x86_64.whl is not a supported wheel on this platform.
# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
(tensorflow-venv3)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.1-cp35-cp35m-linux_x86_64.whl
- After the install you will activate the Virtualenv environment each time you want to use TensorFlow.
- With the Virtualenv environment activated, you can now test your TensorFlow installation.
In your virtualenv, open a python session and type import tensorflow as tf
.
If all went well, you should see the following output:
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so locally
>>>
To use TensorFlow later you will have to activate the Virtualenv environment again:
$ source ~/tensorflow-venv3/bin/activate # If using bash.
(tensorflow-venv3)$ # Your prompt should change.
# Run Python programs that use TensorFlow.
...
# When you are done using TensorFlow, deactivate the environment.
(tensorflow-venv3)$ deactivate
- To delete a virtual environment, just delete its folder. (In this case, it would be
rm -rf tensorflow-venv3
.)
You can test whether both TensorFlow installed in python 2 and python 3 works. See below for my example.

Posts I referenced: