LoginSignup
6
7

More than 5 years have passed since last update.

【Swift】Vagrant で Ubuntu を動かして Swift をインストールするまで

Posted at

環境

macOS: 10.12.1
Vagrant: 1.8.7
VirtualBox: 5.0.24

1. Vagrantのインストール

こちらからダウンロードしてインストールを行います。
https://www.vagrantup.com/downloads.html

2. VirtualBoxのインストール

今回は、最新版ではなく、こちらからバージョン5.0.24をダウンロードし、インストールしました。
https://www.virtualbox.org/wiki/Download_Old_Builds_5_0

※VirtualBoxのバージョンが最新版であることによって、Vagrantが対応できていないことがあるようなので、このようにしました。
参考:http://qiita.com/mtake6212/items/de7b26da8b283213f12a

3. boxの追加

今回はSwiftを使える環境にしたいので、Ubuntu 14.04 or 15.10 (64-bit)のディストリビューションのboxをダウンロードします。

ここから、該当するboxを探します。
http://www.vagrantbox.es/

今回は、次のboxを使うことにしました。

https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box

vagrant box add コマンドを使って、boxを追加します。

$ vagrant box add Ubuntu-14.04 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box

vagrant box list コマンドで先ほど追加したboxが表示されればOKです。

$ vagrant box list
Ubuntu-14.04 (virtualbox, 0)

4. 仮想マシンの初期化

vagrant init {box-name} というコマンドで初期化ができるので、次のように先ほど追加したboxで初期化を行います。

$ vagrant init Ubuntu-14.04
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

初期化が終わると、同ディレクトリにVagrantfileが作成されています。
必要であればこのVagrantfileの内容を書き換えて設定を変えることができますが、一旦このままで行きます。

5. 仮想マシンの起動&ログイン

ここまでで、仮想マシンを立ち上げる準備が完了しました。
作成した仮想マシンを立ち上げるためには、vagrant upコマンドを使います。

$ vagrant up

無事立ち上がったら、仮想マシンへssh接続してログインします。

$ vagrant ssh

6. Swiftのインストール

マニュアルでインストールする場合は、公式に従って行うのがいいかと思います。
https://swift.org/download/#installation-1

今回は、楽をしたいので、Server Side SwiftのフレームワークであるVaporのインストールスクリプトを使ってインストールを行います。

ドキュメントはこちら

sshで入った仮想マシンの中で次のコマンドを実行します。

$ sudo curl -sL swift.vapor.sh/ubuntu | bash

ドキュメントではsudoなしですが、私が実行した際にはsudo付きでないとインストールできませんでした。

※ちなみに、実際に実行されているスクリプトはこちらになります。

無事インストールできたら、最後に~/.bashrcの内容を反映させます。

$ source ~/.bashrc

7. 動作確認

バージョンを確認してみます。

$ swift --version
Swift version 3.0 (swift-3.0-RELEASE)
Target: x86_64-unknown-linux-gnu

対話式に実行してみます。

$ swift
Welcome to Swift version 3.0 (swift-3.0-RELEASE). Type :help for assistance.
  1> let test = "hoge"
test: String = "hoge"
  2> print(test)
hoge

動いていますね。

最後に

今回はVaporのスクリプトを使ってサクッとSwiftのインストールを行いました。
便利な反面、バージョンアップ時の対応やスクリプト自体のバグなどがあった際にすぐに対応できなくなってしまったりすることはあるかと思います。
少なくとも、インストールスクリプト自体は読んで理解した上で使う必要はありますね。

とりあえず、Swiftが動くようになったので、次はVaporで作ったアプリケーションのデプロイなどに挑戦しようと思います。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7