In order not to affect the local environment, so this article is based on Docker to install Tensorflow. My environment is Ubuntu16.04.

Install the Docker

Docker is divided into CE and EE. Here we choose CE, which is the regular community version, and first remove the old version that may exist on the local machine.

Remove the old version

$ sudo apt-get remove docker \
               docker-engine \
               docker.io
Copy the code

Install optional kernel modules

After Ubuntu14.04, some cropping systems will move some kernel modules to the optional kernel package, usually starting with linux-image-extra-*. The AUFS storage driver recommended by Docker is included in the optional kernel module package, so it is recommended to install the optional kernel module package. You can install it using the following command:

$ sudo apt-get update

$ sudo apt-get install \
    linux-image-extra-$(uname -r) \
    linux-image-extra-virtual
Copy the code

Prepare certificates and keys

Before formal installation, we need to add the certificate and HTTPS transfer software package to ensure that the software download process will not be tampered with:

$ sudo apt-get update

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
Copy the code

Add the GPG key of the software source:

$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -


# the official source
# $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Copy the code

Finally add Docker software source:

$ sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"


# the official source
# $ sudo add-apt-repository \
# "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
# $(lsb_release -cs) \
# stable"

Copy the code

Install the Docker

$ sudo apt-get update

$ sudo apt-get install docker-ce
Copy the code

Create a Docker user group

Docker usually uses Unix sockets to communicate with the Docker engine, usually only root and Docker group users can access the socket, otherwise you will always have to sudo, so it is best to add your current docker users to the docker user group.

Create a Docker user group

$ sudo groupadd docker
Copy the code

Add the current user to a user group

$ sudo usermod -aG docker $USER
Copy the code

Finally, log in to the system again

Test the Docker

Ensure service startup

$ sudo service docker start
Copy the code

Use the HelloWorld test

Docker run hello-world Unable to find image'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete 
Digest: sha256:083de497cff944f969d8499ab94f07134c50bcf5e6b9559b27182d3fa80ce3f7
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image whichruns the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the  Docker client,which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
Copy the code

If yes, the installation is successful.

Install Tensorflow

With Docker, there is little difficulty in installing Tensorflow.

Download mirror

docker pull tensorflow/tensorflow
Copy the code

After downloading, display:

Status: Downloaded newer image for tensorflow/tensorflow:latest
Copy the code

Create a Tensorflow container

docker run --name my-tensorflow -it -p 8888:8888 -v ~/tensorflow:/test/data tensorflow/tensorflow
Copy the code
  • –name: creates the container name, namely, my-tensorflow
  • -it: The command line is reserved
  • p 8888:8888: Combines port 8888 with port 8888http://localhost:8888/mapping
  • -v ~/tensorflow:/test/data: mounts the local ~/tensorflow to /test/data in the container
  • Tensorflow/tensorflow: default is tensorflow/tensorflow: latest, specify the use of images

After entering the above command, the default container is started and the command line displays:

[I 15:08:31.949 NotebookApp] Writing Notebook Server cookie secret to / root/local/share/jupyter/runtime/notebook_cookie_secret [W 15:08:31. 970 NotebookApp] WARNING: The notebook Server is listening on all IP addresses and not using encryption. This is not recommended. [I 15:08:31.975 NotebookApp] Serving notebooks fromlocaldirectory: Notebooks [I 15:08:31.975 NotebookApp] 0 active kernels [I 15:08:31.975 NotebookApp] The Notebook is running At: [I 15:08:31.975 NotebookApp] http://[all IP addresses on your system]:8888/? Token = 649 d7cab1734e01db75b6c2b476ea87aa0b24dde56662a27 [I 15:08:31. 975 NotebookApp] Use Control - C to stop this server And shut down all kernels (twice to skip confirmation). [C 15:08:31.975 NotebookApp] Copy/paste this URL into your memory browser when you connectforThe first time to login with a token:; [I 15:09:08.581 NotebookApp] 302 GET /? Token = 649 d7cab1734e01db75b6c2b476ea87aa0b24dde56662a27 (172.17.0.1) 0.42 msCopy the code

Copy the URL with token and open it in the browser

http://[all ip addresses on your system]:8888/?token=649d7cab1734e01db75b6c2b476ea87aa0b24dde56662a27
Copy the code

The following information is displayed:

Closed container

docker stop my-tensortflow
Copy the code

Open again

docker start my-tensortflow
Copy the code

If you don’t like Jupyter Notebook, you can also create a command-line based container

Command line based container

docker run -it --name bash_tensorflow tensorflow/tensorflow /bin/bash
Copy the code

This creates a container named BASH_tensorflow

Start the container again with the start command:

docker start bash_tensorflow
Copy the code

Then connect the container:

docker attach bash_tensorflow
Copy the code

You can see that we are using the terminal to connect to the container, just as we do with Linux.

This image does not have Vim installed by default, so I download Vim to write the code myself.

At this point, the installation process ends.