Help us understand the problem. What is going on with this article?

【Ubuntu16.04】AnsibleでPython3.6のvenv環境を構築する

More than 1 year has passed since last update.

はじめに

Ansibleを使ってPythonをインストールしたら3.5系が入ってしまった。
3.6系を入れたい。

$ python -V
Python 3.5.2

実行環境

  • Ubuntu16.04 LTS
  • Ansible2.5.3

参考情報

リポジトリを追加

公式ページの手順に従いリポジトリを追加する。

- name: Add Python3.6 repository from PPA
  apt_repository:
    repo: 'ppa:jonathonf/python-3.6'

apt-update

- name: Run the equivalent of "apt-get update" as a separate step
  become: yes
  apt:
    update_cache: yes

仮想環境のインストール

python3-venvではなく、python3.6-venvなので注意。

- name: install python36-virtualenv
  become: yes
  apt:
    name: python3.6-venv
    state: latest

apt-installする

明示的にpython3.6python3.6-devを指定する。

- name: Install the packages
  become: yes
  apt:
    name:
      - python-dev
      - python-pip
      - libxml2-dev
      - libxslt1-dev
      - zlib1g-dev
      - libffi-dev
      - libssl-dev
      - python3.6
      - python3.6-dev
    state: present

仮想環境の作成

venv_nameは任意の名称を入れる。

- name: create virtualenv
  shell: python3.6 -m venv venv_name
  args:
    chdir: /home/vagrant

最終的なplaybook

- hosts: all

  tasks:

  - name: Add Python3.6 repository from PPA
    apt_repository:
      repo: 'ppa:jonathonf/python-3.6'

  - name: Update all packages to the latest version
    become: yes
    apt:
      upgrade: dist

  - name: Run the equivalent of "apt-get update" as a separate step
    become: yes
    apt:
      update_cache: yes

  - name: Install the packages
    become: yes
    apt:
      name:
        - python-dev
        - python-pip
        - libxml2-dev
        - libxslt1-dev
        - zlib1g-dev
        - libffi-dev
        - libssl-dev
        - python3.6
        - python3.6-dev
      state: present

  - name: install python36-virtualenv
    become: yes
    apt:
      name: python3.6-venv
      state: latest

  - name: create virtualenv
    shell: python3.6 -m venv venv_name
    args:
      chdir: /home/vagrant

実行結果確認

無事にvenv環境でPython3.6の環境ができました!

$ . venv_name/bin/activate
(venv_name) $ python -V
Python 3.5.2
(venv_name) $ python3.6 -V
Python 3.6.5
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away