107

I have downloaded Python 3.3 from the official site but no idea how to install it.

I'm using Ubuntu 12.04

1

8 Answers 8

115

Python 3.3 has been released on 29 September 2012, several months after Ubuntu 12.04 was released. It is included in Ubuntu 12.10 though as python3.3 package

If you want to install Python 3.3 on Ubuntu version which does not have it in its repositories, you have the following options:

Use a PPA

There's a PPA containing Old and New Python versions maintained by Felix Krull. See Luper Rouch's answer for installation instructions.

Compile Python from source

This is very easy and allows you to have multiple Python versions without messing with system python interpreter (which is used by a lot of Ubuntu own programs). On my dev machine I have literally dozens of different Python versions from 2.4 to 3.2 living happily in /opt.

we need C compiler and other stuff to compile Python

sudo apt-get install build-essential

SQLite libs need to be installed in order for Python to have SQLite support.

sudo apt-get install libsqlite3-dev
sudo apt-get install sqlite3 # for the command-line client
sudo apt-get install bzip2 libbz2-dev

Download and compile Python:

wget http://www.python.org/ftp/python/3.3.5/Python-3.3.5.tar.xz
tar xJf ./Python-3.3.5.tar.xz
cd ./Python-3.3.5
./configure --prefix=/opt/python3.3
make && sudo make install

Some nice touches to install a py command by creating a symlink:

mkdir ~/bin
ln -s /opt/python3.3/bin/python3.3 ~/bin/py

Alternatively, you can install a bash alias named py instead:

echo 'alias py="/opt/python3.3/bin/python3.3"' >> .bashrc

And this is it. Now you can have any Python version, even an alpha, or, say, to have a few copies of Python 3.3 compiled with different settings... not that many people need that though :)

Use pyenv

There's a software called pyenv which may help you to automate the procedure - what it essentially does is compile Python from source, installing it in your home directory. Its goal is to help you manage multiple Python versions.

21
  • 1
    After installation, how would one use this alternative Python installation? Say I have some .py files with the #!/usr/bin/env python shebang line (executable bit set), how would I make them to use this installation in /opt/python3.3 without modifying all of them? Or even system-installed ones.
    – gertvdijk
    Jan 18, 2013 at 8:39
  • 1
    @gertvdijk: the whole point is not to replace the default interpreter - if you do that, then every python app ran from your account will use Python 3.3, including Ubuntu apps, such as Software Centre ect. We don't want that. To run a script, just use py myscript.py (where py is a symlink we've created at the end of the exercise). I also normally use virtualenv or buildout for my projects.
    – Sergey
    Jan 18, 2013 at 8:44
  • 1
    @gertvdijk You can keep multiple python environments manageable using virtualenv.
    – flup
    Jan 18, 2013 at 13:25
  • @gertvdijk Are you aware that python 3.x and python 2.x are incompatible? If you were to point all your existing scripts at python 3.3 they'd probably break. Simply shebang your new python scripts as #! /opt/python3.3 and the correct interpreter will be addressed when you run it. Jan 22, 2013 at 21:39
  • 1
    mkdir ~/bin ln -s /opt/python3.3/bin/python ~/bin/py doesn't work for me. I found that /opt/python3.3/bin/python should be /opt/python3.3/bin/python3, but still getting py: command not found. Any suggestions.
    – user37089
    May 16, 2013 at 9:03
43

Here is what I did to install Python 3.3 on Ubuntu 12.04:

  1. Install dependencies:

    sudo apt-get build-dep python3.2
    sudo apt-get install libreadline-dev libncurses5-dev libssl1.0.0 tk8.5-dev zlib1g-dev liblzma-dev
    
  2. Download Python 3.3.0:

    wget http://python.org/ftp/python/3.3.0/Python-3.3.0.tgz
    
  3. Extract:

    tar xvfz Python-3.3.0.tgz
    
  4. Configure and Install:

    cd Python-3.3.0
    ./configure --prefix=/opt/python3.3
    make  
    sudo make install
    
  5. Test if it worked:

    /opt/python3.3/bin/python3
    

You should see something similar:

Python 3.3.0 (default, Jan 31 2013, 18:37:42) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Some Additional things that are useful... you can create a virtual environment in your home and just activate Python 3.3 on demand..

  1. Create a Virtual Environment in your home:

    /opt/python3.3/bin/pyvenv ~/py33
    
  2. Activate the virtualenv:

    source ~/py33/bin/activate
    
  3. Install distribute tools:

    wget http://python-distribute.org/distribute_setup.py
    python distribute_setup.py
    
  4. Install pip:

    easy_install pip
    
  5. Install any python packages you want (i.e. bottle)

    pip install bottle
    

Enjoy!

2
  • sudo apt-get build-dep python3.2? You probably forgot install in between :)
    – Stam Kaly
    Jan 5, 2017 at 19:12
  • 1
    @StamKaly : nope. build-dep is not a package, it's an apt-get verb (like install). It means "install all packages necessary to build the requested source package(s)"
    – MestreLion
    Jul 25, 2018 at 7:46
36

The deadsnakes PPA has packages for old and new python versions:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.3
3
  • Yes, that installed something alright, but how do I invoke it? Typing 'python' gives methe python 2 interpreter and typing 'python3' suggest that its not installed and can be installed from ubuntu repos.
    – kris
    Oct 11, 2015 at 22:48
  • Ah..I see the executable is called python3.3 (or pythonX.Y, for whatever version of python installed :-)
    – kris
    Oct 11, 2015 at 23:03
  • ppa:fkrull/deadsnakes is archived. Use ppa:deadsnakes/ppa instead.
    – codefool
    Jul 9, 2018 at 14:03
10

Ubuntu 14.04 and earlier:

Python2.7 comes default, Use the package manager to install python3 on top of regular python on Ubuntu, Ubuntu can handle both 2.7 and 3.2 at the same time without a virtualenv:

sudo apt-get install python3
python3 --version
Python 3.2.3
python --version
Python 2.2.3

Ubuntu 18.04:

Python3 comes default with the OS and Python2.7 isn't available unless you specifically install.

Three package names to choose from: python, python-minimal, python-all. Default is minimal. These words are just flags to the Ubuntu repositories to include extra stuff or not. To see exactly what subpackages are and aren't included, drill down on the subpackages of: https://packages.ubuntu.com/bionic/python

sudo apt install python-minimal
python --version

Or to try to upgrade the python3:

sudo apt install python3-minimal
python --version

To try to force a specific version, you can try passing a version parameter:

sudo apt-get install python 3.3.3
3
  • 1
    How do you update the python3 version from 3.2.3 to 3.3.5? Mar 27, 2014 at 22:18
  • 2
    Python 3.3 is only available from the default repositories in Ubuntu 12.10 and later. OP is using 12.04
    – Lenna
    Apr 14, 2014 at 18:41
  • If you want more than 2 versions of python available on one computer (other than the default 2.7 and 3.2 that your OS chooses for you), then each new version of python should be in its own virtual environment (virtualenv). Google search: "Use virtualenv to isolate version of python". If you don't use some kind of container, then you expose yourself to a labyrinth of problems since python takes a giant shit all over your computer, occupying every nook and cranny, and they fight each other in John Cleeseian fashion. Jul 11, 2018 at 13:55
1

For anyone who is interested, I wrote a more verbose step-by-step article on how to install Python 3.3.2 locally from source on Ubuntu 12.04, mostly based on reading @sergey's excellent answer above: http://nicholsonjf.com/blog/install-python3-locally-from-source

1

I have written a script to automate all of the downloading, compiling and installing of non-package Python versions. The script installs the Python version in /opt safely away from the package manager and system versions of Python.

It even fetches the dependencies as well for most versions of Ubuntu. It should work on all currently supported Ubuntu versions (10.04, 12.04, 12.10, and 13.04), and probably on other versions.

I include it below, and have posted it also at my Github repository, which is the master location.

The script should be copied and saved into a text editor as, for example, build_python, and made executable (chmod u+x build_python) and then can be run with two parameters, where the first parameter must always be the Python branch, and the second parameter must always be the Python version.

See python.org for the listings for the version you wish to compile.

Here are a couple of example of the script's usage:

  1. For the stable release, after having checked the listings, it can be run as

    ./build_python '3.3.2' '3.3.2'
    
  2. For the development release, where the two parameters are different in the listings, it can be run as:

    ./build_python '3.4.0' '3.4.0a1'
    

The body of the script is reproduced below (no syntax highlighting here. For that, see my Github page:

#!/usr/bin/env bash

# by mik, aka Exactus29, https://github.com/Exactus29
# 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

##########

# a script to compile the latest stable version of Python and place in /opt

(( $# == 2 )) || { printf "Please provide a version branch (e.g. 3.4.0) and a version release (e.g. 3.4.0a1) in that order.\n"
                   printf "The official site is python.org, see the ftp server at: http://python.org/ftp/python.\n" >&2 ; exit 1; }

# a splew of variables, so that just the version number can be given on the cmd line
# and then then the script can do the rest, including verifying the packages using gpg

# need different branch and version as sometimes the two are different, particularly for dev releases
py_branch="$1"
py_version="$2"
shift 2

# check if install target already exists in /opt, and exit so user can decide what to do
if [[ -d /opt/python-${py_version} ]]; then 
    printf "Target directory for the build already exists, please rename or remove.\n" >&2
    exit 1
else 
    :
fi

# use tar.bz2 as that is what most of the older releases used, i.e. in case user tries to build an older release
py_url="http://python.org/ftp/python/${py_branch}/Python-${py_version}.tar.bz2"
py_asc="http://python.org/ftp/python/${py_branch}/Python-${py_version}.tar.bz2.asc"
py_dir="$HOME/src/python_build" # checked to exist later, etc

# first check if user requested file exists on server
wget --spider ${py_url} >/dev/null 2>&1
(( $? > 0 )) && printf "No such version, version ${py_version} does not exist\n" >&2 && exit 1


# now very important before we do anything else, to check if asc file exists, as it  doesn't for some downloads
# if we don't check and it doesn't exist it causes the script to exit

wget --spider ${py_asc} >/dev/null 2>&1
# set a flag re whether asc file exists, so can check later and avoid problems
(( $? > 0 )) && no_asc=1 || no_asc=0

# set up more variables
py_tarbz2="${py_url##*/}"
(( no_asc == 0 )) && py_tarbz2_asc="${py_asc##*/}" # only set this if there is an asc file
py_folder="${py_tarbz2%.*.*}"
py_gpg_key="" 

# check other build dependencies are installed, beyond build-dep, sqlite support, readline, ncurses, build-essential 
dependencies_check() {

    local installed=()
    local to_be_installed=()
    local dependencies_list=(build-essential wget libreadline-dev libncurses5-dev libssl1.0.0 tk8.5-dev zlib1g-dev liblzma-dev
libsqlite3-dev sqlite3 bzip2 libbz2-dev)    

    for package in "${dependencies_list[@]}"; do 
        if grep -iq '^ii' < <(dpkg -l "$package"); then
            installed+=("$package")
        else 
            to_be_installed+=("$package")
        fi
    done 2>/dev/null

    if (( ${#to_be_installed[@]} > 0 )); then
        printf "If you have recently elevated your privileges with sudo, you will not see a " 
        printf "prompt here, before the apt-get update and install of packages occurs.\n" 
        sleep 2
        sudo -p "We need to install some dependencies, please enter your password: " apt-get update && sudo apt-get -y install "${to_be_installed[@]}"
        return 0
    else 
        printf "\nNothing to install, proceeding.\n"
        return 0
    fi

}

# tailor build-dep to new python version we want to build, basically either 2x or 3x versions
# should work with at least lucid/precise/quantal/raring/saucy, the currently supported versions
if (( ${py_branch:0:1} == 3 )) && grep -iq 'precise' /etc/lsb-release 2>/dev/null; then
    sudo -p "Please provide your password to install dependencies: " apt-get build-dep python3.2 && dependencies_check
elif (( ${py_branch:0:1} == 3 )) && grep -Eiq '(raring|quantal|saucy)' /etc/lsb-release 2>/dev/null; then
    sudo -p "Please provide your password to install dependencies: " apt-get build-dep python3.3 && dependencies_check
elif [[ ${py_branch:0:3} == 2.7 ]] && grep -iq 'lucid' /etc/lsb-release 2>/dev/null; then
    sudo -p "Please provide your password to install dependencies: " apt-get build-dep python2.6 && dependencies_check
elif [[ ${py_branch:0:3} == 2.7 ]]; then
    sudo -p "Please provide your password to install dependencies: " apt-get build-dep python2.7 && dependencies_check
else
    printf "\nProceeding, but make sure you have the correct build deps installed.\n\n"
    sleep 2        
fi

# dir checks
if [[ -d $HOME/src ]]; then 
    cd $HOME/src || exit 1
else
    mkdir $HOME/src && cd $HOME/src
fi

if [[ -d ${py_dir} ]]; then
    mv "${py_dir}" "${py_dir}_old_$(date '+%F_%H_%M_%S')"
    mkdir "${py_dir##*/}" && cd "${py_dir##*/}"
else
    mkdir "${py_dir##*/}" && cd "${py_dir##*/}"
fi

# finally, download python 
printf "\nNow downloading version ${py_version} from branch ${py_branch} ....."
wget "${py_url}" -P "${py_dir}" >/dev/null 2>&1
(( $? == 0 )) && printf "Done.\n"
# only download asc if it exists, set flag earlier
(( no_asc == 0 )) && wget "${py_asc}" -P "${py_dir}" >/dev/null 2>&1

# gpg tests

gpg_test() {
    # if error returned, extract gpg key from error message
    py_gpg_key="$(gpg --verify "${py_tarbz2_asc}" "${py_tarbz2}" 2>&1 | awk '{ print $NF }' | grep -v found)"

    # now check with gpg_key (should be Python release signing key)
    printf "\nReceiving keys.. "
    gpg --recv-keys "${py_gpg_key}" >/dev/null 2>&1
    (( $? > 0)) && printf "Key could not be received\n" || printf "Done.\n"

    printf "\nVerifying download... "
    gpg --verify "${py_tarbz2_asc}" "${py_tarbz2}" >/dev/null 2>&1
    (( $? > 0 )) && printf "The download could not be verified.\n" || printf "Done.\n"

}

if (( no_asc == 0 )); then
    gpg --verify "${py_tarbz2_asc}" "${py_tarbz2}" >/dev/null 2>&1
    if (( $? > 0 )); then 
        gpg_test 
    else
        printf "\nDownload verified\n\n"
    fi
else
    printf "\nProceeding even though asc file is not available for gpg to verify download\n\n"
    sleep 1
fi

# unpack and cd to the python folder
printf "Unpacking archive...."
tar xvjf "${py_folder}.tar.bz2" >/dev/null 2>&1
(( $? == 0 )) && printf "Done.\n" || { printf "Problems occured when unpacking, exiting\n" >&2; exit 1; }
cd "${py_folder}" || exit 1

# tailor the build to your machine here with configure and make

printf "\nNow for the configure (default prefix is /opt/python-${py_version})...."
sleep 2
./configure --prefix=/opt/python-${py_version} >/dev/null 2>&1
# as configure and make will exit anyway on error, no need to add || alternatives to the tests below
(( $? == 0 )) && printf "Done.\n\n"  
sleep 1

printf "\nNow for the compile. (If necessary, please add your own specifications to the make command line and run the script again)\n"
printf "\nPlease wait for the compile to finish: it may take a while...."
make >/dev/null 2>&1
(( $? == 0 )) && printf "Done.\n\n"

printf "\nWe are installing with make install into /opt, instead of using checkinstall.\n"
sudo make install >/dev/null 2>&1
installcode=$?
(( $installcode == 0 )) && printf "\n${py_version} succesfully installed in /opt/python-${py_version}\n\n"

if [[ -d $HOME/bin ]]; then
    ln -s /opt/python-${py_version}/bin/python${py_version:0:3} ~/bin/py-${py_version}
    (( $? == 0 )) && printf "\nSymlink created, run py-${py_version} in the terminal to launch the interpreter\n"
else
    mkdir $HOME/bin && ln -s /opt/python-${py_version}/bin/python${py_version:0:3} ~/bin/py-${py_version}
    (( $? == 0 )) && printf "\nSymlink created, run py-${py_version} in the terminal to launch the interpreter\n"
    printf "\nHowever, you will not be able to call py-${py_version} until you have logged out and in again, as bin will not"
    printf " have been added to your path just as $HOME/bin is created.\nn"
fi

# important info re setting up pyvenv re distribute tools and pip etc
cat <<extra_info

            See also a program called pyvenv with your installation in /opt, 
            with which you can create a virtual environment and use tools
            such as pip, etc. See the official documentation at:
            http://docs.python.org/3.3/using/scripts.html#pyvenv-creating-virtual-environments

extra_info

sleep 2 
exit ${installcode}
1

Warning: Pythonbrew has been deprecated in favor of pyenv. Updated instructions are here

Also you can use something like pythonbrew:

curl -kL http://xrl.us/pythonbrewinstall | bash    
echo "[[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc" >> ~/.bashrc    
pythonbrew install 3.3

It's quite easy to use, and another benefit, that it's possible to install any python version you need. Please see their docs for mode details

0
0

Here are the steps that I followed:

wget http://python.org/ftp/python/3.3.2/Python-3.3.2.tar.bz2
tar -xvjf ./Python-3.3.2.tar.bz2
cd ./Python-3.3.2
./configure --prefix=/opt/python3.3
make && make install
mkdir ~/bin
ln -s /opt/python3.3/bin/python ~/bin/py
echo 'alias py="/opt/python3.3/bin/python3"' >> .bashrc

You must log in to answer this question.

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