16

So I have Debian machine for my Django production server. I need to install second python (2.7.1) to use with virtualenv. But it always write I don't have some modules, then I have to search manually, apt-install them and rebuild. Is there either a way to resolve the dependencies for building, or pre-compiled .deb with python 2.7.1 for Debian Squeeze? Sorry if this is much of a noobie question, I googled, honestly.

creitve
  • 763
  • 4
  • 12
  • 29

5 Answers5

21

Get the Python 2.7.1 sources and compile it manually:

configure --prefix=/path/to/python-2.7
make; make install
  • I'd also suggest installing various libraries before compiling: http://stackoverflow.com/a/4047583/211197 This will be helpful if it's necessary to install setuptools/pip later. – maksimov Sep 24 '12 at 10:54
  • 4
    Why not `make altinstall`? – timss Feb 08 '13 at 14:06
12

Python 2.7 is available for wheezy (testing), so you should be able to install it by adding the testing repository and doing some APT pinning.

1) add the repository in /etc/apt/sources.list

deb http://ftp.us.debian.org/debian testing main contrib non-free

2) do the actual pinning in /etc/apt/preferences

Package: *
Pin: release n=testing
Pin-Priority: 100

A Pin-Priority of under 500 basically means that no packages from testing are installed automatically, so you won't have problems with other packages.

3) install python2.7 from testing:

aptitude -t testing install python2.7

(or apt-get if you don't have aptitude)

  • 12
    This should not be done for multiple reasons. Packages from different Debian releases should not be mixed. The testing/unstable branches are working on changing the default python version to 2.7 for the Wheezy release. Squeeze is ment to use 2.6 so if you replace it with 2.7 you risk breaking everything on the system that uses python. – Arrowmaster May 06 '11 at 09:12
  • @Arrowmaster: I'd rather say that this is unsupported than that this should not be done. *If it breaks, you get to keep both pieces.* – StackExchange saddens dancek May 06 '11 at 10:45
  • 2
    Right but it's easier to just tell people that wouldn't know where to begin to fix it that it just shouldn't be done. – Arrowmaster May 06 '11 at 19:22
  • can you elaborate on how this would be dangerous if you are not using update-alternatives to change the default python to 2.7? As far as I understand, python2.7 would simply exist as an additional package when pinning; it wouldn't change the default python. Am I missing something here? – jorelli Oct 03 '11 at 18:33
  • @jorelli if you're careless you might accidentally install wrong versions of libraries you already have, or something (if you fail pinning). Probably the greatest danger is in changing the default python, as you say. – StackExchange saddens dancek Oct 03 '11 at 18:57
  • 10
    I installed python 2.7 on my Debian Squeeze according to these instructions plus configuration using update-alternatives, and as Arrowmaster warned, it did break! The next apt-get update errored out with python errors; something like python is not debian_default. I switched back to python 2.6 but still had the same error?!? I eventually had to uninstall 2.7 and hack the /usr/bin/python link to get it to work again. – Nostradamnit Nov 02 '11 at 08:04
  • Same experience as @Nostradamnit. Avoid this. – maksimov Sep 24 '12 at 10:19
9

Here is two methods for Debian GNU/Linux 6.0.7 (on 18/07/2013):

The classic

Install dependencies

aptitude -y install build-essential python-pip libmysqlclient-dev libadns1-dev \
 python-dev libreadline-dev libgdbm-dev zlib1g-dev libsqlite3-dev \
 libssl-dev libbz2-dev libncurses5-dev libdb-dev 

Download python

cd /tmp
wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.xz
unxz -c Python*xz | tar xpf -

Compile

cd Python*
./configure  --prefix=/opt/python2.7.5 --enable-shared
make

Install

make install
echo "/opt/python2.7.5/lib" >  /etc/ld.so.conf.d/libpython2.7.conf
ldconfig

Test

/opt/python2.7.5/bin/python -c "print('Ok')" 

Upgrade pip virtualenv

easy_install pip
pip -v install --upgrade distribute==0.7.3
pip -v install --upgrade virtualenv==1.9.1

Create an user and its virtualenv

adduser user_app --home /opt/user_app
su user_app
virtualenv --no-site-packages --verbose -p /opt/python2.7.5/bin/python $HOME

Test again

su user_app
cd 
source bin/activate
python -c "import sys; print sys.version"

The "pythonic"

Use the package pyenv.

 pyenv install 2.7.5
Yohann
  • 6,047
  • 3
  • 26
  • 33
  • I think I'd skip adding the custom Python install to the ld.so.conf cache. My approach would be to create a 'wrapper script' (/usr/local/bin/python2.7) which would : export LD_LIBRARY_PATH=opt/python2.x.x/lib and then call /opt/python-2.x.x/bin/python "$@" etc. This at least keeps everything isolated. – David Goodwin Feb 05 '14 at 10:21
2

Installing a chroot-environment with debootstrap could be also a fast and secure solution. It uses about 300mb

debootstrap wheezy /opt/debian7
chroot /opt/debian7
apt-get install python2.7
juwens
  • 3,729
  • 4
  • 31
  • 39
0

You can install and switch python versions using pythonbrew I installed python 2.7.3 and python 2.7.9 in Debian 6 and Debian 7 and works fine. You can follow this tutorial pythonbrew howto

Fernando Byn
  • 97
  • 1
  • 2
  • 7