35

I'm still new to Linux, so I'm still trying to understand where executables and their libraries are and how to install packages, so I have Debian Wheezy 7.3 which has these Python versions:

  • Python 2.7.3 (default)
  • Python 2.6.8

So in the directory /usr/bin/ there are these files that I can call from the terminal:

  • python (which is a link to python2.7)
  • python2 (also a link to python2.7)
  • python2.6 (Python 2.6.8 executable)
  • python2.7 (Python 2.7.3 executable)

and in /usr/lib/, the main folders:

  • python2.6
  • python2.7

Currently the latest version of Python is 2.7.6 which I want to install, but I don't know how, I tried using apt-get:

apt-get install python

it outputs python is already the newest version..

So how can I install the latest version of Python ? on the Python download page there is the source tarball, how can I use that to install it separately like having another folder in /usr/lib/ like python2.7.6 and make the python link in /usr/bin/ point to the new executable, or maybe upgrade the current version if it won't break anything.

2
  • You could backport Python 2.7.6 from testing/unstable and replace your curent 2.7.3 with it. This will most likely not cause you any problems. If it does, you can easily revert back. Jan 19, 2014 at 15:48
  • I didn't even install it, fresh install of Debian and type "python" from the command line and I'm in the python3 shell.
    – frozenjim
    Jul 24, 2015 at 11:00

3 Answers 3

27

Ok after a lot of searching I decided to build Python from source, so I downloaded the compressed source tarball from the Python download page, now we need to install the build-essential package to be able to compile the source files:

apt-get install build-essential

also we need to install these development packages which are required for some Python modules:

libbz2-dev
libsqlite3-dev
libreadline-dev
zlib1g-dev
libncurses5-dev
libssl-dev
libgdbm-dev

next we extract the downloaded source file:

tar zxf Python-2.7.6.tgz

then we cd into the extracted directory:

cd Python-2.7.6

and follow the instructions on the Python docs:

./configure --prefix=/opt/python
make
make install # <- in the docs but don't execute: use checkinstall

I chose to install it under the directory /opt/python which I created before, also I used the checkinstall package to create a .deb file so I can uninstall easily later, so we install it first:

apt-get install checkinstall

and substitute the last line make install with:

checkinstall

now I have a new python installation in /opt/python/lib/python2.7 and the binary file is in /opt/python/bin/python2.7.

now I can run in the command line /opt/python/bin/python2.7 to use this Python version, also we can make a link to this executable so we don't type the full path, I named it python2.7.6.

ln -s /opt/python/bin/python2.7 /usr/local/bin/python2.7.6

also the directory /opt/python/bin will contain later other executables like pip and virtualenv or any other modules you install so we can add it to the PATH environment variable, edit ~/.profile and add this line:

PATH="$PATH:/opt/python/bin"

and run:

source ~/.profile

I appended the path to the end because it contains executable names like the ones in /usr/bin like python, python2 and python2.7 so we keep the higher priority for /usr/bin.

3
  • Now, would you happen to know how to configure your new python to see modules that were installed via apt-get?
    – elsurudo
    Jan 27, 2016 at 21:22
  • Unfortunately for Python 2.6, compiling from source doesn't work properly on Debian 8 (Jessie) but I found, at least for a Raspberry Pi, that I was able to use older packages from packages.debian.org. The order, assuming build-essentials are already in place is something like libdb, python2.6-minimal, python2.6, libpython2.6, python2-6-dev but dpkg -i will tell you what's needed. Apr 14, 2016 at 10:29
  • 1
    @elsurudo That would not make sense really, because the modules might well be unsupported on your new version. If you want full control over your Python (as you do when compiling from source), you will want to install modules separately, too.
    – tripleee
    Oct 20, 2016 at 8:01
16

You probably are looking for virtualenv or pyenv or some other non-system-wide method to install Python. The method using APT (Advance Package Tool) and dpkg, ensures that all parts of the system are working in harmony, so you maybe want to install python in a separated path, hidden of all the other programs that you can call at will, which is the purpose of pyenv/virtualenv. This answers how to install the latest version of python without breaking the system.

BTW, you can check out the latest version of python that Debian in madison, while the latest version of python 2 at the date is the one you pointed out:

➜  ~  apt-cache policy python
python:
  Installed: 2.7.5-5
  Candidate: 2.7.5-5
  Version table:
 *** 2.7.5-5 0
        500 http://ftp.us.debian.org/debian/ testing/main i386 Packages
        100 /var/lib/dpkg/status

(pythonbrew is not longer maintained).

2
  • ok I installed pip and virtualenv as root, so how can I create a virtual environment with a new Python version ? as far as I know we need the new version to be installed first right ?
    – Pierre
    Jan 19, 2014 at 16:27
  • 1
    @Peter I think that the first step is pyenv install 2.7.6. You can check all versions using pyenv install -l
    – Braiam
    Jan 19, 2014 at 16:57
-2

you can use:

aptitude install python3.5  

or, if you haven't installed aptitude, then you can install it using:

apt-get install aptitude
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .