apt-get install for different python versions

73,952

Solution 1

Python has got its own package managing facilities, in parallel to the one sets by the Linux distributions (including Ubuntu). The repository is the Pypi - Python Package Index, and packages are installed with pip or the easy_install script, which is part of Python's setuptools package.

As a rule of thumb, you should not use both the packages installed via pip/setuptools, and packages available to your distro (via apt-get, yum, urpmi, etc...) as they might conflict.

So, one of the less error prone way to deal with it is to have separate Python installs in your system - leave the python that came with the system for system scripts and such - on this python, make use of packages installed by your package manager only. And install other versions of Python (or even the same), to be run with "virtualenv"s - on these other install you install things with pip/setuptools only.

(And even if one opt to live boldly and not use virtualenvs, installing another python version on the same prefix (/usr, and even /usr/local) than your system's Python is a source to confusing errors and conflicts).

Note that the Debian - and Ubuntu - systems devised a way to run parallel official Python's in /usr, and to have apt-get to install Python packages to both Python versions at once. This mostly works, but they mess with Python's default directory hierarchy, and some applications fail to use Python in this way. (It is also a mess to find the module files themselves in a Debian or Ubuntu). So the above method apply as a recommendation even if your system do have more than one version of Python available on apt-get.

In short, once you have compiled your desired version of Python, do this:

  1. use your system's package manager to install "python-setuptools" and "python-virtualenv" (not sure if these are the actual package names).
  2. Use virtualenv to create an environment from which you will use your different Python version
  3. Activate your virtualenv, and install Python packages using pip on it.

Virtualenv does feature a "--help" switch to help you, but you basically do:

$ virtualenv -p <path-to-python-interpreter>  <environment-dir>
$ source <environment-dir>/bin/activate

And there you are - all things using Python will "see" the interpreter in the virtualenv, due to environment variables set.

Solution 2

ubuntu 10.04 doesn't have a python2.7 package. You have to build 2.7 yourself. I did read an article about ubuntu releasing a python2.7 package when 12.04 came out but i'm not sure what the repository location is.

http://eli.thegreenplace.net/2011/10/10/installing-python-2-7-on-ubuntu/

or:

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python2.7

https://askubuntu.com/questions/101591/install-python-2-7-2-on-ubuntu-10-04-64-bit

this question has lots of answers online.

Solution 3

pyenv

https://github.com/pyenv/pyenv

Pyenv allows you to manage multiple Python versions without sudo for a single user, much like Node.js NVM and Ruby RVM.

Install Pyenv:

curl https://pyenv.run | bash

Then add to your .bashrc:

export PATH="${HOME}/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Find Python version to install:

pyenv install --list

Install the python version you want:

# Increase the chances that the build will have all dependencies.
# https://github.com/pyenv/pyenv/wiki/Common-build-problems
sudo apt build-dep python3
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
  libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
  xz-utils tk-dev libffi-dev liblzma-dev python-openssl git

# Build and install a Python version from source.
pyenv install 3.8.0

List available Python versions:

pyenv versions

We now have:

* system (set by /home/cirsan01/.pyenv/version)
  3.8.0

Select a different python version:

pyenv global 3.8.0
python --version
python3 --version

Both output:

Python 3.8.0

We can now proceed to install and use packages normally:

pip install cowsay
python -c 'import cowsay; cowsay.tux("Python is fun")'
cowsay 'hello'

We can confirm that everything is locally installed in our clean environemnt with:

python -c 'import cowsay; print(cowsay.__file__)'

gives:

/home/ciro/.pyenv/versions/3.8.0/lib/python3.8/site-packages/cowsay/__init__.py

and:

which cowsay

gives:

/home/ciro/.pyenv/shims/cowsay

and:

which python

gives:

/home/ciro/.pyenv/shims/python

Per project usage

In the previous section, we saw how to use pyenv in a global setup.

However, what you usually want is to set a specific python and package version on a per-project basis. This is how to do it.

First install your desired Python version as before.

Then, from inside your project directory, set the desired python version with:

pyenv local 3.8.0

which creates a file .python-version containing the version string.

And now let's install a package locally just for our project: TODO: there is no nice way it seems: Pyenv choose virtualenv directory

Now, when someone wants to use your project, they will do:

pyenv local

which sets the Python version to the correct one.

Related threads:

Tested on Ubuntu 18.04, pyenv 1.2.15.

Share:
73,952
torayeff
Author by

torayeff

Updated on April 06, 2021

Comments

  • torayeff
    torayeff about 3 years

    I have ubuntu 10.04 with python2.6 by default. I have installed python2.7.

    When I want to install python packages with

    apt-get python-<package> 
    

    it gets installed to python2.6. How can I make it to install the package to python2.7? Is there any option?

    I have looked at this, but I could not find such directories in my OS. I have considered using easy_install-2.7, but not all packages are supported. For example python-torctl.

    I am more interested in binding python2.7 with apt-get install.

  • torayeff
    torayeff almost 12 years
    Even I have already installed python2.7, I did it again, the problem is not with installation with python2.7, the problem is with installing packages with apt-get install, by default it installs to python2.6, for example apt-get install python-torctl installs it to python2.6, I can not use it with python2.7, how can I do this?
  • Fred Foo
    Fred Foo almost 12 years
    @torayeff: apt-get manages packages for the version of Python that it itself installs. You can't get it to work for a different version unless you install that version with apt-get.
  • MSalters
    MSalters over 8 years
    Things can go even more wrong when you have Debian set up for cross-compilation and there are not just different versions, but even different architectures of Python.