How do I install Python 3.6 using apt-get in Ubuntu 16.04?

Published on Author Code Father
How do I install Python 3.6 using apt-get in Ubuntu 16.04?

If you are using Ubuntu 14.04 or 16.04, you can use J Fernyhough’s PPA at https://launchpad.net/~jonathonf/+archive/ubuntu/python-3.6 to install Python 3.6:

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6

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

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

—————————————–/OR/———————————————– Another way 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 just as your user, so you don’t have to worry about messing up the Python used by Ubuntu itself.

Install pyenv

  1. Install headers needed to build Pythons:
    sudo apt-get install libbz2-dev libssl-dev libreadline-dev libsqlite-dev
    
    # optional scientific package headers (for Numpy, Matplotlib, SciPy, etc.)
    sudo apt-get install 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://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
    
  3. Add init lines to your ~/.bash_profile or ~/.bashrc (it mentions it at the end of the install script):
    export PATH="/home/ubuntu/.pyenv/bin:$PATH"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
    
  4. Restart your shell to reload the profile script.

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.

 

 

Comments

comments