Sunday, February 28, 2016

How to Compile the Latest Development Version of GNU Package - Findutils

In this post, I will discuss how to download and compile the latest development version of findutils GNU package. Findutils package consists of find, locate, updatedb, and xargs. More info and stable releases of this package can be found at http://www.gnu.org/software/findutils/. This post intends to help people contribute to free and open source packages and programs, as GNU/Linux and GPL have changed the world significantly, and we can be all part of the movement. Although I will only discuss the Findutils package here, most of other GNU packages can be obtained in the similar manner.

The instruction here will be based on Ubuntu 14.04 LTS 64bit version.
We need to install necessary packages first as usual:
$ sudo apt-get install -y autoconf automake git gettext m4 dejagnu autopoint bison texinfo

If you are trying to build the package on Mac OS X, you will need to install development tools first.
$ sudo xcode-select --install

Then you would want to install homebrew.
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then, you will need to install the necessary packages with
$ brew install autoconf automake git gettext dejagnu bison texinfo

Finally, you will need to create a symlink of autopoint
$ sudo ln -s /usr/local/Cellar/gettext/0.19.7/bin/autopoint /usr/local/bin/autopoint

Now, you are ready to follow along for both Linux and Mac OS X.

We will download the latest development version using git
$ git clone git://git.sv.gnu.org/findutils

When complete, go to the findutils directory and run bootstrap
$ cd findutils && ./bootstrap

It will probably complain about gettext version.
autopoint: *** The AM_GNU_GETTEXT_VERSION declaration in your configure.ac
               file requires the infrastructure from gettext-0.19.3 but this version
               is older. Please upgrade to gettext-0.19.3 or newer.
autopoint: *** Stop.

To see what version of gettext is currently installed, we can run
$ gettext --version
I am getting 0.18.3 as of now. This is the latest version available from the Ubuntu repository. We can manually install the newer version by building the executable from the source.

Let's then download and build the latest gettext!
$ cd .. && wget https://ftp.gnu.org/gnu/gettext/gettext-latest.tar.gz

Unarchive the source files
$ tar xfz gettext-latest.tar.gz

Let's configure it!
$ mkdir gettext-0.19.7/build && cd gettext-0.19.7/build
$ ../configure

When complete, we can compile it
$ make -j 2
where the number 2 represents the desired # of cores to use when compiling.

By the way, if you intend to debug the source code, you may want to define a macro DEBUG
$ make -j 2 CFLAGS="-D DEBUG -g"
which sets the option -D DEBUG and -g to the compiler.

After some time, it should build successfully. We are now ready to install the latest get text on the system.
$ sudo make install

Finally, we should now have the latest version of gettext
$ gettext --version
which returns 0.19.7 as expected.

OK, let's go back to findutils and continue our process of bootstrapping
$ cd ../../findutils/ && ./bootstrap

The last step is to simply configure and compile!
$ mkdir build && cd build
$ ./configure
$ make -j 2

When complete, we can execute the new program and verify if it works
$ find/find --version
$ xargs/xargs --version
$ locate/locate --version

They all should say version 4.7.0-git as of today. The latest stable release findutils version is 4.6.0 as can be seen from http://ftp.gnu.org/pub/gnu/findutils/.

You could install it on the system if you want, but I would not recommend so, as it is not a stable release version yet.


Thursday, February 18, 2016

Setup SSH Login Email Alert on Ubuntu 14.04 or Debian

Anytime one enables ssh server, the machine will be at great risk from random attempts all around the world. It may be wise to setup an alert mail when someone logs into the machine remotely. In this post, we will look into how to do so.

I will make use of mail.mailutils program to send emails out here, but there are many other alternative mail out packages.
$ sudo apt-get install -y mailutils postfix
When prompted with postfix configuration, just choose the default setting, which is Internet Site.

Next, create a bash script file that will be executed when someone logs in remotely. In this example, I will place it in the /etc/ssh directory.
$ sudo vim /etc/ssh/ssh_alert.sh

Add the following content to the file
#!/bin/bash
# replace with sender's email address
sender="sender_address@some_mail.com"
# replace with recipient's email address
recipient="recipient_address@some_mail.com"
time=$(date)
if [ "$PAM_TYPE" != "close_session" ]; then
# replace with host name
host="ubuntu-server"
subject="SSH Login: $PAM_USER from $PAM_RHOST on $host at $time"

message="SSH login $PAM_USER from $PAM_RHOST at $time on $host"
echo "$message" | mail.mailutils -r "$sender" -s "$subject" "$recipient"
fi

Next, enable execute
$ sudo chmod u+x /etc/ssh/ssh_alert.sh

Now, we need to make sure that the script actually works. To do so, simply run it
$ sudo /etc/ssh/ssh_alert.sh

If you see a message saying that the email was sent successfully, then you can set it up so that a ssh remote login will execute the script and allow the remote login only if the email was successfully sent. To see whether the execution returns successful, type in
$ echo $?
The output of 0 means successful.

When the mail out is successful, open up /etc/pam.d/sshd
$ sudo vim /etc/pam.d/sshd

Add the following line to /etc/pam/sshd
session required pam_exec.so seteuid /etc/ssh/ssh_alert.sh

That's it. You should now receive the email when someone logs into the server through ssh.

Mount NFS Directory from Ubuntu and Mac OS X Clients

Here, I assume that you have set the NFS server following the previous post. In this post, I will show how the client machine can be configured to connect to the NFS directory.

Make sure that the client's IP address is specified in the server's /etc/export file. In the previous post, this should be 192.168.0.101. Also, I am going to assume that the NFS server IP address is 111.111.111.111.

Instructions for Ubuntu Client
First, install necessary package
$ sudo apt-get install -y nfs-common

Create a mount point
$ sudo mkdir -p /mnt/nfs

Mount the NFS directory
$ sudo mount 111.111.111.111:/var/nfs /mnt/nfs

That's it. To unmount, simply type in
$ sudo umount /mnt/nfs


Instructions for Mac OS X Client
First, create a mount point
$ sudo mkdir -p /private/nfs

Mount the NFS directory
$ sudo mount -o resvport,rw -t nfs 111.111.111.111:/var/nfs /private/nfs

That's it. To unmount, simply type in
$ sudo umount /private/nfs


How to Setup Network File System (NFS) on Ubuntu 14.04

In this tutorial, I will cover how to setup Ubuntu 14.04 (either desktop or server edition) as a NFS server.

First, install nfs-kernel-server
$ sudo apt-get install -y nfs-kernel-server

Next, create a directory which will be used as a network file system. In this example, I will use /var/nfs folder
$ sudo mkdir /var/nfs

Change the owner of the directory
$ sudo chown nobody:nogroup /var/nfs

Change the permission of the directory if you want to allow the clients to have access to read/write
$ sudo chomod 777 /var/nfs

Modify the export config file to let it know which directory will be used as NFS
$ sudo vim /etc/exports

Add the following line to the /etc/exports file
/var/nfs 192.168.0.101(rw,sync,no_subtree_check)

Here, I am assuming that this directory will only be accessed by 192.168.0.101. You may want to change this address if you want. Also, make sure to set your DHCP to reserve this IP address to the desired client.

Create NFS table that holds exports
$ sudo exportfs -a

Finally, simply start up the server
$ sudo service nfs-kernel-server start

That's it. In the next post, I will discuss how to connect to NFS directory from the client side.

By the way, you may want to note the server's IP address
$ ifconfig

This IP address will be needed for a client to connect to, which will be discussed din the next post.

Friday, January 29, 2016

How to Compile Fuego 1.1 on Ubuntu 14.04 LTS x64

My dad loves the game of GO, so I decided to let him play it vs AI. I first installed GNU GO on his computer and let him play against it; after a couple of times he played, he complained that GNU GO cannot even count accurately. So I figured I should provide him a better GO AI. That is how I decided to go for Fuego.

To install Fuego, download its source files
$ wget http://downloads.sourceforge.net/project/fuego/fuego/1.1/fuego-1.1.tar.gz

Unzip the source files
$ tar xfc fuego-1.1.tar.gz && cd fuego-1.1

Before configure, we need to install Boost library and build-essential
$ sudo apt-get install libboost-all-dev build-essential -y

Now, let's configure
$ ./configure

You will probably see some error saying 
configure: error: Could not find a version of the library!

The reason is that it cannot find the boost library, so we try to configure with the option
$ ./configure --with-boost-libdir=/usr/lib/x86_64-linux-gnu

The configure should have completed successfully. Let's compile.
$ make

You will probably encounter compilation error saying
GtpEngine.cpp:352:33: error: expected unqualified-id before numeric constant
         xtime_get(&time, boost::TIME_UTC);


We can fix this by replacing all instances of TIME_UTC with TIME_UTC_. Let's fix one by one first. Open the file
$ vim gtpengine/GtpEngine.cpp

Go to line 352 by entering :352 in vim and replace TIME_UTC with TIME_UTC_. Save and exit.

Let's compile again
$ make

This time you will see some error saying
SgPointSetUtil.h:67:25: error: reference 'm_pointSet' cannot be declared 'mutable' [-fpermissive]
     mutable SgPointSet& m_pointSet; // allow temp objects to modify


Let's solve this by removing 'mutable' in the file.
$ vim smartgame/SgPointSetUtil.h

After removing the keyword in line 67, save, and exit and give it another try.
$ make

You will encounter another error
GoGtpEngine.cpp:381:46: error: 'class boost::filesystem::path' has no member named 'native_file_string'
                            << m_sentinelFile.native_file_string() << "'";


This one is easy. If you search for native_file_string() method for Boost in Google, you will see that this method has been deprecated and replaced by string(). So, let's do that.
$ vim go/GoGtpEngine.cpp

In fact, you will have probably encountered another TIME_UTC error, which you can fix now, right? It is in the same file, so it should be easy.

Let's continue.
$ make

You will encounter another error saying
FuegoMain.cpp:65:55: error: invalid conversion from 'bool (*)(const string&) {aka bool (*)(const std::basic_string<char>&)}' to 'boost::enable_if_c<true, void>::type* {aka void*}' [-fpermissive]
     return path(programPath, boost::filesystem::native).branch_path();


This is again due to boost deprecation. One of the easiest ways to fix it for the moment is to replace line 65 with
return boost::filesystem::current_path();

This is not a remedy because this will only work if the executable is called from the same directory. However, let's just focus on compiling at the moment.

By the way, we will also need to include the boost::filesystem header file for the above quick fix to work. Insert the following at, say, line 11 of the FuegoMain.cpp file.
#include <boost/filesystem.hpp>

Let's continue.
$ make

We will encounter another complaint for native_file_string error, so let's fix it and compile again.
$ make

Lastly, we see another error saying
/usr/bin/ld: ../smartgame/libfuego_smartgame.a(libfuego_smartgame_a-SgUctSearch.o): undefined reference to symbol 'pthread_mutexattr_settype@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line


As the error suggests, it is complaining because pthread library is missing. The easiest way to fix this is to run the g++ command with -lpthread option
$ cd fuegomain
$ g++ -g -O2 -DNDEBUG -Wall -Wextra -L/usr/lib/x86_64-linux-gnu  -o fuego fuego-FuegoMain.o fuego-FuegoMainEngine.o fuego-FuegoMainUtil.o ../gouct/libfuego_gouct.a ../go/libfuego_go.a ../smartgame/libfuego_smartgame.a ../gtpengine/libfuego_gtpengine.a -lboost_program_options -lboost_filesystem -lboost_system -lboost_thread -lpthread
$ cd ..

Let's continue!
$ make

You will most likely successfully compiled it finally! By the way, in order to save some compilation time, you may want to add in -j 2 option for make command, telling it to use 2 cores.


*** EDIT ***
After I created the post, I realized that Fuego website actually has a nice tutorial on building from the source files for Mac OS X and Linux. Furthermore, the latest Fuego source files can only be obtained via svn; these source files fix all of the problems mentioned above, except for the last one. In any case, I have created a script file that will build Fuego from the latest source files and install gogui and openjdk as well. Copy the script below and paste into a new file, say build_fuego_on_Ubuntu_14.04_x64.sh

#!/bin/bash
# this script attempts to compile and install fuego on Ubuntu 14.04 LTS x64


apt-get install -y subversion build-essential autoconf openjdk-7-jdk libboost-all-dev


sudo -u $SUDO_USER bash -c "svn checkout svn://svn.code.sf.net/p/fuego/code/trunk fuego"

cd fuego

#sudo -u $SUDO_USER bash -c "export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/"

sudo -u $SUDO_USER bash -c "autoreconf --force --install"
sudo -u $SUDO_USER bash -c "./configure --with-boost=/usr/lib/x86_64-linux-gnu/ --with-boost-libdir=/usr/lib/x86_64-linux-gnu/"
sudo -u $SUDO_USER bash -c "make -j 2"

cd fuegomain
sudo -u $SUDO_USER bash -c "g++  -g -O2 -DNDEBUG -Wall -Wextra -L/usr/lib/x86_64-linux-gnu/  -o fuego fuego-FuegoMain.o fuego-FuegoMainEngine.o fuego-FuegoMainUtil.o ../gouct/libfuego_gouct.a ../go/libfuego_go.a ../features/libfuego_features.a ../smartgame/libfuego_smartgame.a ../gtpengine/libfuego_gtpengine.a -lboost_program_options -lboost_filesystem -lboost_system -lboost_thread -lpthread"

cd ..
sudo -u $SUDO_USER bash -c "make -j 2"
cd ..
sudo -u $SUDO_USER bash -c "wget http://downloads.sourceforge.net/project/gogui/gogui/1.4.9/gogui-1.4.9.zip"
sudo -u $SUDO_USER bash -c "unzip gogui-1.4.9.zip"
cd gogui-1.4.9/lib

sudo -u $SUDO_USER bash -c "java -jar gogui.jar"


To run this file, simply set the execution flag and execute with sudo
$ chmod u+x build_fuego_on_Ubuntu_14.04_x64.sh
$ sudo ./build_fuego_on_Ubuntu_14.04_x64.sh
 
That's it!

Thursday, January 28, 2016

Function and Alias in Bash

People are lazy in general. Sometimes, it is simply annoying to type long commands in bash with lots of options. Let us see how we can use bash functions and alias to relieve stress of typing too much.

alias helps us to shorten a long command. On Linux, I use $ ls --color=auto -CF commands quite a lot. However, it becomes annoying typing the command repeatedly. What one can do is create an alias to shorten this as
$ alias ls='ls --color=auto -CF'

Now, when you type the command $ ls, it will essentially execute $ ls --color=auto -CF command. In many cases, you may want to write the alias command in ~/.bashrc for Linux and ~/.bash_profile for Mac OS X to be executed every time you open up the terminal. In fact, on my system (Ubuntu 14.04 LTS), the default ~./bashrc file already contains some alias commands as below:
alias ls='ls --color=auto'
...
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'


Hence, the command $ l will actually execute $ ls -CF, which in turn will execute $ ls --color=auto -CF, i.e., $ l is a nested alias for $ ls --color=auto -CF.

By the way, the utility ls is a bit different on a Mac OS X system, since it is a BSD version of ls whereas on Linux it is GNU version of ls. The two are very similar but some of the options are different. In any case, the use of alias is identical, so I will not go into the difference of BSD ls and GNU ls here.

Next up is bash function. Although alias can shorten the commands quite significantly, it does not support arguments. That is when the bash function comes very handy. Consider the example below.

 To search for files and directories, I often use the command $ find / -name name_to_search 2> /dev/null. Again, typing this command repeatedly makes my fingers very tired. I'd like to create an alias for this, but I can't because I need to supply the argument name_to_search and alias does not support it. So, I will need to make a function here.
$ f() { find / -name $1 2> /dev/null; }

Now if I run $ f bash, it will execute the command $ find / -name bash 2> /dev/null. Note that $1 is the first argument, $2 is the second argument, and so forth. For example, the function below will take two arguments.
$ ff() { find / -name $1 -type $2 2> /dev/null; }

Now, if I execute $ ff bash f, this will execute $ find / -name bash -type f 2> /dev/null. Again, you can save these functions in the ~/.bashrc for Linux for ~/.bash_profile for Mac OS X to load the function automatically at the startup of the terminal.

Tuesday, January 26, 2016

How to Build and Install Numpy, Scipy, and Matplotlib Packages on Ubuntu

This is a continuation from the last post on How to build and install the latest version of Python on Ubuntu. If you are simply looking to install Python packages, such as numpy, scipy, and matplotlib, on the default system Python (which is 2.7.6 for Ubuntu 14.04 LTS), the answer is quite simple:
$ sudo apt-get install python-numpy python-scipy python-matplotlib -y

The command will install the packages in the /usr/lib/python2.7/dist-packages directory. With this method, however, you can import those packages from the system default Python (version 2.7.6) but not from newly installed Python (version 2.7.11). Thus, in this post we will go over the steps to build and install the Python packages for the newly installed version.

First, we need to install setuptools package. We will need to download the script and run it.
$ wget https://bootstrap.pypa.io/ez_setup.py && sudo python ez_setup.py

Next, we need to install pip package. We will download the archive, extract, and run the script
$ wget https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
$ tar -xf pip-8.0.2.tar.gz
$ cd pip-8.0.2
$ sudo python setup.py
$ cd ..

Finally, use pip to install the packages
$ sudo pip install numpy scipy matplotlib

It will take quite some time to build, so please be patient. The packages will be installed in /usr/local/lib/python2.7/site-packages directory.

How to Build and Install the Latest Version Python on Ubuntu

Ubuntu 14.04 LTS comes with Python 2.7.6 and 3.4.0 pre-installed. However, sometimes you may wish to install the most recent version, say 2.7.11 as of now along with the default system python. Unfortunately, Ubuntu's repository does not have the most recent version, so you will need to manually build Python from source files. This post will show you how to do so. Note that in this example, I will be installing 2.7.11, but you may want to use a different version as desired.

Before we do anything, let us examine where the system default Python is located and what version it is

$ which python
/usr/bin/python

The which  command shows the file which will be executed upon running the command $ python in shell. This is the default Python file pre-installed on the system. To check its version, run

$ python --version
Python 2.7.6

So, the default version that comes with Ubuntu 14.04 LTS is indeed 2.7.6 and is located in the /usr/bin directory.

OK, let us now start the installation process for the latest version 2.7.11. First, download the Python source archive file
$ wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz

Extract the files
$ tar -xf Python-2.7.11.tgz

Enter the directory
$ cd Python-2.7.11

Before we configure, we must install quite a few packages necessary to build Python successfully. Although not all of the packages below are required, I recommend getting them because they will be needed for installing packages later on, such as numpy, scipy, and matplotlib, which I will cover in the next post.
$ sudo apt-get install -y build-essential gcc-multilib g++-multilib libffi-dev libffi6 libffi6-dbg python-crypto python-mox3 python-pil python-ply libssl-dev zlib1g-dev libbz2-dev libexpat1-dev libbluetooth-dev libgdbm-dev dpkg-dev quilt autotools-dev libreadline-dev libtinfo-dev libncursesw5-dev tk-dev blt-dev libssl-dev zlib1g-dev libbz2-dev libexpat1-dev libbluetooth-dev libsqlite3-dev libgpm2 mime-support netbase net-tools bzip2 libblas-dev liblapack-dev gfortran

Now, configure
$ ./configure

Then build
$ sudo make install

It will take some time to build and install. When complete, we verify whether Python is now updated to 2.7.11 version
$ python --version
Python 2.7.11

Great. So, the question is, did we just overwrite the system's default python? The answer is no; we actually installed the new version on the system side by side from the old one, so the old version is still intact. How do we figure? Simply run

$ which python
/usr/loca/bin/python

The output shows that the command $ python will now execute /usr/local/bin/python, which is different from the old one /usr/bin/python. To verify this, see if we can still run the old one

$ /usr/bin/python --version
Python 2.7.6

So, the old one is still there; if we need to run the old one, we just need to execute it by the full path. This is because the default $PATH environment looks something like this

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

The directory /usr/local/bin, where version 2.7.11 is installed, precedes the directory /usr/bin, where version 2.7.6 is installed. Thus, when we run $ python command, it will execute the Python file in /usr/local/bin, thus running the 2.7.11 version.

Thursday, January 21, 2016

Setup Latex Environment for Ubuntu

I personally use TexWorks for creating and editing TEX documents. I also often use IEEEtran.cls package along with many other related scientific packages, such as subfig, etc. This post will go over how to install these packages successfully in the Ubuntu environment.

First off, add the TexWorks repository
$ sudo add-apt-repository ppa:texworks/stable -y

Install TexWorks and lots of latex-related packages that are frequently used
$ sudo apt-get install -y texworks texlive-fonts-recommended texlive texlive-latex-extra texlive-math-extra texlive-pstricks texlive-science latex-beamer

Download IEEEtran.zip file from http://www.ctan.org/tex-archive/macros/latex/contrib/IEEEtran/ by
$ wget http://mirrors.ctan.org/macros/latex/contrib/IEEEtran.zip

Unzip the file to the latex package location
$ sudo unzip IEEEtran.zip -d /usr/share/texmf/tex/latex/IEEE

Let tex know that the package has been added manually
$ sudo texhash


Now, you should be able to run TexWorks and compile TEX files!

Tuesday, January 19, 2016

Installing Muliple Desktop Environments in Ubuntu

I love to try new things out. In that respect, I love trying different various desktop environments on Ubuntu.

Ubuntu comes default with Unity desktop, but you can install other desktop environments as well. To install Ubuntu desktop environment, simply run
$ sudo apt-get install -y --no-install-recommends desktop_environment_to_install

where desktop_environment_to_install can be any of the following corresponding to the desktop environment in parentheses:
ubuntu-gnome-desktop (GNOME)
kubuntu-desktop (KDE)
ubuntu-desktop (Unity)
ubuntu-mate-desktop (MATE)
xubuntu-desktop (Xfce)
lubuntu-desktop (LXDE)

When you have multiple desktop environments installed, you may be able to choose one at the log-in screen.