Tuesday, November 6, 2018

Python:: Installation on Ubuntu

Recently I had a requirement to install python in linux VM running on Ubuntu 18.04. Here is the list of steps, I followed to set it up.

1. Download the source code of an official Python release.

Download the "Gzipped source tarball" using the wget command. If wget is not installed, install it using OS package manager.

sudo yum install wget

wget https://www.python.org/ftp/python/2.6.6/Python-2.6.6.tgz

Once the file is downloaded, unpack it using the tar command. 

tar zxvf Python-2.6.6.tgz

zxvf -->  "unzip/extract verbosely from a file"

Switch to unzipped directory 

cd Python-2.6.6

2. Configure the build appropriately for our machine.

If ./configure and/or make commands used with the default settings, then use "make clean" to wipe out any work it did and start again.

./configure produces is another large, automatically generated script called Makefile. The Makefile contains instructions on how to achive various tasks like "build" or "make the executable called python" or "run the test suite". The Makefile script is executed with the make command. We can run make to execute the default task (i.e. the first one in the file) or, e.g., make test to run the "test" task.


If we need to install multiple versions side-by-side and point new installation to somewhere else, then run the ./configure command with --prefix argument. It's also useful, if we don't have root privilege to use sudo command.

./configure --prefix=/usr/local/lib/Python-2.6.6

To make Python code execution faster, use optimization. It will take long time to configure, but the resulting python binary interpreter is 10% faster at executing Python code.

./configure --enable-optimizations

3. Compile the software.

The default task for Python, like most packages, is to compile the software so running. So run make to execute the default task


4. Test the software to make sure it works properly.

Run the test suite to make sure that it all works as expected. The tests can take a long time to run so we can skip this step, if needed.

make test

5. Install the software.

Once it’s built, then install the software. make install convention will install under the /usr/local/ directory by default: they’ll put their executables in /usr/local/bin/, data files in /usr/local/share/, etc. The /usr/local/ directory is properly considered part of the operating system

sudo make install

This will create the directories and copy the compiled files into place 

sudo make altinstall

We use the altinstall target here in order to not overwrite the system’s version of Python. Please only use the altinstall target on make. Using the install target will overwrite the python binary. While this seems like it would be cool, there are big portions of the system that rely on the pre-installed version of Python.

6. Configure system to make sure the software can be used easily.

Create symbloic link for python binary into /usr/bin or /usr/local/bin/ as python2.6

sudo ln -fs /usr/local/lib/Python-2.6.6/Python /usr/bin/python2.6


sudo ln -fs /usr/local/lib/Python-2.6.6/Python /usr/local/bin/python2.6

We can also at add the directory to user $PATH rather than using symbolic links.

If we want to use the new install in just this shell session we can change $PATH temporarily. The $PATH variable contains a : separated list of directories to search for an executable when we run a command in the shell. The first executable file with the right name is the one that will be executed. The which command can do this search and tell which file will be executed or even give a list of all the options:

export PATH

To make this change permanent we need to change user shell configuration files. The name of these configuration files and whether or not they already exist can change from system to system but they are always in user $HOME directory and are called one or more of the following:


If any of those files already exist in user $HOME directory (files beginning with a . are considered "hidden" by most UNIX tools, we'll need to use ls -a instead of ls to see them) then we should edit that file. If not, .profile is probably a safe bet.

Ubuntu:: Unable to lock the administration directory (/var/lib/dpkg/)

I encountered an error, when I tried to use apt-get command in Ubuntu Linux command line.


E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

This error may be caused by the "Update Manager" trying to automatically refresh the list of packages in background, usually right after login, thus locking the directory.

In this case just wait few seconds (or more, if last update was long ago) for it to complete or launch Update Manager to check the status.

And check if 'Update Manager' running. If the error appears even though 'Update Manager' is not running, then remove stale apt lock file manually from "/var/lib/dgkg/lock" and force package reconfiguration.

Remove the apt lock file in the /var/lib/dpkg/ directory:

sudo rm /var/lib/dpkg/lock

Force package(s) to reconfiguration:

sudo dpkg --configure -a

Alternatively, delete the lock files in the /var/lib/apt/lists/ and cache directory as below:

sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock

Now apt update command will work without any issues.