How do I install Python 3.6 using apt-get?

Ubuntu 14.04 (Trusty) and 16.04 (Xenial)

If you are using Ubuntu 14.04 or 16.04, you can use Felix Krull's deadsnakes PPA at https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6

J Fernyhough's PPA (at https://launchpad.net/~jonathonf/+archive/ubuntu/python-3.6) used to be an alternative option, but he has shut it down to protest against (ab)use.

Ubuntu 16.10 and 17.04

If you are using Ubuntu 16.10 or 17.04, then Python 3.6 is in the universe repository, so you can just run:

sudo apt-get update
sudo apt-get install python3.6

After installation for Ubuntu 14.04, 16.04, 16.10 and 17.04

To invoke the Python 3.6 interpreter, run python3.6.

Ubuntu 17.10, 18.04 (Bionic) and onwards

Ubuntu 17.10 and 18.04 already come with Python 3.6 as default. Just run python3 to invoke it.


I would recommend pyenv to solve your woes. It doesn't use Aptitude, and does involve "building it yourself", but it's fully automated. You can build and install a new (or old) version of Python by simply saying pyenv install 3.6.0. Everything runs as your user, so you don't have to worry about messing up the Python used by Ubuntu itself.

Plus, the answer to the follow-up question "How do I install Python 3.7 using apt-get?" has the same answer: pyenv update; pyenv install 3.7.0. It will generally work same day of a release because you don't need to wait for someone else to package it for Ubuntu. See all the versions you can install with pyenv install --list

Install pyenv

  1. Install tools and headers needed to build CPythons (exotic Pythons like PyPy or Jython may have other dependencies). Git is used by pyenv, plus it also enables builds/installs of source branches, so you could install whatever 3.8 is right now, i.e. the master branch of CPython fresh off GitHub:

    sudo apt-get install -y git
    sudo apt-get install -y build-essential libbz2-dev libssl-dev libreadline-dev \
                            libffi-dev libsqlite3-dev tk-dev
    
    # optional scientific package headers (for Numpy, Matplotlib, SciPy, etc.)
    sudo apt-get install -y libpng-dev libfreetype6-dev    
    
  2. Run the installer script (installs pyenv and some very useful pyenv plugins by the original author; see here for more)

    curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
    
  3. Add init lines to your ~/.profile or ~/.bashrc (it mentions it at the end of the install script):

    export PATH="$HOME/.pyenv/bin:$PATH"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
    
  4. Restart your shell (close & open or exec $SHELL) or reload the profile script. (with e.g. source ~/.bashrc)

Done!

Setting up an environment

To not touch the system Python (generally a bad idea; OS-level services might be relying on some specific library versions, etc.) make your own environment, it's easy! Even better, no sudo, for it or pip installs!

  1. Install your preferred Python version (this will download the source and build it for your user, no input required)

    pyenv install 3.6.0
    
  2. Make it a virtualenv so you can make others later if you want

    pyenv virtualenv 3.6.0 general
    
  3. Make it globally active (for your user)

    pyenv global general
    
  4. Do what you want to with the Python/pip, etc. It's yours.

If you want to clean out your libraries later, you could delete the virtualenv (pyenv uninstall general) or make a new one (pyenv virtualenv 3.6.0 other_proj). You can also have environments active per-directory: pyenv local other_proj will drop a .python-version file into your current folder and any time you invoke Python or pip-installed Python utilities from it or under it, they will be shimmed by pyenv.

Troubleshooting

  • bash: pyenv: command not found, fish: Unknown command 'pyenv'

    1. Check your $PATH, there should be one entry that ends in something like .pyenv/bin. If it's missing make sure you followed #3 AND #4 (restart your shell) under Install pyenv above.
  • pyenv: no such command 'virtualenv'

    1. If you didn't use the installer script, you likely only installed the root pyenv package. See pyenv-virtualenv for instructions to add the plugin
    2. If you used the installer script, check if it shows up with pyenv commands.

It depends on which version of Ubuntu you are using.

Ubuntu 16.10 and Ubuntu 17.04

Since Python 3.6 is installed in the universe repository of Ubuntu 16.10 and Ubuntu 17.04, you can directly install python 3.6 from the repository. Just use the commands below:

sudo apt update
sudo apt install python3.6

Ubuntu 16.04

There are two ways to install Python3.6 on Ubuntu 16.04

  • Compile and install python 3.6 on Ubuntu 16.04
  • Install python 3.6 on Ubuntu 16.04 from PPA

1. Compile and install python 3.6 on Ubuntu 16.04

Install the necessary dependencies, download the python 3.6 source code, and build the environment and install

sudo apt install build-essential checkinstall
sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz
tar xvf Python-3.6.0.tar.xz
cd Python-3.6.0/
./configure
sudo make altinstall

2. Install python 3.6 on Ubuntu 16.04 from PPA

You can install Python 3.6  from PPA using the commands below

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.6

If Python 3.6 is correctly installed, you can invoke the python interpreter by running python3.6 in the terminal.

I hope this helps. If you are having any issues, you can check this blog post here.