learning python by individual training courses. How to install Python on linux debian ubuntu, python programming courses by RasadaCrea - Grenoble Lyon France

Skip header | smaller text web accessibility bigger text web accessibility | easy print web main content share this web document with a partner

How to properly install the Python programming language source code

learning python programming, python courses, python hands-on training, python individual courses

Installation of Python under Debian GNU/Linux, Ubuntu

Python is already installed in Debian Linux distributions, such as Ubuntu. However, it is possible to install a specific version, available from the Ubuntu distribution, with the apt-get command. We can check the names of Python (Ubuntu pre-built) available packages with the apt-cache command. However this way of installing additional Python versions can be a source of potential problems (conflicts with the original installed version of the OS distribution). Therefore be warned on this.

$ apt-cache search python | less
$ sudo apt-get install pythonX.X

Rather, it is preferable to proceed with an installation of the wished version Python source code. To install the latest versions of Python from the source code, we can upload it from FTPPython by selecting the desired version number. Download the file .tgz and run the usual sequence of operations configure, make, make altinstall with administrator rights. We must also determine and install dependencies for the wished Python version with the Ubuntu command apt-get build-dep. In addition, before compiling the new Python source code, we must also install readline. In the following example we proceed with the installation of the Python version 2.7.2:

$ sudo apt-get build-dep python
$ sudo apt-get install libreadline-dev
$ wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
--2012-01-18 20:33:32--  http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
Resolving www.python.org... 82.94.164.162
Connecting to www.python.org|82.94.164.162|:80... connected.
HTTP request sent, awaiting response... 200 OK
...
$ tar xfz Python-2.7.2.tgz
$ cd Python-2.7.2
Python-2.7.2$ ./configure
checking MACHDEP... linux2
checking EXTRAPLATDIR...
checking for −−without−gcc... no
...
Python-2.7.2$ make
gcc -pthread -c -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3
    -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE
    -o Modules/python.o ./Modules/python.c
...
Python-2.7.2$ sudo make altinstall
  (rather than "sudo make install" to not interfere with the OS pre-installed version)
/usr/bin/install −c python /usr/local/bin/python2.7.2
...
Python-2.7.2$ cd ..
$ sudo rm -r Python-2.7.2*
$ which python
/usr/bin/python
$ python
Python 2.6.5 (#6, Apr 16 2010, 14:23:17)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[Ctrl+D]
$ which python2.7
/usr/local/bin/python2.7
$ python2.7
Python 2.7.2 (default, Jan 18 2012, 16:23:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Indeed, while launching this new version of Python, we could see an import error package for readline from the launching file .pythonstartup (which is why we compiled with the libreadline-dev dependency). In any case such an error would not prevent us to use this new Python version, freshly installed.

Tip

GCC error and missing libraries at compilation

In case of any compilation error involving the gcc library (error: Setup script exited with error: command 'gcc' failed with exit status 1) or any error for missing libraries while trying installing third parties Python packages, such for example setuptools, there might have lacked specific libraries at the Python source code compilation. It's important therefore to make sure the installation of those Python dependencies are done before compiling a specific version X.X of the Python source code. We should also ensure that the library development readline is present. In most cases the following two lines of dependencies installation satisfy our needs:

$ sudo apt-get build-dep pythonX.X
$ sudo apt-get install libreadline-dev

How to protect the pre-installed Python of the OS distribution

It is important not to modify (or destroy) the Python version installed by the Ubuntu Linux distribution, because there are dependencies to be respected within the OS distribution itself. In addition to the above example making use of make altinstall instead of make install, there are many ways to install a new version of Python in a "closed" environment without interfering with the primary installation of the OS distribution.

For example, here's how to install Python under /opt by creating a "closed" environment (/opt is indicated for installations of third party packages, which are not part of the official OS distribution, which installs rather in /bin, /usr/bin). By installing from the source code, each Python version would be entirely contained in a directory under /opt. If ever you want to delete the installed version, all what would be needed is to delete the corresponding directory and any related symbolic link. Let's install a new version of Python by starting with installation of needed dependencies first:

$ sudo apt-get build-dep python3.2
$ sudo apt-get install libreadline-dev
$ cd /opt/
/opt$ sudo wget http://www.python.org/ftp/python/3.2.2/Python-3.2.2.tgz
--2012-01-18 19:41:21--  http://www.python.org/ftp/python/3.2.2/Python-3.2.2.tgz
Resolving www.python.org... 82.94.164.162
Connecting to www.python.org|82.94.164.162|:80... connected.
HTTP request sent, awaiting response... 200 OK
...
/opt$ sudo tar xfz Python-3.2.2.tgz
/opt$ cd Python-3.2.2
/opt/Python-3.2.2$ sudo ./configure --prefix=/opt/python3.2.2
checking for --enable-universalsdk... no
checking for --with-universal-archs... 32-bit
checking MACHDEP... linux2
checking machine type as reported by uname -m... i686
checking for --without-gcc... no
checking for gcc... gcc
...
/opt/Python-3.2.2$ make
gcc -pthread -c  -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
    -I. -IInclude -I./Include -DPy_BUILD_CORE -o Modules/python.o ./Modules/python.c
...
/opt/Python-3.2.2$ sudo make altinstall
 (rather than "sudo make install" to not interfere with the OS pre-installed version)
/usr/bin/install -c python /opt/python3.2.2/bin/python3.2m
if test "3.2" != "3.2m"; then       ...
/opt/Python-3.2.2$ cd ..
/opt$ sudo rm -r Python-3.2.2*
/opt$ sudo ln -s /opt/python3.2.2/bin/python3.2 /usr/local/bin/python3.2
/opt$ which python3.2
/usr/local/bin/python3.2
/opt$ which python2.7
/usr/local/bin/python2.7
/opt$ which python
/usr/bin/python
/opt$ python3.2
Python 3.2.2 (default, Jan 18 2012, 19:52:21)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> [press Ctrl+D to get back to the command prompt]

Individual training and Python learning programming courses by RasadaCrea

All Python programming courses taught by RasadaCrea are individuals and personals. Contact RasadaCrea for more information about creating your personalized Python programming training plan, as well as for prices and organization of the Python learning programs.