みそしりんぐ

現在進行形みそしる

Ubuntu16.04(Vagrant) x Python3.5 x Django1.9

Vagrant上のUbuntu 16.04にPython 3.5 + Django 1.9の環境を作ります.

目次

  1. Ubuntu
  2. Python
    • インストール
    • virtualenv
  3. Django
    • インストール
    • アプリを作る
  4. 参考

Ubuntu

Vagrant上でUbuntu 16.04を用います.
boxはbento/ubuntu-16.04を使用.

# Vagrantfile

Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "bento/ubuntu-16.04" # ←変更

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  config.vm.network "forwarded_port", guest: 8000, host: 8080 # ←変更(Djangoのrunserverができるように)
......
$ cat /etc/issue
# Ubuntu 16.04 LTS \n \l

Python

インストール

$ sudo apt-get install python3
$ sudo apt-get install python3-pip

virtualenv

インストール
$ sudo pip3 install virtualenv virtualenvwrapper
# ~/.bashrc

### Virtualenvwrapper
if [ -f /usr/local/bin/virtualenvwrapper.sh ]; then
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh
fi
環境設定
$ mkvirtualenv mien
# myenvという環境を作る

$ mkvirtualenv myenv --python=/usr/bin/python2.6
# Pythonのバージョンを指定したい場合

$ workon myenv
# myenvという環境ににうつる
$(myenv) python --version
Python 3.5.1+
# こうなったら成功

$(myenv) deactivate
# 環境を抜ける

$ workon
# 作成した環境の一覧

$ rmvirtualenv myenv
# 作成した環境を削除する

Django

インストール

$(myenv) pip install django umemcache 
$(myenv) python -c "import django; print(django.get_version())"
# 1.9.7

アプリを作る

$(myenv) django-admin startproject project-name
$(myenv) cd project-name
$(myenv) python manage.py runserver 0.0.0.0:8000

ブラウザからlocalhost:8080にアクセスすると,下の画像のようなページにアクセスできるはず!
f:id:mssr_nm:20160607114559p:plain