yoshiislandblog.net
元営業の駆け出しアラサーSEが、休日にMACと戯れた際の殴り書きメモ。日々勉強。日々進歩。

この記事は3年以上前に書かれた記事で内容が古い可能性があります

Ubuntu14にpyenvとPython3環境導入

2018-05-05

サクッとamazon linuxにpyenvとpython3を導入にてAmazonLinuxにpythonを入れたが、
今回は、Vagrantで作成したVirtualbox上のUbuntu14のVMにpyenvとpython3を導入する。
vagrant環境のセットアップで作成したVM

VMを起動する

[yoshi@18-05-05T19:32:09] ~/vagrant_work
% cd ubuntu14_test
[yoshi@18-05-05T19:32:15] ~/vagrant_work/ubuntu14_test
% vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
[yoshi@18-05-05T19:32:54] ~/vagrant_work/ubuntu14_test
%

起動したのでSSHでログイン

[yoshi@18-05-05T19:34:25] ~/vagrant_work/ubuntu14_test
% vagrant ssh
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-145-generic x86_64)

......
......

Last login: Thu May  3 11:57:49 2018 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty-64:~$

必要パッケージのインストール

Amazon Linuxと同じパッケージでは入らない。

vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get install gcc gcc-c++ make git openssl-devel bzip2-devel zlib-devel readline-devel sqlite-devel
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package gcc-c+
E: Couldn't find any package by regex 'gcc-c+'
E: Unable to locate package openssl-devel
E: Unable to locate package bzip2-devel
E: Unable to locate package zlib-devel
E: Unable to locate package readline-devel
E: Unable to locate package sqlite-devel
vagrant@vagrant-ubuntu-trusty-64:~$

これでインストールできる。

vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get install git gcc make openssl libssl-dev libbz2-dev libreadline-dev libsqlite3-dev

pyenvのインストール

vagrant@vagrant-ubuntu-trusty-64:~$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv

環境編集の記述

bash_profileなるものが見当たらない、、、

vagrant@vagrant-ubuntu-trusty-64:~$ sudo cat ~/.bash_profile
cat: /home/vagrant/.bash_profile: No such file or directory
vagrant@vagrant-ubuntu-trusty-64:~$


vagrant@vagrant-ubuntu-trusty-64:~$ ls ~/.bash
.bash_history  .bash_logout   .bashrc
vagrant@vagrant-ubuntu-trusty-64:~$

bashrcの中身を見ると、~/.bash_aliasesを作れば読み込んでくれそう

vagrant@vagrant-ubuntu-trusty-64:~$ cat ~/.bashrc

......
......

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

......
......

vagrant@vagrant-ubuntu-trusty-64:~$

なので、~/.bash_aliasesを作成して中に記載する

vagrant@vagrant-ubuntu-trusty-64:~$ vim ~/.bash_aliases
vagrant@vagrant-ubuntu-trusty-64:~$ cat ~/.bash_aliases
# ~/.bash_aliases created by yoshi 20180505
# User specific environment and startup programs

PATH=$PATH:$HOME/bin
export PATH

export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"
vagrant@vagrant-ubuntu-trusty-64:~$

一度exitして再ログインすることで環境変数が読み込まれる

vagrant@vagrant-ubuntu-trusty-64:~$ exit
logout
Connection to 127.0.0.1 closed.
[yoshi@18-05-05T19:58:19] ~/vagrant_work/ubuntu14_test
% vagrant ssh
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-145-generic x86_64)

......
......

Last login: Sat May  5 10:34:29 2018 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty-64:~$

vagrant@vagrant-ubuntu-trusty-64:~$ pyenv
pyenv 1.2.4
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
vagrant@vagrant-ubuntu-trusty-64:~$

pyenvでpython3のインストール

pyenvのインストール&セットアップができたので、python3を入れる

確認

vagrant@vagrant-ubuntu-trusty-64:~$ pyenv versions
* system (set by /home/vagrant/.pyenv/version)
vagrant@vagrant-ubuntu-trusty-64:~$

python3.5のインストール

vagrant@vagrant-ubuntu-trusty-64:~$ pyenv install 3.5.0

Downloading Python-3.5.0.tar.xz...
-> https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tar.xz
Installing Python-3.5.0...
Installed Python-3.5.0 to /home/vagrant/.pyenv/versions/3.5.0

vagrant@vagrant-ubuntu-trusty-64:~$

vagrant@vagrant-ubuntu-trusty-64:~$ pyenv versions
* system (set by /home/vagrant/.pyenv/version)
  3.5.0
vagrant@vagrant-ubuntu-trusty-64:~$

デフォルトを3.5に設定する

vagrant@vagrant-ubuntu-trusty-64:~$ pyenv global 3.5.0
vagrant@vagrant-ubuntu-trusty-64:~$ pyenv versions
  system
* 3.5.0 (set by /home/vagrant/.pyenv/version)
vagrant@vagrant-ubuntu-trusty-64:~$

完了