Wednesday 13 June 2012

Latest Python on Crunchbang Linux

Introductory Spiel

 It's been an interesting week and a half for me. I've been writing a system script in Python for backing up some source code files, transferring them between various servers and then setting them up in the Mercurial version control system as repositories. Because of this mission, I have (re)discovered the wonderful usefulness of a number of Python modules, including:
  • subprocess: for setting up an ssh connection and port forwarding.
  • elementTree: for storing server logins and directory paths in XML.
  • paramiko: for ssh logins, executing BASH scripts and sftp transfers.

Anyway, the end result of all this was that I needed to test out some user and ssh logins with keys. (I'll also need to test HTTP password access to a Mercurial repository, done through Apache and mod_wsgi. But I digress!) I didn't want to mess up my nice, tidy Ubuntu install, so I thought: give me a virtual machine running a lightweight Linux with Python. I'll be able to mess that up all I like by setting up dummy user accounts!


After trying out a few lightweight distros I settled on Crunchbang Linux as the Linux to install in my VM. It's derived from Debian, a distro I used to like a lot. By default, it runs the lightweight Openbox window manager and uses only an awesomely low amount of RAM. So I installed Crunchbang 10, ran most of the options in the handy post-installation script, and then...

I realised it has the same problem I usually had with Debian, which is that its default Python installed version is rather old, i.e. 2.6.6 from over 16 months ago. Right, I thought, time to solve this issue. What I did was download and compile Python 2.7.3 from source, then make a shortcut to it.

Do the Python Dance

 Here are the steps I followed, gentle blog reader:

Switch to root-ish user:
sudo -i
(Enter password)

Install Debian packages for Python:
apt-get install build-essential zlib1g-dev libncurses5-dev
apt-get install libgdbm-dev libbz2-dev libreadline5-dev
apt-get install libssl-dev libdb-dev libxml2 libxslt1-dev
apt-get install python-setuptools python-dev python-pip
# If you know you'll inevitably use MySQLdb too:
apt-get install libmysqlclient-dev
pip install virtualenv

There is a problem with a libsqlite3 package version for Python. To solve it, start Synaptic and search for libsqlite3-0. (Note: this removes the Iceweasel browser, but this sucks anyway.)
Go to >Packages >Force version >3..7.3-1 (stable)
Click >Apply, and apply again

Then install the trouble-maker in the terminal:
apt-get install libsqlite3-dev

Browse to a directory, like /home/scott/downloads. Download the latest Python source, and unzip it.
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar xvf Python-2.7.3.tgz

I wanted to store my latest Python version in the /opt directory. So I moved it there.
mv Python-2.7.3 /opt
cd /opt/Python-2.7.3/

Then run the compile and install commands.(These can take a while.)
./configure --prefix=/opt/python27 --enable-shared
make
make install

Linux needs to find the new Python library object file. Test that you can run the Python 2.7 intrepreter with: 
LD_LIBRARY_PATH=/opt/python27/lib /opt/python27/bin/python

If this works, you will want to make it work permanently by:
(a) editing shell profile files:
vim ~/.profile (for a non-root user, vim ~/.bashrc instead)
add this:
export LD_LIBRARY_PATH=/opt/python27/lib

(b) Setting this in the config file so the system can find the library information:
vim /etc/ld.so.conf
add this:
include /opt/python27/lib

(c) Running this command:
ldconfig

(d) Quitting out of a shell, then starting it up again.Then type:
/opt/python27/bin/python
-you should see the Python 2.7 prompt!

Make a link (shortcut) to run Python 2.7.
ln -s /opt/python27/bin/python /usr/bin/python27
-then you can just type python27 to get the Python you need and love! :-D

Afterwards, it's a good idea to quit being root user, and as a normal user set up a virtualenv that uses your new Python version only. I created my virtualenv in my /home/scott/code/py folder, and named it very originally, env27.
cd ~/code/py
virtualenv -p /opt/python27/bin/python env27
cd env27
source bin/activate
python

Python 2.7.3 (default, Jun 13 2012, 22:26:54) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

1 comment: