Como instalar o Vagrant

por Pedro Resende em 01/07/2013 às 14:09:33

Vagrant

Hoje vou-vos explicar como instalar e configurar o Vagrant.Para quem não sabe o que é, trata-se de um software open-source para criação e configuração de ambientes de desenvolvimento virtuais.

Pode ser considerado um invólucro em torno VirtualBox e software de gerenciamento de configuração, como Chef, Salt, Puppet e Ansible. Apesar de escrito em Ruby, é utilizável em outros projetos de programação como PHP, Python, Java e C #.

Como tal é necessário terem instalado o Virtual Box ou o Vmware.

As seguintes instruções foram executadas no Ubuntu 12.04, contúdo deverão funcionar nos outros sistemas operativos.

Vamos então começar por fazer o "download" da última versão do vagrant atravês do seguinte site, uma vez terminada abram a linha de comando e escrevam o seguinte:

$ cd Downloads

$ sudo dpkg -i vagrant_x.x.x_i686.deb em que devem substituir os x's pela versão selecionada

Uma vez terminada, vamos então fazer o download de uma máquina, para issue executem o seguinte comando

$ vagrant box add <name> <url>

Quando terminar, vamos initializar a máquina através

$ vagrant init <name>

Vamos então alterar algumas configurações

$ vi Vagrantfile

Vamos começar por descomentar as seguintes linhas

 config.vm.network :forwarded_port, guest: 80, host: 8080
 config.vm.network :public_network

Para que possamos aceder à máquina virtual através da porta local 8080 e para que esteja disponível na nossa rede

Vamos então iniciar a vm através:

$ vagrant up

Deverão deparar-se com o seguinte

Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'ubuntu12'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Available bridged network interfaces:
1) eth0
2) eth1
3) wlan0
What interface should the network bridge to?

No meu caso escolhi o 1, sendo a primeira placa de rede

What interface should the network bridge to? 1
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] -- 80 => 8080 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...

Uma vez terminado poderão aceder através de ssh à vossa máquina utilizando o comando

$ vagrant ssh

Para desligarem a máquina usem

$ vagrand halt

Para suspenderem

$ vagrand suspend

e para destruirem e voltarem ao inicio

$ vagrant destroy

e está feito, como podem ver nada do outro mundo.

Pedro Resende

Pedro Resende

With over two decades of experience as a Full Stack Developer, I have journeyed through diverse facets of software development. Commencing with the creation of compact institutional websites, I progressed to crafting portals for insurance brokers and contributed to substantial projects for renowned companies such as MS Amlin. My expertise expanded further by delving into product development for Advertiser Kwanko and collaborating with startups in their pivotal ramp-up stages. Notably, I've been involved in architecting extensive e-commerce solutions for global enterprises like Wayfair.

I currently serve as the Tech Lead at Emergn, contributing to the establishment and advancement of the Strategyzer client.