Parallel programming on Ubuntu using OpenMP with C/C++

This post introduces the basic of parallel programming on Ubuntu using OpenMP with C/C++. (MP in OpenMP refers to as Multi-processing.)

OpenMP, is an an API that enables direct multi-threaded, shared memory parallelism. See here for a nice OpenMP tutorial. A good introduction of OpenMP can be found here.

See here for wiki intro to OpenMP.

OpenMP uses a portable, scalable model that gives programmers a simple and flexible interface for developing parallel applications for platforms ranging from the standard desktop computer to the supercomputer.

An application built with the hybrid model of parallel programming can run on a computer cluster using both OpenMP and Message Passing Interface (MPI), such that OpenMP is used for parallelism within a (multi-core) node while MPI is used for parallelism between nodes. There have also been efforts to run OpenMP on software distributed shared memory systems, to translate OpenMP into MPI and to extend OpenMP for non-shared memory systems.

1: Create a text file and paste the follow code into it

Writing the hello world code in C is the same everywhere. There are only minor differences in the code.


int main( int ac, char **av)


#pragma omp parallel // specify the code between the curly brackets is part of an OpenMP parallel section.


printf("Hello World!!!\n");


return 0;


Save the code as anyname.c.  Now let’s compile the code.

2:  Open a terminal (CTRL + ALT + T)

3: Compile the code

In your terminal, cd to the path where the c script you just created is located.

To compile your code, simply type this in the terminal

gcc anyname.c -o anyname.out

If there is any problem in the code, you must fix it and compile again. Now to next (big) step .

4: Number of threads and running in OpenMp

When the code is compiled correctly, we can run it parallel form using OpenMP. firstly we determine number of threads we are going to use. type this in your terminal:


you can try changing the number of threads into numbers you like. (Note that how many maximum threads you can use depends on your machine’s hardware.)

Then, we are going to compile it using the OpenMP. It is similar to normal compiling but with addition of a few words. The format is like this:

gcc -fopenmp anyname.c -o anyname.out

after that, you can run the program. To run the program, type this into your terminal:


you will find out that the hello world prints out as many times as your thread number.


Notes for c++ with OpenMP :

Here I will just talk about how to compile cpp code, the other part with OpenMP is the same as c code, except for: need to change .c to .cpp and gcc to g++.

If you don’t have C/C++ compiler then first install it by  (Normally this comes with an installed Ubuntu OS):
sudo apt-get build-essential
Then open Gedit or any editor , write your code and save it as
“myprogram.cpp”. (See below for a simple example of c++ code)

#include <iostream>
int main()
  #pragma omp parallel 
      std::cout << "hello world \n";
    return 0;

Next step is to compile:
Make sure your terminal is cd to the folder where you have saved your .cpp file.
cd /path to/the cpp file
Once done you can compile it using G++
g++ myprogram.cpp
This will output a.out (by default) which is your executable code that can be run as
If you want to name the output instead of a.out, you can pass a parameter to G++
g++ myprogram.cpp -o myprogram
This time it will output a file “myprogram” which again can be run in the same way


Notes for OpenMP:

You don’t install OpenMP. OpenMP is a feature of the compiler. Check that the compiler you are using implements OpenMP, which is an API for parallel programming in C/C++/Fortran.

Check with the OpenMP website, for a list of compilers and platforms that support OpenMP programming.

OpenMP Directives:

  1. Directive Format
  2. C/C++ Directive Format
  3. Directive Scoping
  4. PARALLEL Construct

(Thanks Weiming for introducing this cool feature of OpenMP to the lab.)

Posts referenced:

Commonly used Linux commands (Ubuntu)

This page lists commonly used Linux commands to help those who are not very familiar with Linux command environment. I have been collecting and recording those from my experience.

I was once a beginner, so I can understand the pain for Linux beginners. I have not yet seen any post that has done comprehensive collection of commands on Linux, so I thought I could help this out. That is why you see this post. Here you go. Happy Learning!

You can see further reading list at the end of this post.

Note: Do not contain space in your filename or directory name, use underscore instead of space.

======Basic commands:

  • cd 

this command will goes back to the home directory of your account, no matter where your current directory is located in your terminal.

  • cd ../

this command will go to the parent directory of your current directory in terminal

  • rmdir 

remove/delete an empty folder.


first cd into the parent folder of the empty folder to remove

rmdir test

  • rm -rf ./*

    first cd to a directory, and this command will empty all of things under the current directory

  • ls  

list all the files and folders under current path

  • ls -l  

list all the files and folders with details like dates.

  • ls -l -t

list files and folders ordered by time.

  • ls -ltrh 

list all the csv files under the current directory in long format by time and in reverse order, the file size in human readable format (e.g., in mb, or gb, instead of byte size)

  • -l List in long format. If the output is to a terminal, a total sum for all the file sizes is output on a line before the long listing.
  • -r Reverse the order of the sort to get reverse lexicographical order or the oldest entries first (or largest files last, if combined with sort by size.
  • -t Sort by time modified (most recently modified first) before sorting the operands by lexicographical order.
  • ls -ltrh *.csv

list all the csv files under the current directory in long format by time and in reverse order, the file size in human readable format (e.g., in mb, or gb, instead of byte size)

  • find

find -name ‘*.jpg’ -exec cp {} ./test/ \;

Find all jpg files  and then copy the found files to the folder test which is subfolder of current path.
Note that: the current path should be the path where the files to search are located. (i.e., use cd to locate to the directory where the files are in before type in the cmd below into terminal.)

  • rm -r -f

-r means recursive, it will remove folders and subfolders and files within the folders and subfolders

-f means force

  • mkdir [folder name]

create new folder


mkdir image

  • cp [filename] [new filename]

copy and rename file

  • cp [filename] [path/to/new/lotcation/filename]

copy the file to another location

if you use this commond to copy a directory, you would meet this error:

cp: omitting directory ...

The error notice means you told cp to copy files and not directories. The warning is about cp finding a directory and informing you it will be skipped.

  • cp -r [directory] [path/to/new/location/directory]

copy a directory to another location.

cp -r means recursive and this option will make cp also include sub-directories.

If you meet permission denied error, add sudo before the command, and it will ask your password.

  • mv [directory] [path/to/new/location/directory]

If you meet permission denied error, add sudo before the command, and it will ask your password.

  • nano [new file name or /path/to/new file/new file name]


nano  myexample   #it will create a new empty file named “myexample” under the current directory

  • nano [file name]

If the file name already exists, it will open the file and you can edit it.

Note: Ctrl+O to save the file, and then hit Enter, and then Ctrl +X to close the file.



======More advanced commands:

  • cd into directory without having permission

When cd into a directory and the following error occurs

bash: cd: your-dirctory: Permission denied

The solution is:

Enter super user mode, and cd into the directory that you are not permissioned to go into. Sudo requires administrator password.

sudo su  
cd directory  # you will notice that your prompt changes after your enter your root password. now you can cd to the directory.

# to exit "super user" mode, type exit.
  • lspci

check GPU information on Ubuntu

look for “VGA compatible controller:”…

  • sudo nvidia-smi

check GPU info and GPU usage.

  • sudo reboot -h now

reboot a server from terminal

  • sudo shutdown -h now

shut down a server from terminal

Note: If your Ubuntu Server 16.o4 LTS has Black Screen after reboot or shut down, try pressing (simultaneously) Ctrl + Alt + F2 to see whether you can switch to different console

  • vncserver -kill :1  

This is a vncserver command. It is used to kill a port of a GUI by VNC server, where 1 is the port you would like to kill.

  • vncviewer -via username@yourserver_hostname :1

connect to a server via vncviewer from a linux-based client. you need to change the port number 1 to yours.

echo is a built-in command in the bash and C shells that writes its arguments to standard output.

See here, and here, and herefor example usage of it.

  • cat

See here for example usage of cat command.

  • chmod

see here for example usage of chmod command.


  • check supercomputing Cluster’s Linux distribution and version

$ lsb_release -a
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID:    RedHatEnterpriseServer
Description:    Red Hat Enterprise Linux Server release 6.4 (Santiago)
Release:    6.4
Codename:    Santiago

  • show the list of top processes ordered by RAM and CPU  use in descendant form

(remove the pipeline and head if you want to see the full list):

$ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

Brief explanation of the options used in the command above:

— The -o (or –format) option of ps allows us to specify the output format.

— the processes’ PIDs (pid), PPIDs (pid)

— the name of the executable file associated with the process (cmd), and

— the RAM and CPU utilization (%mem and %cpu, respectively).

We can use --sort to sort by either %mem or %cpu. By default, the output will be sorted in ascendant form, but usually we prefer to reverse that order by adding a minus sign in front of the sort criteria to make it list in descendant.

To add other fields to the output, or change the sort criteria, refer to the OUTPUT FORMAT CONTROL section in the man page of ps command.

======File Transfer: getting files to/from your account on a server

  • On Linux generally the command line scp command.
 Examples of using the command line are:

scp -p file_name username@yourserver_hostname:destination/directory

  • or for a full directory tree:

scp -pr dir_name username@yourserver_hostname:destination/directory


Note that if you want to transfer files from server to your client computer, just reverse the directory.

e.g., scp -pr username@yourserver_hostname:source/directory dir_name_on_your_client


======download files

  • wget (tool for downloading files)  (pdf)
  • See Linux wget command (pdf), which provides detailed and comprehensive different tags (options) to use with wget command.

======Save terminal output to a file

  • sudo command -option | tee logThis command will show output on terminal and save to a file at the same time.
  • Save terminal output to a fileredirect the output to a file: someCommand > someFile.txt Or if you want to append data: someCommand >> someFile.txt If you want stderr too use this: someCommand &> someFile.txt or this to append:  someCommand &>> someFile.txt
  • Tail -f log.txt

Python related commands:

  • enter python environment

type python in terminal, and it will show python 2.7 version info and also enter into python 2 environment

  • enter python 3 environment


and it will show python 3 version info (e.g., python 3.5.2) and also enter into python 3 environment.


======Git related commands

  • git clone the url to gitclone

for example:

first cd into the folder you want the models to be cloned to in your terminal, and then issue this command. it will clone the model foder from under your current folder in your terminal.

git clone


======Some useful shortcuts on linux

  • you can open multiple terminals

open each terminal by pressing Ctrl + Alt + T.

  • Shortcut to bring all open terminals to the front

After you bring one terminal window in the front, press Alt+~ to bring all other terminal windows in the front one by one:

  • CTRL + C − terminate the current command. 

======References and further reading list:

Linux and Unix top 10 command pages  (See here for links to more commands intro)

Below is a listing of the top 10 Unix command pages by the amount of times they have been accessed on the Computer Hope server.

  1. Linux and Unix tar command help
  2. Linux and Unix chmod command help
  3. Linux and Unix ls command help
  4. Linux and Unix find command help
  5. Information about the Linux and Unix grep command
  6. Linux and Unix cp command help
  7. Linux and Unix vi command help
  8. Linux and Unix ifconfig command help
  9. Linux and Unix date command help
  10. Linux and Unix kill command help

======TOC of the nice tutorial: Linux Shell Commands: A Tutorial Quick Reference for Desktop Users

Table of Contents

1. A Short Intro to the Command Line

This chapter will acquaint you with the basics of the command line. To maximize your learning, you should follow along by typing in the example commands given. Every major Linux distribution has a menu item called “shell”, “console”, “terminal” or the like, which will give you a window with a command line interface. In this book, I assume that readers work in a graphical desktop environment and use the Bash shell in a terminal window. Bash is the default shell in all major Linux distributions.

2. Getting Information

The commands presented in this chapter provide valuable information on the state and configuration of your system.

3. Managing Files and Directories

The command line offers you great flexibility in creating, copying, moving and editing files and directories, as this chapter shows.

  • cd (change directory)  (pdf)
  • chgrp (change group ownership)  (pdf)
  • chmod (change file permissions)  (pdf)
  • chown (change file ownership)  (pdf)
  • cp (copy files and directories)  (pdf)
  • dd (write data to devices)  (pdf)
  • find (search for files)  (pdf)
  • ln (make links between files)  (pdf)
  • locate (find files by name)  (pdf)
  • mkdir (create a directory)  (pdf)
  • mount (mount file systems)  (pdf)
  • mv (rename files)  (pdf)
  • rm (remove files or directories)  (pdf)
  • rmdir (remove empty directories)  (pdf)
  • shred (delete a file securely)  (pdf)
  • touch (change file timestamps)  (pdf)
  • umount (unmount file systems)  (pdf)

4. Managing Processes

Linux provides powerful tools for controlling the execution of your programs. Some of the most important tools are presented in this chapter.

  • disown (detach a job from the shell)   (pdf)
  • kill (terminate a process)  (pdf)
  • ps (list running processes)  (pdf)
  • pstree (display a tree of processes)  (pdf)
  • shutdown (halt or reboot the system)  (pdf)
  • sudo (execute a command as root)  (pdf)

5. Working with Text

Processing plain text files is a big strength of Linux. The commands presented in this chapter allow you to display particular parts of files (e.g. head, tail), reorder their contents (e.g. sort), carry out search/replace operations (e.g. grep, sed), and much more.

  • cat (concatenate and output files)  (pdf)
  • cut (output columns from files)  (pdf)
  • diff (show differences between files)  (pdf)
  • grep (print lines matching a pattern)  (pdf)
  • head (output the first part of files)  (pdf)
  • less (view file by pages)  (pdf)
  • pdftk (manipulate PDF files)  (pdf)
  • sed (search and replace text)  (pdf)
  • sort (sort lines of text files)  (pdf)
  • tail (output the last part of files)  (pdf)
  • wc (count lines, words and characters)  (pdf)

6. Being Productive

This chapter collects some commands that can help you accomplish everyday tasks quickly and efficiently. Many of the commands are faster or more reliable replacements for popular graphical applications. For example, wget can replace a graphical download manager.

  • alias (define command shortcuts)  (pdf)
  • alsamixer (audio mixer)  (pdf)
  • bc (command line calculator)  (pdf)
  • history (display command history)  (pdf)
  • rsync (fast, versatile file copying tool)  (pdf)
  • tar (Linux archiving utility)  (pdf)
  • unrar (extract files from RAR archives)  (pdf)
  • unzip (extract files from ZIP archives)  (pdf)
  • wget (tool for downloading files)  (pdf)
  • xmodmap (change key bindings)  (pdf)

======The end of the TOC of the nice tutorialLinux Shell Commands: A Tutorial Quick Reference for Desktop Users

======apt-get usages

======curl command examples

cURL can be used in many different and useful ways. Using cURL, we can download, upload and manage files, check email address, or even update status on some of the social media websites or check the weather outside.

cURL is very useful command line tool to transfer data from / to a server. cURL supports various protocols, including FILE, HTTP, HTTPS, IMAP, IMAPS, LDAP, DICT, LDAPS, TELNET, FTP, FTPS, GOPHER, RTMP, RTSP, SCP, SFTP, POP3, POP3S, SMB, SMBS, SMTP, SMTPS, and TFTP.

This tutorial covers five of the most useful and basic uses of cURL tool:

–Check URL

One of the most common and simplest uses of cURL is typing the command itself, followed by the URL you want to check

#This command will display the content of the URL on your terminal

–Save the output of the URL to a file

The output of the cURL command can be easily saved to a file by adding the -o option to the command, as shown below

curl -o website
#the output will be save to a file named ‘website’ in the current working directory

–Download files with cURL

curl -O

# the -O option used for saving files to current working directory without renaming
# e.g.,  the ‘’ zip archive will be downloaded to the current working directory.
curl -o

# the ‘’ archive will be downloaded and saved as ‘’.
curl -O -O

# cURL can be also used to download multiple files simultaneously
#cURL can be also used to download files securely via SSH

curl -u user s

# Note that the full path of the file to be downloaded is required

–Get HTTP header information from a website

You can easily get HTTP header information from any website you want by adding the -I option (capital ‘i’) to cURL.

curl -I

–Access an FTP server

#  access your FTP server with cURL 
curl --user username:password

# cURL will connect to the FTP server and list all files and directories in user’s home directory
curl --user username:password
# download a file via FTP using curl
curl -T --user username:password
# upload a file to  the FTP server

–check cURL manual page to see all available cURL options and functionalities

man curl

This post covers detailed and comprehensive explanation of different options to use with curl command.

  • $ free -m

Linux has the habit of caching lots of things for faster performance, so that memory can be freed and used if needed.

  • $ cat /proc/meminfo
  • $ vmstat -s

Install GPU TensorFlow from Source on Ubuntu Server 16.04 LTS

I installed GPU TensorFlow from source on Ubuntu Server 16.04 LTS with CUDA 8 and a GeForce GTX 1080 GPU, but it should work for Ubuntu Desktop 16.04 LTS.

In this tutorial I will be going through the process of building the latest TensorFlow from sources for Ubuntu Server 16.04.  TensorFlow now supports using Cuda 8.0 & CuDNN 5.1 so you can use the pip’s from their website for a much easier install.

In order to use TensorFlow with GPU support you must have a NVIDIA graphic card with a minimum compute capability of 3.0.

Getting started I am going to assume you know some of the basics of using a terminal in Linux. (Check this post for commonly used Linux commands.)

1: Install Required Packages

Open a terminal by pressing Ctrl + Alt + T.

(Because it is Ubuntu Server 16.04, need to install those required packages below, if you are on Ubuntu Desktop 16.04, most of the libraries below already come with the OS installation.)

Paste each line one at a time (without the $) using Shift + Ctrl + V

$ sudo apt-get install openjdk-8-jdk git python-dev python3-dev python-numpy python3-numpy build-essential python-pip python3-pip python-virtualenv swig python-wheel libcurl3-dev

2: Update & Install NVIDIA Drivers

Note that if you have a monitor connected to your server, be sure to disconnect it before you start to install the NVIDIA drivers. Otherwise, it may cause trouble when you reboot your server after you install your NVIDIA drivers. You can reconnect your monitor after you successfully install the NVIDIA drivers.

You must also have the 367 (or later) NVidia drivers installed, this can easily be done from Ubuntu’s built in additional drivers after you update your driver packages. (you can check the latest drivers version according to your GPU info from The NVIDIA downloads page, for example, mine is 375.)

$ sudo add-apt-repository ppa:graphics-drivers/ppa
$ sudo apt update
$ sudo apt-get install nvidia-375  

(Note: use the following command if you encounter this error “sudo: add-apt-repository: command not found”)

$ sudo apt-get install software-properties-common

Once installed the driver restart your computer. You can use the command below to reboot the server from command line.

$ sudo reboot -h now

If you experience any troubles booting linux or logging in: try disabling fast & safe boot in your bios and modifying your grub boot options to enable nomodeset.

You can use the following command to get various diagnostics of the GTX 1080.

$ sudo nvidia-smi


3: Install NVIDIA CUDA Toolkit 8.0 

Skip if not installing with GPU support

(Note: If you have older version of CUDA and cuDNN installed, check the post for uninstallation.  How to uninstall CUDA Toolkit and cuDNN under Linux? (02/16/2017) (pdf))

(If you need to use command line to transfer files from your clienet computer to your server. refer to the following scp command)

File Transfer: getting files to/from  your Ubuntu server

copy file:

scp -p file_name username@yourserver_hostname:destination/directory

for a full directory tree:

scp -pr dir_name username@yourserver_hostname:destination/directory


To install the Nvidia Toolkit  download base installation .run file from Nvidia website (download the .run file. NOT THE DEB FILE!!).


$ cd ~/Downloads # or directory to where you downloaded file
$ sudo sh  # hold s to skip

This will install cuda into: /usr/local/cuda-8.0

MAKE SURE YOU SAY NO TO INSTALLING NVIDIA DRIVERS! (Very important, If you answer yes, the GTX 1080 375 driver will be overwritten.

Also make sure you select yes to creating a symbolic link to your cuda directory.

(FYI, the following is the questions to be asked.)

The following contains specific license terms and conditions
for four separate NVIDIA products. By accepting this
agreement, you agree to comply with all the terms and
conditions applicable to the specific product(s) included

Do you accept the previously read EULA?
accept/decline/quit: accept

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 361.62?
(y)es/(n)o/(q)uit: n

Install the CUDA 8.0 Toolkit?
(y)es/(n)o/(q)uit: y

Enter Toolkit Location
[ default is /usr/local/cuda-8.0 ]:

Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y

Install the CUDA 8.0 Samples?
(y)es/(n)o/(q)uit: y

Enter CUDA Samples Location
[ default is /home/liping ]:

Installing the CUDA Toolkit in /usr/local/cuda-8.0 …
Installing the CUDA Samples in /home/liping …
Copying samples to /home/liping/NVIDIA_CUDA-8.0_Samples now…
Finished copying samples.


= Summary =

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-8.0
Samples:  Installed in /home/liping, but missing recommended libraries

Please make sure that
 –   PATH includes /usr/local/cuda-8.0/bin
 –   LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ and run ldconfig as root

To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-8.0/bin

Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-8.0/doc/pdf for detailed information on setting up CUDA.

***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 361.00 is required for CUDA 8.0 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
    sudo <CudaInstaller>.run -silent -driver

Logfile is /tmp/cuda_install_7169.log


4: Install NVIDIA cuDNN

Once the CUDA Toolkit is installed, download cuDNN v5.1 for Cuda 8.0 from NVIDIA website (Note that you will be asked to register an NVIDIA developer account in order to download) and extract into /usr/local/cuda via:

$ sudo tar -xzvf cudnn-8.0-linux-x64-v5.1.tgz
$ sudo cp cuda/include/cudnn.h /usr/local/cuda/include
$ sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
$ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

Then update your bash file:

$ nano ~/.bashrc

This will open your bash file in a text editor which you will scroll to the bottom and add these lines:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda

Once you save and close the text file you can return to your original terminal and type this command to reload your .bashrc file:

$ source ~/.bashrc

5: Install Bazel

Instructions also on Bazel website

$ echo "deb [arch=amd64] stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
$ curl | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install bazel
$ sudo apt-get upgrade bazel

6: Clone TensorFlow

$ cd ~
$ git clone

7: Configure TensorFlow Installation

$ cd ~/tensorflow
$ ./configure

Use defaults by pressing enter for all except:

Please specify the location of python. [Default is /usr/bin/python]:

For Python 2 use default or If you wish to build for Python 3 enter:

$ /usr/bin/python3.5

Please input the desired Python library path to use. Default is [/usr/local/lib/python2.7/dist-packages]:

For Python 2 use default or If you wish to build for Python 3 enter:

$ /usr/local/lib/python3.5/dist-packages

Unless you have a Radeon graphic card you can say no to OpenCL support. (has anyone tested this? ping me if so!)

Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]:

$ 8.0

Please specify the Cudnn version you want to use. [Leave empty to use system default]:

$ 5

Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at:
Please note that each additional compute capability significantly increases your build time and binary size.
[Default is: “3.5,5.2”]: 5.2,6.1
INFO: Starting clean (this may take a while). Consider using –expunge_async if the clean takes more than several minutes.
INFO: All external dependencies fetched successfully.
Configuration finished

If all was done correctly you should see:

INFO: All external dependencies fetched successfully.
Configuration finished.

8: Build TensorFlow

Warning Resource Intensive I recommend having at least 8GB of computer memory.

(Note that you current path in terminal is ~/tensorflow) 

If you want to build TensorFlow with GPU support enter (Note that the command should be one line):

$ bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

For CPU only enter:

$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

9:Build & Install Pip Package

(Note that you current path in terminal is ~/tensorflow) 

This will build the pip package required for installing TensorFlow in your ~/tensorflow_pkg [you can change this directory as the one you like]

$ bazel-bin/tensorflow/tools/pip_package/build_pip_package ~/tensorflow_pkg

Remember that, at any time, you can manually force the project to be reconfigured (run the ./configure file in step 7 above to reconfigure) and built from scratch by emptying the directory ~/tensorflow_pkg  with:

rm -rf ./*

Now you can cd into the directory where you build your tensorflow, for example my case is  ~/tensorflow_pkg

then issue the following command according to you are using python or python 3.

To Install Using Python 3 (remove sudo if using a virtualenv)

$ sudo pip3 install tensorflow-0.12.1-cp27-cp27mu-linux_x86_64.whl

# tip: after you type tensorflow, you can hit Tab on your keyboard to autofill the name of the .whl file you just built

For Python 2 (remove sudo if using a virtualenv)

$ sudo pip install tensorflow-0.12.1-cp27-cp27mu-linux_x86_64.whl

# tip: after you type tensorflow, you can hit Tab on your keyboard to autofill the name of the .whl file you just built

Note that if you meet this error:

The directory ‘/home/youraccountname/.cache/pip/http’ or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo’s -H flag. 

Change the command above to

 sudo -H pip install tensorflow-0.12.1-cp27-cp27mu-linux_x86_64.whl

If you meet this warning

You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the ‘pip install –upgrade pip’ command.

I would suggest just ignore this – sometimes after doing upgrade there might appear some trouble because of dependencies.

10: Test Your Installation

Finally, time to test our installation.

To test the installation, open an interactive Python shell and import the TensorFlow module:

$ cd # this will return to your home root directory ~
$ python  # or python3
>>> import tensorflow as tf
I tensorflow/stream_executor/] successfully opened CUDA library locally
I tensorflow/stream_executor/] successfully opened CUDA library locally
I tensorflow/stream_executor/] successfully opened CUDA library locally
I tensorflow/stream_executor/] successfully opened CUDA library locally
I tensorflow/stream_executor/] successfully opened CUDA library locally

With the TensorFlow module imported, the next step to test the installation is to create a TensorFlow Session, which will initialize the available computing devices and provide a means of executing computation graphs:

>>> sess = tf.Session()
>>> sess = tf.Session() 
I tensorflow/core/common_runtime/gpu/] Found device 0 with properties: 
name: GeForce GTX 1080
major: 6 minor: 1 memoryClockRate (GHz) 1.7335
pciBusID 0000:03:00.0
Total memory: 7.92GiB
Free memory: 7.81GiB

To manually control which devices are visible to TensorFlow, set the CUDA_VISIBLE_DEVICES environment variable when launching Python. For example, to force the use of only GPU 0:


You should now be able to run a Hello World application:

>>> hello_world = tf.constant("Hello, TensorFlow!") 
>>> print 
Hello, TensorFlow! 
>>> print*tf.constant(3)) 

TensorFlow also has instructions on how to do a basic test and a list of common installation problems.

You should now have TensorFlow installed on your computer. This tutorial was tested on a fresh install of Ubuntu Server 16.04 with a GeForce GTX 1080.


Referenced posts (See this page for more TensorFlow setup links I collected):






Install and Configure VNC Server on Ubuntu Server 16.04

VNC server is used to share graphical desktop which can be controlled from other computers.
Basically ubuntu server does not contain GUI, which needs to be installed before installing VNC server. Please note that ubuntu server edition is  carefully designed to utilize less hardware resources (minimal environment), installing GUI might leads to high hardware utilization.

In this guide, we will be setting up VNC on a Ubuntu 16.04 server and connecting to it securely through an SSH tunnel. The VNC server we will be using is TightVNC, a fast and lightweight remote control package. This choice will ensure that our VNC connection will be smooth and stable even on slower internet connections.

1: Installing the Desktop Environment and VNC Server

By default, an Ubuntu Server 16.04 does not come with a graphical desktop environment or a VNC server installed, so we’ll begin by installing those. Specifically, we will install packages for the latest Xfce desktop environment and the TightVNC package available in the official Ubuntu repository.

On your server, install the Xfce and TightVNC packages.

$ sudo apt install xfce4 xfce4-goodies tightvncserver

To complete the VNC server’s initial configuration after installation, use the vncserver command to set up a secure password.

$ vncserver

You’ll be promoted to enter and verify a password, and also a view-only password. Users who log in with the view-only password will not be able to control the VNC instance with their mouse or keyboard. This is a helpful option if you want to demonstrate something to other people using your VNC server, but isn’t necessary.

Note: if you are not promoted to enter and verify a password, you can use the following command to do so. See here (pdf) for more info about vncpasswd command.


Running vncserver completes the installation of VNC by creating default configuration files and connection information for our server to use. With these packages installed, you are now ready to configure your VNC server.

2: Configuring the VNC Server

First, we need to tell our VNC server what commands to perform when it starts up. These commands are located in a configuration file called xstartup in the .vnc folder under your home directory. The startup script was created when you ran the vncserver in the previous step, but we need modify some of the commands for the Xfce desktop.

When VNC is first set up, it launches a default server instance on port 5901. This port is called a display port, and is referred to by VNC as :1. VNC can launch multiple instances on other display ports, like :2, :3, etc. When working with VNC servers, remember that :X is a display port that refers to 5900+X.

Because we are going to be changing how the VNC server is configured, we’ll need to first stop the VNC server instance that is running on port 5901.

$ vncserver -kill :1

The output should look like this, with a different PID:

Killing Xtightvnc process ID 17648

Before we begin configuring the new xstartup file, let’s back up the original.

$ mv ~/.vnc/xstartup ~/.vnc/xstartup.bak

Now create a new xstartup file with nano or your favorite text editor.

$ nano ~/.vnc/xstartup

Paste these commands into the file so that they are performed automatically whenever you start or restart the VNC server, then save and close the file.

xrdb $HOME/.Xresources
startxfce4 &

The first command in the file, xrdb $HOME/.Xresources, tells VNC’s GUI framework to read the server user’s .Xresources file. .Xresources is where a user can make changes to certain settings of the graphical desktop, like terminal colors, cursor themes, and font rendering. The second command simply tells the server to launch Xfce, which is where you will find all of the graphical software that you need to comfortably manage your server.

To ensure that the VNC server will be able to use this new startup file properly, we’ll need to grant executable privileges to it.

$ sudo chmod +x ~/.vnc/xstartup

Now, restart the VNC server.

$ vncserver

The server should be started with an output similar to this:

New 'X' desktop is

Starting applications specified in /home/liping/.vnc/xstartup
Log file is /home/liping/.vnc/

At this point you have a VNC server running on display :1. You will use this information later when you want to connect to it.

3: Testing the VNC Desktop

connect directly to the server using VNC Viewer.

on your client computer (not on the server), install vncviewer using the command below (assuming your client computer is running Ubuntu or linux-based OS).

$ sudo apt-get install vncviewer

then type the command below in your client computer terminal to connect to your server via the VNC Desktop. Note the space after your server host name and before “:”

$ vncviewer -via username@yourserver_hostname :1

You need to change the port number 1 to yours.

Once you are connected, you should see the default Xfce desktop. It should look something like this:

You can access files in your home directory with the file manager or from the command line, as seen here:

4: Creating a VNC Service File

Next, we’ll set up the VNC server as a systemd service. This will make it possible to start, stop, and restart it as needed, like any other systemd service.

First, create a new unit file called /etc/systemd/system/vncserver@.service using your favorite text editor:

$ sudo nano /etc/systemd/system/vncserver@.service

Copy and paste the following into it. Be sure to change the value of User and the username in the value of PIDFILE to match your username.

Description=Start TightVNC server at startup

ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i
ExecStop=/usr/bin/vncserver -kill :%i


Save and close the file.

Next, make the system aware of the new unit file.

$ sudo systemctl daemon-reload

Enable the unit file.

$ sudo systemctl enable vncserver@1.service

Stop the current instance of the VNC server if it’s still running.

$ vncserver -kill :1

Then start it as you would start any other systemd service.

$ sudo systemctl start vncserver@1

You can verify that it started with this command:

$ sudo systemctl status vncserver@1

If it started correctly, the output should look like this:


NOTE: DO NOT USE the shut down or log off menu in your VNC viewer, instead, just click the cross button at the top left corner. Otherwise, it will cause problems when you try to connect to it next time. Because this is a virtual GUI, remember not to use the shut down etc. menu on the top right corner of your GUI.


You should now have a secured VNC server up and running on your Ubuntu 16.04 server. Now you’ll be able to manage your files, software, and settings with an easy-to-use and familiar graphical interface.


Posts referenced:

How to Install and Configure VNC on Ubuntu 16.04 (April 26, 2016,
How to install VNC server on ubuntu 14.04
Setup VNC in Ubuntu 16.04
How To Install VNC Server On Ubuntu 14.04
How do you run Ubuntu Server with a GUI?
Administer Ubuntu Server Trusty Tahr 14.04LTS using VNC

Install Ubuntu Server 16.04 LTS

To install Ubuntu Server 16.04, first download the Ubuntu Server 16.04 LTS ISO file and create a bootable USB. If you do not know how to do this, refer to this guide about installing Ubuntu Server from USB (check the section “Create a Bootable USB Installer” in the post. If it is not accessible, check this pdf version). Once you have the bootable USB ready, pop it in and make your system boot from USB (through the System BIOS).

ATTN: Check my post about boot mode first before proceeding to the installation steps.

1: Select Ubuntu Installer Language

Install Ubuntu 16.04 Server – Installer Language

2: Install Ubuntu Server 16.04

Start Ubuntu Server Installation. Click on Install Ubuntu Server to continue. For normal installations, you do not have to mess with any of the advanced options.

3: Select Operating System Language

4: Select Server Location

This helps in determining timezone.

5: Keyboard Detection

6: Network configuration

if your network has two interfaces, if you are sure which one to use, just select one and try, if it says failure, then go back and select the other options.

Note that: if you encounter this error below, it might be the case: Ethernet cable was loose… Reinstalled the OS and it should work.

Network autoconfiguration failed Your network is probably not using the DHCP protocol. Alternatively, the DHCP server may be slow or some network hardware is not working properly. <Continue>

If the ethernet is for sure not loose, then it’s most probably the issue that I introduced in this post (change the boot mode to Legacy Boot (i.e., disable UEFI mode), Secure Boot Off.) Reinstall the OS after set the Boot mode and boot order, then you should see the this succeeded screen:)

7: Hostname

Enter the hostname of the system. In this example, my server is named, so I enter

8: Server User Fullname

provide a Fullname for the primary account. This is not the root (administrator) user but this user can temporarily gain admin privileges using the sudo command.

Ubuntu does not let you log in as root user directly. Therefore, create a new system user here for the initial login. I will create a user with the name Administrator and user name administrator (don’t use the user name admin as it is a reserved name on Ubuntu Linux):

9: Server Username

provide the login username for the primary account.

10: Server Password

Select a password for the user created above.

Make sure you set a strong password with a mix of upper case letters, lower case letters, numbers, and special characters.

11: Home Directory Encryption

In general, you do not have to encrypt your home directory. But you may do so if you want to.

12: Confirm Timezone

The installer should automatically pick your timezone. Confirm the choice and move forward.

13: Ubuntu Server Drive Partitioning

This is one of the key steps and probably most complicated of all steps. For now, ensure that you have at least 8GB (headless – no desktop environment) or 10 GB (with desktop environment) of space. In this guide, assume that the server has 2 physical hard disks (sda and sdb). sda is the drive that will be formatted for Ubuntu Server 16.04 installation. sdb is a data storage drive (will not be formatted). Choose Manual partitioning method and proceed.

Primary partition vs. Logic partition

For Linux (Ubuntu), you need minimum TWO Partitions (Root and Swap). The recommended is to have 3 (Root, Swap and Home).
You you need to understand that it’s not possible to have more than 4 Primary Partitions, thus you need to create Extended Partition and inside that, you could have many other Logical Partitions.

If you are not familiar with partition, check this post: Manual Disk Partitioning Guide for Ubuntu Server Edition (pdf if the page is not accessible).

  • root partition (/). The bulk of the programs used for running the system will be installed here.
  • home partition (/home) the partition where your home directory will be located. In the course of using the system, files and folders you create will reside in various folders here
  • swap partition (swap): unformatted disk space for use as virtual memory. swap partition should be at least as big as your RAM size.

14: Confirm Partition Scheme

15: Write the Partitions to Disk

Because partitioning is critical, you will be asked one more time to confirm before partitions will be written to the hard disk.

16: Base Ubuntu 16.04 Server Installation

After partitioning, the installer continue to install Ubuntu Server 16.04 base system. Nothing to do here than wait for it to complete.

17: Setup HTTP Proxy

In typical Ubuntu Server setup, this is generally not needed. So, leave it blank and continue to install Ubuntu 16.04 Server.

Leave the HTTP proxy line empty unless you’re using a proxy server to connect to the Internet:

18: APT Repository Configuration

Ubuntu software are installed from the APT repository. Wait for the installer to configure it.

19: Setup Automatic Updates

Ubuntu Server can automatically install updates when they are available. While this can break things sometimes, installing just the security updates should be fine. So I recommend installing security updates automatically on your Ubuntu server.

20: Ubuntu Server Tasksel

After partitioning, this is the step that requires most user intervention. You will have to select what services you want to install on your Ubuntu 16.04 Server. “Standard System Utilities” should already be selected. In addition, there are some other server services you can install, for example, LAMP server, Samba file server, and OpenSSH Server.

LAMP server installs Apache web server, MySQL database server, and PHP. MySQL may be needed for running a dynamic website. Samba file server will allow you share files with Windows systems.  

OpenSSH will allow you to remotely connect and administer your Ubuntu Server through SSH.

The items I select here are OpenSSH Server and Standard System Utilities so that I can immediately connect to the system with SSH or an SSH client such as PuTTY after the installation has finished:

21: Ubuntu Server Installation

Ubuntu Server installer will continue to setup Ubuntu home server packages you selected in Tasksel. Nothing to do here than just wait.

22: Ubuntu Server Installation Continues

Once again, nothing much to do here. Let the installation continue.

23: GRUB Notification

Select Yes when you are asked Install the GRUB boot loader to the master boot record?

GRUB is the boot menu that is shown immediately after your Ubuntu Server powers on. It shows a list of all OSes installed on the system. It is installed to the hard drive containing the OS. In most cases this is /dev/sda.

24: Installing GRUB

As explained above, it is safe to install GRUB to /dev/sda. No input needed. Let the installer do its thing.

25: Finishing Ubuntu Server Installation

Again, nothing to do here. Let the installer finish a few things to complete setting up Ubuntu 16.04 LTS server.

The base system installation is now finished.

26: Reboot Ubuntu Server

Yah! you are done. After installation completes, hit continue to reboot server.

Note: If your Ubuntu Server 16.o4 LTS has Black Screen after reboot: 

Try pressing (simultaneosly) <Ctrl><Alt><F2> to see whether you can switch to different console

27: Ubuntu GRUB Menu

As explained above you will see the GRUB menu while booting. Nothing to do here. Your server will automatically boot to Ubuntu, which is the default setting.

28: First Login

When you are presented with the login screen enter the username and password you created earlier in the Ubuntu 16.04 Xenial Xerus installation process. Remember that Linux commandline does not show anything (asterisk) while typing passwords.

Now Login on the shell (or remotely by SSH) on the server as user “administrator”.

29: Ubuntu Server Headless Commandline

You should be on your Ubuntu Server commandline after successful login.

30: Get root Privileges

After the reboot, you can log in with your previously created username (e.g. administrator). Because we must run all the steps from this tutorial with root privileges, we can either prepend all commands in this tutorial with the string sudo, or we become root right now by typing:

sudo -s

(You can as well enable the root login by running)

sudo passwd root

And giving root a password. You can then directly log in as root, but this is frowned upon by the Ubuntu developers and community for various reasons. See

About Root

The root user is the administrative user in a Linux environment that has very broad privileges. Because of the heightened privileges of the root account, you are actually discouraged from using it on a regular basis. This is because part of the power inherent with the root account is the ability to make very destructive changes, even by accident.

The next step is to set up an alternative user account with a reduced scope of influence for day-to-day work.  It will tell you how to gain increased privileges during the times when you need them.

31: Create a New User

This example creates a new user called “sam”, but you should replace it with a username that you like:

adduser sam

You will be asked a few questions, starting with the account password.

Enter a strong password and, optionally, fill in any of the additional information if you would like. This is not required and you can just hit ENTER in any field you wish to skip.

32: Add root privileges to the new user you just created

Now, we have a new user account with regular account privileges. However, we may sometimes need to do administrative tasks.

To avoid having to log out of our normal user and log back in as the root account, we can set up what is known as “superuser” or root privileges for our normal account. This will allow our normal user to run commands with administrative privileges by putting the word sudo before each command.

To add these privileges to our new user, we need to add the new user to the “sudo” group. By default, on Ubuntu 16.04, users who belong to the “sudo” group are allowed to use the sudo command.

As root, run this command to add your new user to the sudo group (substitute the highlighted word with your new user):

  • usermod -aG sudo sammy

Now your user can run commands with superuser privileges! For more information about how this works, check out this sudoers tutorial.


33: Install the SSH Server (Optional)

If you did not select to install the OpenSSH Server during the system installation above, you could do it now:

apt-get install ssh openssh-server

From now on you can use an SSH client such as PuTTY on windows, and on mac and linux, you can ssh directly in your terminal by typing the command:

ssh your_account_name@yourserver_ip or

ssh your_account_name@hostname_of_yourserver


34: How to reboot the Server

Once you’ve installed Ubuntu Server you should make sure the server can boot properly. So type the following command at the prompt to reboot the server:

sudo reboot -h now

Once it’s rebooted and assuming everything’s working fine you’ll end up back at the command prompt. Now you can disconnect the keyboard and screen, but keep the Ethernet cable plugged in.

Note: If your Ubuntu Server 16.o4 LTS has Black Screen after reboot: 

Try pressing (simultaneosly) <Ctrl><Alt><F2> to see whether you can switch to different console

35: How to shutdown the Server with command line

Note: if you are using VNC to connect to your server, DO NOT SHUT DOWN your server through GUI, that would cause trouble. Instead, issue this command in your terminal to shut down the Ubuntu server.

sudo shutdown -h now

You can use the -P switch with shutdown to poweroff the computer.

sudo shutdown -P now

if you want to shut down forcefully, i.e., you don’t want to wait for processes to close normally? In that case you can use:

sudo poweroff -f


There are 35 steps to install Ubuntu 16.04 Server, most steps require no interaction from you. The whole installation can be done in less than 30 min (but this is the case when you are not encountering any errors while installation).


Posts referenced: 

Screenshot Guide: Install Ubuntu Server 16.04 LTS Xenial Xerus (

How to install a Ubuntu 16.10 (Yakkety Yak) Minimal Server (

Initial Server Setup with Ubuntu 16.04 (

How To Edit the Sudoers File on Ubuntu and CentOS (

How to install Ubuntu Server – Xenial Xerus 16.04LTS (

Installing Ubuntu Server for general use (

How to install Ubuntu server 16.04 and the Webmin GUI (

How to Install Ubuntu Server (

installing Ubuntu Server from USB  (

Linux: How to Install Ubuntu Linux Server 16.04 LTS (

Manual Disk Partitioning Guide for Ubuntu Server Edition

Choosing partition types for swap and root and choosing device for bootloader installation


Look at this before installing Ubuntu 16.04 LTS (Desktop and Server versions)

Known issues for Ubuntu 16.04:

currently impossible to boot a Xen hypervisor from grub in UEFI mode. However the package does not detect this and will set the default boot to Xen mode.

So for any machine in UEFI mode, do not install the Xen hypervisor (or enable legacy mode first).

For Dell computers the steps to change boot mode are below:

  • Restart the computer and press F2 (this key is for Dell computer, others might differ) while starting up. This enters the setup program. 
  • Select the Boot tab and change to Legacy Boot (i.e., disable UEFI mode), Secure Boot Off. After restarting, this shows the boot sequence and the order can be changed using +/- keys.

(in my case, if I choose Legacy Boot, the Secure Boot automatically turned off. but it is better to double check Secure Boot is turned off.

  • I then change the boot order to. CD/DVD. USB drive. Hard drive.


How to Enable Secure Shell (SSH) on Ubuntu Server 16.04 LTS

SSH service is not enabled by default in Ubuntu Desktop and Ubuntu Server, but you can easily enable it just by one command.

Note: If you have installed OpenSSH Server when you install your Ubuntu Server (i.e., the software selection process page after your finish your installation of Ubuntu Server base system), just skip this post. 

Log into Ubuntu server and run the command:

sudo apt-get install openssh-server

It installs OpenSSH server, and ssh remote access will be automatically enabled.

You can check its status by the command: sudo service ssh status

OpenSSH is a FREE version of the SSH connectivity tools that technical users of the Internet rely on. Users of telnet, rlogin, and ftp may not realize that their password is transmitted across the Internet unencrypted, but it is. OpenSSH encrypts all traffic (including passwords) to effectively eliminate eavesdropping, connection hijacking, and other attacks. Additionally, OpenSSH provides secure tunneling capabilities and several authentication methods, and supports all SSH protocol versions.

You can change some settings (e.g., the listening port, and root login permission) by editing the configuration file bye the command:

sudo nano /etc/ssh/sshd_config

Remember to apply the changes by restarting or reloading SSH by the command:

sudo /etc/init.d/ssh restart