I got yet another computer. To work on. But there will be slow days on airports and hotels with this baby to... So I decided to try something new; I decided to go Linux and setup a development environment for Node Js. I did not set up a goal of this being a no-cost project, it just ended up that way (with one worthy exception, in the end). I have never used Linux before and decided to go with Ubuntu that looked like it would suit my Windows/Mac background best. It did too. It's been a pure joy to use so far. This blog post described what I did and what obstacles I ran into on the way. It was ... not surprisingly maybe... but at least gratifyingly simple and smooth. I've tried to describe it so that you can be both a total newbie and a little bit more advance. The short version (aka TL;DR) comes first in each section and then the details. Skip what you don't want to read. I started with a normal PC Lenovo (2.5 Ghz, 4 Gb ram) with a 64-bits Windows 8.1 Pro on it. The good news is that you don't have to care about that. Because we are building a virtual computer using VirtualBox. This post is ~~pretty scary~~ very very long but the installation is quick. Downloading Ubuntu and installing it it what's takes the most time. With a good internet connection (that I do NOT have), you should be able to complete all of this in under 1 hour. Here we go. ### Downloading downloading downloading This section will contain a lot of waiting. I've put in some information for you to read in the meanwhile. Here it is in short form; - Download VirtualBox - Download Ubuntu installation media #### VirtualBox First we need to get VirtualBox itself. Download the correct version for your host operating system from here. | | |:-------------------------------------------------------------------------------------------------------:| | | | From ForeverGeek | If you're new to this VirtualBox is free a virtualization software, from Oracle of all places. Meaning that we can create a single file (aka an image) that represents an entire computer. With this you can run another computer in your computer. It's a bit Inception-like but useful. Your physical computer, aka the host environment, is where you will run VirtualBox. So you need to download the correct version for your host. Windows, Mac what have you. Also way down at end of the downloads page there's a link to a set of prebuilt images. Virtual computers that you can download. I had some problems to get that to work. And downloading them on my **slow** network took forever. #### Ubuntu install media Let's download Ubuntu installation media. We want the desktop version (although the server version would probably do just fine too). There's some helpful advice on the Ubuntu download page too, as seen on the right. WATCHOUT! The version of VirtualBox that I have downloaded (just a few weeks ago) only supported 32 bit Ubuntu. I got that in other words. Get the one that is suitable for you. Once you click the Download button you will be taken to a "pay what it's worth page". I strongly recommend that you pay a couple of bucks. An operating system. For $10. Check the latests quotation for a Windows 8.1 license. You getting something very similar. But it's up to you. If you don't want to pay there's a "Not now. Take me to the download"-link. Ubuntu is a free "distribution" of Linux. For us non-Linux people this means that a company has taken the Linux core source code (it's open source remember) and created a version of their own. And is now giving it away for free. Ubuntu earns their money on consultancy and selling support I would suspect. Amazingly true. Ubuntu is Swahili, btw, and [means...](http://askubuntu.com/questions/424/what-does-ubuntu-mean) Nice, huh? Ok, that has not finished downloading yet. But will pretend it has. ### Create your virtual computer This is simple... if you know how. 1. Install VirtualBox ("Next, Next, Next, Wait, Finish") 2. Create a new virtual machine and attach the Ubuntu installation media 3. Boot up and complete the Ubuntu installation #### Install VirtualBox on your host Now that you have everything downloaded, first install VirtualBox on your host system. This is your typical "Next, Next, Next, Wait, Finish"-installation and I will not say much about that. Just doubleclick the "VirtualBox-yada-yada-yada-Win.exe" file you downloaded and you should be fine. #### Create your virtual machine
Now, again if you're new to this it will be a bit strange. We will now create a virtual computer machine. Or virtual machine for short and if you under 85 years old, and stopped use that phrase. Luckily this is a breeze in VirtualBox. 1. Start VirtualBox 2. Click New 3. Give your machine a name 4. Set the Operating system type and version 5. Click Next
That was pretty easy. Now we will allocate memory. Here you will have to think about that your host operating system needs some memory to work properly. VirtualBox has indicated what they think is max memory to allocate to the virtual computer. I usually go below that just to make sure. Here I have allocated 1664 out of my 4096 Mb to the virtual image. Works fine for the things I will be doing. Now we will create the virtual hard drive. This was the thing that confused me the most when I first started to use virtual machine, quite a few years back. But when you think about it, its's natural; your virtual machine needs a hard drive. It's a file stored on your host system. We are now going to create it. I will not show every screen here, most of them are super easy to grasp; - Select "Create new hard drive" and click Create - For your Virtual drive type select Virtual Disk Image (VDI) if you have no special reason not to - Click Next - Select "Dynamically allocated" as storage on physical drive. The slowness they are warning you about has never bothered me. Or I have not noticed it at least. - For file location you need to make sure that you know where the file ends up. Default it is under your user-folder on your host system. I usually put my virtual disk drives on a separate disk. Click the little browse symbol and choose a location - For size... well I went with 20 Gb. It will increase as needed. - Click Create. - And when you have done that your computer is done... This is where I got confused, but the virtual machine you have now is actually completed. You can compare this with buying a blank, uninstalled computer from a store. Nothing is installed on it right now. Let's fix that. ### Installing Ubuntu on your virtual machine 1. Attach the .ISO with Ubuntu 2. Start the Virtual Machine and follow the installation instruction to install Ubuntu 3. Done - Ubuntu is installed 4. Oh yeah, install the VirtualBox Guest Additions as well. If you got home with an empty computer, what would you do? Install an OS. From a DVD probably. Let's do that, but from the .iso-file we downloaded from Ubuntu.
The first thing you would do is to insert the DVD with the OS in the DVD slot of your new physical computer. Well you don't have neither a DVD nor a DVD player. We'll have to figure something else out. Select your virtual machine in the VirtualBox (quite ugly, old school GUI) and select Settings. This will open a scary dialog box where you can tweak all sorts of settings for your virtual machine. Don't! If you're not absolutely sure that ~~that's what that blogpost, on performance you read, said what you should do~~ you know what your are doing. Things can stop working from here.
Instead head over to the Storage-section and click that strange little +-sign. This let's you choose a "disk". Choose the .ISO-file you downloaded from Ubuntu and click ok. This is called mounting a ISO, and is the equivalent of putting a physical DVD in the DVD drive of the computer. You are now ready to fire your computer up for the first time. This is done by selecting your virtual machine and clicking Start (big green arrow). When you do that it will install Ubuntu from the ISO-file. There's not much use for me to repeat that process. It's smooth and requires you to follow the on-screen instructions. Takes about 15 mins to install. Here are the comments and caveats that got me wondering. - The first thing that happened was that I got a big red screen saying "Uninitialized yada yada yada... upgrade BIOS yada yada yada...". That went away by itself - The first choice is if you want to "try" or "install" Ubuntu. A bit strange maybe but you of course should go with Install there. - Check "Downloading updates during install" - You want to erase the disk, since theres nothing on it and install Ubuntu on it - And the fill in the personal things. IMHO a strong password is not needed here since it will be run on your strong password protected computer... But you do as you want - As normal these days theres some commercials and instructions on how to use the OS during the install. Quite handy stuff if you are a newbie. Like I was at that point. - Did you notice how Ubuntu keeps working while you where typing in thing and indicating your timezone and keyboard etc. Nice! - Doing a lot of other things while installing is stupid. Like writing a blog post... The host is quite slow while installing. Or you might have to adjust the memory settings for your virtual machine. I backed it off to 1.2 Gb instead for faster handling on my host. When Ubuntu is installed it will ask you to reboot and then you can log in again. In all honesty VirtualBox crashed on me here... Twice... I reinstalled but the same thing happened. But unmounting and starting the virtual machine worked just fine... Strange and a bit uneasy feeling. You can now "unmount" the .ISO file if you like, you don't need it anymore. When the virtual machine is not running go to
settings/storage like before, but remove the ISO file this time. You can also click the little disc at the bottom of the screen when you virtual machine is running and chose "Remove disk from virtual drive". #### VirtualBox guest additions Oh, one last thing. We should install the VirtualBox guest additions. What's that you ask (and me too from time to time)? Well: > They consist of device drivers and system applications that optimize > the guest operating system for better performance and usability You install them by: 1. Start the virtual machine 2. Click the Devices menu in the VirtualBox window that your virtual machine is running in 3. This will mount a .iso with the guest additions and start installing it 4. You will have to give your password to give the installation permission to run 5. Don't be scared of the terminal window flashing by... It's the Linux way. "Press Return" when it's completed. 6. Soon it will be done and you're wise to restart the computer again. 7. Done ### Setting up VirtualBox There's a few settings that I find useful and always to in VirtualBox. First I make sure that I know which the Host-key is. This key is used to invoke commands from your host environment. For example making the virtual machine window fullscreen (Host+F). And now that it's fullscreen go back again (Host+F again).
So... which is my host key then? That's actually pretty hard to find out: 1. Open VirtualBox 2. Got to File - Properties 3. Select Input 4. Select Virtual Machine 5. In the top you'll find the Host Key Combination... For me it's the right CTRL-button. I seldom use that button for other things. 6. Easy huh? NOT!
Luckily I found a much easier place to see the host key. The currently selected host key is displayed in the lower right corner of the virtual machine window. Here's that part of my screen that shows the "Right Ctrl"-key as the host key. Click it to change. I also make sure the that the clipboard is "bi-directional". That means that I can copy something in the virtual machine and then paste it in a program on my host. Or vice versa. Very handy.
### Get to know Linux / Ubuntu Skip this if you're not afraid of new OS's. If you are. Don't be that. Read this instead. If you've used Windows XP/7 or 8 or OS X during the 2000-ies you will be able to figure all of this out yourself. Here's my homescreen:
- To the right is my Start-menu docked. You can see all my programs there. When you start a new program it will show there. You can then rightclick it and pin it to the start menu so that it's easy to start again - On this screen I've clicked the Ubuntu logo so that I can search through my system for applications and files - Theres an "explorer" that shows your files and folders. I have stored everything I use under my Home-folder. The first thing I created was Projects folder - When you open a window it's not fullscreen. You can make it so by click the square symbol in the upper left corner. Now those symbols got lost and you wonder where they are. You can find them again in the upper left corner of the desktop. Where the name of the application is visible. Confused me for awhile. - In the upper right corner there's a little cog-symbol. Click that to find Shut down... That took me QUITE sometime to find. There's loads of other things but all in all it was very easy to understand and get started. I like it! ### Installing Node stuff We are now ready to start installing the things we need to develop Node applications. Here is the list of things that I use frequently. - Chrome for Linux - [Node](http://nodejs.org/) - [Mongo Db](http://www.mongodb.org/) - Robomongo - Git - Nodemon - Mocha #### Chrome for Linux Chrome is simplest to install from the normal download site. I always use Chrome on all my computers and this is no exception. I always log in to Chrome as well since it syncs my bookmarks, apps and history. Installing this way is very much like doing it on Windows or Mac. Except... the file that you download is not an .exe of course. It's a .deb (debian package). Save that and then double click it to start the installation. This will open the "Ubuntu software center" and from there you can click Install. Go ahead and install your favorite browser now. If it's not Firefox because that's included... #### Node Now, using that browser, let's install [Node](http://nodejs.org/). Head over to the [Node](http://nodejs.org/)-site and click Install. This will download a .tar.gz file for you with the installation in. When that is completed open the file and ... or we can do this the Linux way and install it via apt-get. The way ~~God~~ Linus intended it. Either way you'll be using the terminal a lot, might as well get to know it. Apt-get is a package manager that is used by Linux to install things on your computer. Very handy and have been cloned in both Mac (Homebrew) and Windows (Chocolatey) 1. Open the terminal by clicking the Ubuntu logo and search for Terminal 2. Now enter this command; "sudo apt-get install nodejs". Use CTRL+SHIFT+V to paste (CTRL+SHIFT+C to copy) in the terminal window 3. "sudo" just means that you run this as administrator, kinda. You will have to enter your password. 4. Wait while apt-get does it's magic. 5. Done. To check if it worked type "node --version" and you should see the version of Node that was just installed... Sadly... that didn't bring down Nodes own package manager, npm. We need that. Very soon. Let's do it again: 1. Open terminal 2. "sudo apt-get install npm" 3. Wait 4. Check with "npm --version" 5. Done And now npm is installed too. That's good we will be using it right away. Because the version of Node was way to low (in my case...0.6.12?!). But you can use npm to upgrade it. There's a very nifty little tool called "n". No, I'm not kidding. It's called n. Only. Install it with npm and then you can update your version of node pretty easy. 1. Open the terminal 2. First we need to clear out the cache of npm; "sudo npm cache clean -f" 3. Then install n; "sudo npm install -g n" (-g means install it globally on my system, not only in the local folder). If you got errors... see below. 4. Finally, use n to update NodeJs; "sudo n stable" (in this case we're updating to the most recent stable release, but you can update it to a version number too. "sudo n 0.11.9" for example if you're planning on doing KoaJs work...) 5. That means that you can install more than one version of Node on the system. And you can switch between version using the "n" command. Read more on their site #### Errors errors errors When I ran the "npm install" command for the first time I ran into some problems. The following error was shown "Error: failed to fetch from registry: n". Luckily others have had this problem too. The solution was to set the registry to use for npm, i.e. where it should look for packages. Here's the command > npm config > set registry <http://registry.npmjs.org/> But it didn't stop there. When I ran "sudo n stable" it came back by saying "/usr/local/bin/n: line 216: curl: command not found". Command not found?! I typed "curl" at the prompt and here is what it said: > style="font-family: Courier New, Courier, monospace;">marcus@marcusJsVm2:\~$ > curl > The program 'curl' is currently not installed. You can install it by > typing: > sudo apt-get install curl What a lovely error message! So I did that... "sudo apt-get install curl"... and now finally it worked! With that in place I could install n. And when that was done Node was installed. Let's press on! #### Mongo Db That apt-get thing was fun. Let's do it again, to install Mongo Db, the database of choice for most Node-developers. This is a little bit tricker since we need to add a keyserver to ensure the validity of the packages. But nothing we cannot handled. It's describe in greater detail here, on the manufacturers page. 1. Open the terminal 2. Add the MongoDb public key to apt-get's trusted servers; "sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10" 3. Create a MongoDb list file; "echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' \| sudo tee /etc/apt/sources.list.d/mongodb.list" 4. Reload the apt-get repository; "sudo apt-get update" 5. And now, finally we can install the latest version of MongoDb; "sudo apt-get install mongodb-10gen" 6. We are now done and MongoDb is running in the background, as a service, ready for us to use. Type "mongo" to open the interactive client. Phew! That was tricky. But as I said; the terminal is the Linux way. Might as well get used to it. Note that you could do all of this from the NodeJs site as well. But you wouldn't learn as much. Ok, Mongo is installed. On to the next. #### Robomongo As much as I'm getting used to using the terminal for almost everything it's some tasks where I like a GUI. Like browsing through and checking a database for collections and content for example. Robomongo is a graphical UI on MongoDb that I've come to love. Let's install it, but not via the terminal this time. That looked messy indeed. 1. Go to Robomongo 2. Click Download for Linux and pick the correct version 32/64 bit 3. Wait for the download to complete 4. Open the file which will open it in Ubuntu Software center. 5. Click Install 6. Done #### Git Git is the de facto standard for version control these days. You must know it. You must have it. As before trying to run git from the terminal without it being installed gives you a lovely error message; > The > program 'git' is currently not installed. You can install it by > typing: > sudo apt-get install git And then you just follow the instructions and a "sudo apt-get install git" later you are done. Git is installed. #### Global Node packages; Nodemon & Mocha Finally I usually install two packages gobally. Nodemon helps me monitor Node process so that I don't have to manually restart them when i make changes in my files. And Mocha is my choice of testing. Let's install both of them using npm; 1. In the terminal 2. "sudo npm install nodemon -g" 3. "sudo npm install mocha -g" 4. See you already fluent in this. 5. Done. ### Get an editor The only thing that is missing now is some way to write code. I use Sublime. I'm paying for it. You should too. One license goes for all platforms. It's well worth it! Install Sublime here. Other options is Brackets and Atom (if you can get an invite in time). I will not write much about this since it's very much up to everyone's taste (and the post is crazy long already). Depending on what you have installed you will probably need to install some add-ins as well. ### Summary If you stayed with me for this long, I'm very grateful and hope that you got value from this. I know that I was surprised how easy this environment was to work with. I hope you will like it too.