在Vagrant中安装Node,NPM(以及Grunt和Bower全局)

时间:2022-09-19 07:58:37

I've been following this gist for installing a bunch of core dependencies for PHP development, but one that I do need and have not been able to get working as of yet is to install Node, NPM (and then Grunt and Bower globally) in Vagrant. I've seen answers like this one but it doesn't ever appear to complete the install (ie. I can't then install bower and grunt). There is a nodejs package through apt-get that does appear to install Node (and I can install NPM from there), but the version appears to be too old for Bower, so that's why I backtracked to a manual approach. Thanks for any help.

我一直在遵循这个要点,为PHP开发安装了一堆核心依赖项,但我确实需要并且尚未能够正常工作的是安装Node,NPM(以及Grunt和Bower全局)流浪汉。我已经看到了这样的答案,但它似乎没有完成安装(即我不能再安装bower和grunt)。有一个通过apt-get的nodejs包看起来似乎安装了Node(我可以从那里安装NPM),但是版本对于Bower来说似乎太旧了,所以这就是我回溯到手动方法的原因。谢谢你的帮助。

Update

Here is my bootstraph.sh file:

这是我的bootstraph.sh文件:

#!/usr/bin/env bash

echo ">>> Starting Install Script"

# Update
sudo apt-get update

# Install MySQL without prompt
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password root'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root'

echo ">>> Installing Base Items"

# Install base items
sudo apt-get install -y curl wget build-essential python-software-properties python g++ make

echo ">>> Adding PPA's and Installing Server Items"

# Add repo for latest PHP
sudo add-apt-repository -y ppa:ondrej/php5

# Add NodeJS repository
sudo add-apt-repository -y ppa:chris-lea/node.js

# Update Again
sudo apt-get update

# Install the Rest
sudo apt-get install -y php5 apache2 libapache2-mod-php5 nodejs php5-mysql php5-curl php5-gd php5-mcrypt php5-xdebug mysql-server

echo ">>> Configuring Server"

# xdebug Config
cat << EOF | sudo tee -a /etc/php5/mods-available/xdebug.ini
xdebug.scream=1
xdebug.cli_color=1
xdebug.show_local_vars=1
EOF

# Apache Config
sudo a2enmod rewrite
curl https://gist.github.com/fideloper/2710970/raw/5d7efd74628a1e3261707056604c99d7747fe37d/vhost.sh > vhost
sudo chmod guo+x vhost
sudo mv vhost /usr/local/bin

# Symlink /var/www to project web root
sudo rm -rf /var/www
sudo ln -s /vagrant/ /var/www

# PHP Config
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php5/apache2/php.ini
sed -i "s/display_errors = .*/display_errors = On/" /etc/php5/apache2/php.ini

sudo service apache2 restart

echo ">>> Installing NPM, Grunt CLI and Bower"

curl https://npmjs.org/install.sh | sh
sudo npm install -g grunt-cli bower

echo ">>> Installing Composer"

# Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

2 个解决方案

#1


5  

Found the following that works well:

发现以下效果很好:

#!/usr/bin/env bash

echo "--- Let's get to work. Installing now. ---"

echo "--- Updating packages list ---"
sudo apt-get update

echo "--- MySQL time ---"
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password root'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root'

echo "--- Installing base packages ---"
sudo apt-get install -y vim curl python-software-properties

echo "--- Updating packages list ---"
sudo apt-get update

echo "--- We want the bleeding edge of PHP ---"
sudo add-apt-repository -y ppa:ondrej/php5

echo "--- Updating packages list ---"
sudo apt-get update

echo "--- Installing PHP-specific packages ---"
sudo apt-get install -y php5 apache2 libapache2-mod-php5 php5-curl php5-gd php5-mcrypt mysql-server-5.5 php5-mysql git-core

echo "--- Installing and configuring Xdebug ---"
sudo apt-get install -y php5-xdebug

cat << EOF | sudo tee -a /etc/php5/mods-available/xdebug.ini
xdebug.scream=1
xdebug.cli_color=1
xdebug.show_local_vars=1
EOF

echo "--- Installing node.js ---"
sudo apt-get update
sudo apt-get install -y  python g++ make
sudo add-apt-repository -y ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install -y  nodejs

echo "--- Installing Ruby ---"
\curl -L https://get.rvm.io | bash -s stable
source /usr/local/rvm/scripts/rvm
rvm requirements
rvm install ruby
rvm use ruby --default
rvm rubygems current

echo "--- Enabling mod-rewrite ---"
sudo a2enmod rewrite

echo "--- Setting document root ---"
sudo rm -rf /var/www
sudo ln -fs /vagrant/public /var/www


echo "--- Turn on errors ---"
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php5/apache2/php.ini
sed -i "s/display_errors = .*/display_errors = On/" /etc/php5/apache2/php.ini

sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf

echo "--- Restarting Apache ---"
sudo service apache2 restart

echo "--- Install Composer (PHP package manager) ---"
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

# Laravel stuff here, if you want

echo "--- All done, enjoy! :) ---"

Source: https://github.com/JeffreyWay/Vagrant-Setup

#2


2  

We use n to manage node.js versioning on both our Vagrant VMs and cloud boxes. We are using Ansible to manage the installation but it can easily be achieved using bash, something like:

我们使用n在我们的Vagrant VM和云盒上管理node.js版本控制。我们使用Ansible来管理安装,但可以使用bash轻松实现,例如:

# Install n
n_directory='/opt/n'
git clone https://github.com/visionmedia/n.git $n_directory
cd $n_directory
make install

# Install node.js/npm using n
node_version='0.10.24'
n $node_version

# Install the global dependencies
npm install -g grunt-cli bower

This relies on n and npm being available on $PATH.

这依赖于$ PATH上的n和npm。

#1


5  

Found the following that works well:

发现以下效果很好:

#!/usr/bin/env bash

echo "--- Let's get to work. Installing now. ---"

echo "--- Updating packages list ---"
sudo apt-get update

echo "--- MySQL time ---"
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password root'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root'

echo "--- Installing base packages ---"
sudo apt-get install -y vim curl python-software-properties

echo "--- Updating packages list ---"
sudo apt-get update

echo "--- We want the bleeding edge of PHP ---"
sudo add-apt-repository -y ppa:ondrej/php5

echo "--- Updating packages list ---"
sudo apt-get update

echo "--- Installing PHP-specific packages ---"
sudo apt-get install -y php5 apache2 libapache2-mod-php5 php5-curl php5-gd php5-mcrypt mysql-server-5.5 php5-mysql git-core

echo "--- Installing and configuring Xdebug ---"
sudo apt-get install -y php5-xdebug

cat << EOF | sudo tee -a /etc/php5/mods-available/xdebug.ini
xdebug.scream=1
xdebug.cli_color=1
xdebug.show_local_vars=1
EOF

echo "--- Installing node.js ---"
sudo apt-get update
sudo apt-get install -y  python g++ make
sudo add-apt-repository -y ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install -y  nodejs

echo "--- Installing Ruby ---"
\curl -L https://get.rvm.io | bash -s stable
source /usr/local/rvm/scripts/rvm
rvm requirements
rvm install ruby
rvm use ruby --default
rvm rubygems current

echo "--- Enabling mod-rewrite ---"
sudo a2enmod rewrite

echo "--- Setting document root ---"
sudo rm -rf /var/www
sudo ln -fs /vagrant/public /var/www


echo "--- Turn on errors ---"
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php5/apache2/php.ini
sed -i "s/display_errors = .*/display_errors = On/" /etc/php5/apache2/php.ini

sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf

echo "--- Restarting Apache ---"
sudo service apache2 restart

echo "--- Install Composer (PHP package manager) ---"
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

# Laravel stuff here, if you want

echo "--- All done, enjoy! :) ---"

Source: https://github.com/JeffreyWay/Vagrant-Setup

#2


2  

We use n to manage node.js versioning on both our Vagrant VMs and cloud boxes. We are using Ansible to manage the installation but it can easily be achieved using bash, something like:

我们使用n在我们的Vagrant VM和云盒上管理node.js版本控制。我们使用Ansible来管理安装,但可以使用bash轻松实现,例如:

# Install n
n_directory='/opt/n'
git clone https://github.com/visionmedia/n.git $n_directory
cd $n_directory
make install

# Install node.js/npm using n
node_version='0.10.24'
n $node_version

# Install the global dependencies
npm install -g grunt-cli bower

This relies on n and npm being available on $PATH.

这依赖于$ PATH上的n和npm。